From e3cced9d9787b5a3ab7dd36d9b8117d73c8bd16b Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 15 Jun 2021 19:48:05 +0100 Subject: [PATCH] consolidate data sources and queries together in one UI --- .../DatasourceNavigator.svelte | 16 +++- .../DatasourceNavigator/icons/Budibase.svelte | 82 +++++++++++++++++++ .../DatasourceNavigator/icons/index.js | 2 + .../modals/CreateDatasourceModal.svelte | 2 +- .../TableNavigator/TableNavigator.svelte | 7 +- .../PropertyControls/DataSourceSelect.svelte | 2 +- packages/builder/src/constants/index.js | 2 + .../app/[application]/data/_layout.svelte | 34 ++------ .../[selectedDatasource]/index.svelte | 7 +- .../table/[selectedTable]/_layout.svelte | 13 +++ .../table/[selectedTable]/index.svelte | 16 ++++ .../[selectedField]/index.svelte | 10 +++ .../relationship/[selectedRow]/index.svelte | 6 ++ .../[selectedTable]/relationship/index.svelte | 6 ++ .../bb_internal/table/_layout.svelte | 19 +++++ .../datasource/bb_internal/table/index.svelte | 21 +++++ .../builder/src/stores/backend/datasources.js | 5 +- .../server/src/api/controllers/datasource.js | 22 ++++- .../server/src/api/controllers/table/index.js | 19 ++++- packages/server/src/api/routes/datasource.js | 2 +- packages/server/src/db/utils.js | 9 ++ packages/server/src/integrations/postgres.js | 2 +- 22 files changed, 257 insertions(+), 47 deletions(-) create mode 100644 packages/builder/src/components/backend/DatasourceNavigator/icons/Budibase.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/_layout.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/index.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/[selectedField]/index.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/index.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/index.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/_layout.svelte create mode 100644 packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/index.svelte diff --git a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte index 87daad4c3a..83edc08445 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/DatasourceNavigator.svelte @@ -1,10 +1,12 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js b/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js index 6d469e0904..e251595934 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js +++ b/packages/builder/src/components/backend/DatasourceNavigator/icons/index.js @@ -9,8 +9,10 @@ import SqlServer from "./SQLServer.svelte" import MySQL from "./MySQL.svelte" import ArangoDB from "./ArangoDB.svelte" import Rest from "./Rest.svelte" +import Budibase from "./Budibase.svelte" export default { + BUDIBASE: Budibase, POSTGRES: Postgres, DYNAMODB: DynamoDB, MONGODB: MongoDB, diff --git a/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte b/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte index 180d03f7ff..ca573d083b 100644 --- a/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte +++ b/packages/builder/src/components/backend/DatasourceNavigator/modals/CreateDatasourceModal.svelte @@ -1,6 +1,6 @@ -{#if datasource} +{#if datasource && integration}
diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/_layout.svelte new file mode 100644 index 0000000000..14f6303e5f --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/_layout.svelte @@ -0,0 +1,13 @@ + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/index.svelte new file mode 100644 index 0000000000..a68c0dc651 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/index.svelte @@ -0,0 +1,16 @@ + + +{#if $database?._id && $tables?.selected?.name} + +{:else}Create your first table to start building{/if} + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/[selectedField]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/[selectedField]/index.svelte new file mode 100644 index 0000000000..eddb5ab598 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/[selectedField]/index.svelte @@ -0,0 +1,10 @@ + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/index.svelte new file mode 100644 index 0000000000..8e195ddb12 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/[selectedRow]/index.svelte @@ -0,0 +1,6 @@ + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/index.svelte new file mode 100644 index 0000000000..7d081b6976 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/[selectedTable]/relationship/index.svelte @@ -0,0 +1,6 @@ + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/_layout.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/_layout.svelte new file mode 100644 index 0000000000..9ab874a4d0 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/_layout.svelte @@ -0,0 +1,19 @@ + + + diff --git a/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/index.svelte b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/index.svelte new file mode 100644 index 0000000000..6d61614145 --- /dev/null +++ b/packages/builder/src/pages/builder/app/[application]/data/datasource/bb_internal/table/index.svelte @@ -0,0 +1,21 @@ + + +{#if $tables.list.length === 0} + Create your first table to start building +{:else}Select a table to edit{/if} + + diff --git a/packages/builder/src/stores/backend/datasources.js b/packages/builder/src/stores/backend/datasources.js index 3615596882..186068d28c 100644 --- a/packages/builder/src/stores/backend/datasources.js +++ b/packages/builder/src/stores/backend/datasources.js @@ -28,10 +28,11 @@ export function createDatasourcesStore() { update(state => ({ ...state, selected: datasourceId })) queries.update(state => ({ ...state, selected: null })) }, - save: async datasource => { + save: async (datasource, opts = {}) => { let url = "/api/datasources" - if (datasource.plus) { + if (datasource.plus && opts.refresh) { + // Pull the latest tables from the datasource url += "?refresh=1" } diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index 0aff868bb6..44bcfcd9ee 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -4,6 +4,8 @@ const { getDatasourceParams, getQueryParams, DocumentTypes, + BudibaseInternalDB, + getTableParams, } = require("../../db/utils") const { integrations } = require("../../integrations") const plusIntegrations = require("../../integrations/plus") @@ -11,13 +13,31 @@ const { makeExternalQuery } = require("./row/utils") exports.fetch = async function (ctx) { const database = new CouchDB(ctx.appId) - ctx.body = ( + + // Get internal tables + const db = new CouchDB(ctx.appId) + const internalTables = await db.allDocs( + getTableParams(null, { + include_docs: true, + }) + ) + const internal = internalTables.rows.map(row => row.doc) + + const bbInternalDb = { + ...BudibaseInternalDB, + entities: internal, + } + + // Get external datasources + const datasources = ( await database.allDocs( getDatasourceParams(null, { include_docs: true, }) ) ).rows.map(row => row.doc) + + ctx.body = [bbInternalDb, ...datasources] } exports.save = async function (ctx) { diff --git a/packages/server/src/api/controllers/table/index.js b/packages/server/src/api/controllers/table/index.js index d919f140e2..35736fe399 100644 --- a/packages/server/src/api/controllers/table/index.js +++ b/packages/server/src/api/controllers/table/index.js @@ -6,27 +6,38 @@ const { getTableParams, generateTableID, getDatasourceParams, + DocumentTypes, + BudibaseInternalDB, } = require("../../../db/utils") const { FieldTypes } = require("../../../constants") const { TableSaveFunctions } = require("./utils") exports.fetch = async function (ctx) { const db = new CouchDB(ctx.appId) + const internalTables = await db.allDocs( getTableParams(null, { include_docs: true, }) ) - const internal = internalTables.rows.map(row => row.doc) + + const internal = internalTables.rows.map(row => ({ + ...row.doc, + sourceId: BudibaseInternalDB._id, + })) const externalTables = await db.allDocs( getDatasourceParams("plus", { include_docs: true, }) ) - const external = externalTables.rows.flatMap(row => - Object.values(row.doc.entities) - ) + + const external = externalTables.rows.flatMap(row => { + return Object.values(row.doc.entities).map(entity => ({ + ...entity, + sourceId: row.doc._id, + })) + }) ctx.body = [...internal, ...external] } diff --git a/packages/server/src/api/routes/datasource.js b/packages/server/src/api/routes/datasource.js index 2a2952d005..06964b61fd 100644 --- a/packages/server/src/api/routes/datasource.js +++ b/packages/server/src/api/routes/datasource.js @@ -17,7 +17,7 @@ function generateDatasourceSchema() { return joiValidator.body(Joi.object({ _id: Joi.string(), _rev: Joi.string(), - source: Joi.string().valid("POSTGRES_PLUS"), + // source: Joi.string().valid("POSTGRES_PLUS"), type: Joi.string().allow("datasource_plus"), relationships: Joi.array().items(Joi.object({ from: Joi.string().required(), diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index 64da148b07..d382bdbccc 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -57,6 +57,14 @@ exports.StaticDatabases = { ...StaticDatabases, } +const BudibaseInternalDB = { + _id: "bb_internal", + type: "budibase", + name: "Budibase Internal", + source: "BUDIBASE", + config: {}, +} + exports.APP_PREFIX = APP_PREFIX exports.APP_DEV_PREFIX = APP_DEV_PREFIX exports.USER_METDATA_PREFIX = `${DocumentTypes.ROW}${SEPARATOR}${InternalTables.USER_METADATA}${SEPARATOR}` @@ -68,6 +76,7 @@ exports.SEPARATOR = SEPARATOR exports.UNICODE_MAX = UNICODE_MAX exports.SearchIndexes = SearchIndexes exports.AppStatus = AppStatus +exports.BudibaseInternalDB = BudibaseInternalDB exports.generateRoleID = generateRoleID exports.getRoleParams = getRoleParams diff --git a/packages/server/src/integrations/postgres.js b/packages/server/src/integrations/postgres.js index 54cbcbe62a..72b02431be 100644 --- a/packages/server/src/integrations/postgres.js +++ b/packages/server/src/integrations/postgres.js @@ -57,7 +57,7 @@ const SCHEMA = { async function internalQuery(client, sql) { try { - return await client.query(sql.sql, sql.bindings) + return await client.query(sql) } catch (err) { throw new Error(err) }