From de279fa6a9990640fa7a6d6ef2bc3c70a5dec080 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 15 Jun 2021 13:32:11 +0100 Subject: [PATCH 1/2] removing _all concept for tables, more work on plus datasource --- .../store/screenTemplates/rowDetailScreen.js | 2 +- .../store/screenTemplates/rowListScreen.js | 2 +- .../backend/DataTable/DataTable.svelte | 26 +++++++++---------- .../backend/DataTable/ViewDataTable.svelte | 7 +++-- .../src/components/backend/DataTable/api.js | 4 +-- .../TableNavigator/TableNavigator.svelte | 2 +- packages/builder/src/stores/backend/tables.js | 2 +- .../src/stores/backend/tests/tables.spec.js | 2 +- .../server/src/api/controllers/datasource.js | 2 +- .../server/src/api/routes/tests/row.spec.js | 2 +- .../server/src/integrations/plus/postgres.js | 13 +++++++--- 11 files changed, 33 insertions(+), 31 deletions(-) diff --git a/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js b/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js index f15f1de5a4..4292e29674 100644 --- a/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js +++ b/packages/builder/src/builderStore/store/screenTemplates/rowDetailScreen.js @@ -84,7 +84,7 @@ const createScreen = table => { .customProps({ dataSource: { label: table.name, - name: `all_${table._id}`, + name: table._id, tableId: table._id, type: "table", }, diff --git a/packages/builder/src/builderStore/store/screenTemplates/rowListScreen.js b/packages/builder/src/builderStore/store/screenTemplates/rowListScreen.js index f31c4c12d2..da966132b5 100644 --- a/packages/builder/src/builderStore/store/screenTemplates/rowListScreen.js +++ b/packages/builder/src/builderStore/store/screenTemplates/rowListScreen.js @@ -75,7 +75,7 @@ const createScreen = table => { .customProps({ dataSource: { label: table.name, - name: `all_${table._id}`, + name: table._id, tableId: table._id, type: "table", }, diff --git a/packages/builder/src/components/backend/DataTable/DataTable.svelte b/packages/builder/src/components/backend/DataTable/DataTable.svelte index a030adfcf6..e79ee1074c 100644 --- a/packages/builder/src/components/backend/DataTable/DataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/DataTable.svelte @@ -27,22 +27,20 @@ // Fetch rows for specified table $: { - if ($views.selected?.name?.startsWith("all_")) { - loading = true - const loadingTableId = $tables.selected?._id - api.fetchDataForView($views.selected).then(rows => { - loading = false + loading = true + const loadingTableId = $tables.selected?._id + api.fetchDataForTable($tables.selected._id).then(rows => { + loading = false - // If we started a slow request then quickly change table, sometimes - // the old data overwrites the new data. - // This check ensures that we don't do that. - if (loadingTableId !== $tables.selected?._id) { - return - } + // If we started a slow request then quickly change table, sometimes + // the old data overwrites the new data. + // This check ensures that we don't do that. + if (loadingTableId !== $tables.selected?._id) { + return + } - data = rows || [] - }) - } + data = rows || [] + }) } diff --git a/packages/builder/src/components/backend/DataTable/ViewDataTable.svelte b/packages/builder/src/components/backend/DataTable/ViewDataTable.svelte index 6a555cac58..7db09f4480 100644 --- a/packages/builder/src/components/backend/DataTable/ViewDataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/ViewDataTable.svelte @@ -20,10 +20,8 @@ // Fetch rows for specified view $: { - if (!name.startsWith("all_")) { - loading = true - fetchViewData(name, view.field, view.groupBy, view.calculation) - } + loading = true + fetchViewData(name, view.field, view.groupBy, view.calculation) } async function fetchViewData(name, field, groupBy, calculation) { @@ -32,6 +30,7 @@ const thisView = allTableViews.filter( views => views != null && views[name] != null )[0] + // don't fetch view data if the view no longer exists if (!thisView) { return diff --git a/packages/builder/src/components/backend/DataTable/api.js b/packages/builder/src/components/backend/DataTable/api.js index bac3b01273..b461c70c4b 100644 --- a/packages/builder/src/components/backend/DataTable/api.js +++ b/packages/builder/src/components/backend/DataTable/api.js @@ -21,8 +21,8 @@ export async function deleteRow(row) { }) } -export async function fetchDataForView(view) { - const FETCH_ROWS_URL = `/api/views/${view.name}` +export async function fetchDataForTable(tableId) { + const FETCH_ROWS_URL = `/api/${tableId}/rows` const response = await api.get(FETCH_ROWS_URL) const json = await response.json() diff --git a/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte b/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte index fa8733a8b3..0d219a0f6c 100644 --- a/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte +++ b/packages/builder/src/components/backend/TableNavigator/TableNavigator.svelte @@ -36,7 +36,7 @@ border={idx > 0} icon={table._id === TableNames.USERS ? "UserGroup" : "Table"} text={table.name} - selected={selectedView === `all_${table._id}`} + selected={selectedView === table._id} on:click={() => selectTable(table)} > {#if table._id !== TableNames.USERS} diff --git a/packages/builder/src/stores/backend/tables.js b/packages/builder/src/stores/backend/tables.js index 34bd5a9a24..2c0d065919 100644 --- a/packages/builder/src/stores/backend/tables.js +++ b/packages/builder/src/stores/backend/tables.js @@ -25,7 +25,7 @@ export function createTablesStore() { selected: table, draft: cloneDeep(table), })) - views.select({ name: `all_${table._id}` }) + views.select({ name: table._id }) } } diff --git a/packages/builder/src/stores/backend/tests/tables.spec.js b/packages/builder/src/stores/backend/tests/tables.spec.js index ef65632ec8..72b68bd0e4 100644 --- a/packages/builder/src/stores/backend/tests/tables.spec.js +++ b/packages/builder/src/stores/backend/tests/tables.spec.js @@ -46,7 +46,7 @@ describe("Tables Store", () => { await store.select(tableToSelect) expect(get(store).selected).toEqual(tableToSelect) - expect(get(views).selected).toEqual({ name: `all_${tableToSelect._id}` }) + expect(get(views).selected).toEqual({ name: tableToSelect._id }) }) it("saving a table also selects it", async () => { diff --git a/packages/server/src/api/controllers/datasource.js b/packages/server/src/api/controllers/datasource.js index e79834b7c5..51731f3931 100644 --- a/packages/server/src/api/controllers/datasource.js +++ b/packages/server/src/api/controllers/datasource.js @@ -34,7 +34,7 @@ exports.save = async function (ctx) { if (ctx.query.refresh) { const PlusConnector = plusIntegrations[datasource.source].integration - const connector = new PlusConnector(ctx.request.body.config) + const connector = new PlusConnector(ctx.request.body.config, datasource) await connector.init() datasource.entities = connector.tables diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index 69b1031a99..3e26915744 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -309,7 +309,7 @@ describe("/rows", () => { it("should be able to fetch tables contents via 'view'", async () => { const row = await config.createRow() const res = await request - .get(`/api/views/all_${table._id}`) + .get(`/api/views/${table._id}`) .set(config.defaultHeaders()) .expect('Content-Type', /json/) .expect(200) diff --git a/packages/server/src/integrations/plus/postgres.js b/packages/server/src/integrations/plus/postgres.js index 3ac49dc925..c379463178 100644 --- a/packages/server/src/integrations/plus/postgres.js +++ b/packages/server/src/integrations/plus/postgres.js @@ -2,6 +2,7 @@ const Sql = require("../base/sql") const { Pool } = require("pg") const { FieldTypes } = require("../../constants") const { FIELD_TYPES } = require("../Integration") +const { SEPARATOR } = require("@budibase/auth/db") const TYPE_MAP = { text: FieldTypes.LONGFORM, @@ -60,7 +61,7 @@ class PostgresPlus extends Sql { "select * from information_schema.columns where table_schema = 'public'" PRIMARY_KEYS_SQL = ` - select tc.table_schema, tc.table_name, tc.column_name, kc.column_name as primary_key + select tc.table_schema, tc.table_name, kc.column_name as primary_key from information_schema.table_constraints tc join information_schema.key_column_usage kc on kc.table_name = tc.table_name @@ -69,9 +70,11 @@ class PostgresPlus extends Sql { where tc.constraint_type = 'PRIMARY KEY'; ` - constructor(config) { + constructor(config, datasource) { super("pg") this.config = config + this.datasource = datasource + if (!this.pool) { this.pool = new Pool(this.config) } @@ -84,7 +87,7 @@ class PostgresPlus extends Sql { const primaryKeys = {} for (let table of primaryKeysResponse.rows) { - primaryKeys[table.primary_key] = table.column_name + primaryKeys[table.column_name] = table.primary_key } const columnsResponse = await this.client.query(this.COLUMNS_SQL) @@ -97,7 +100,9 @@ class PostgresPlus extends Sql { // table key doesn't exist yet if (!tables[tableName]) { tables[tableName] = { - _id: primaryKeys[tableName], + _id: `${this.datasource._id}${SEPARATOR}${tableName}`, + // TODO: this needs to accommodate composite keys + primary: primaryKeys[tableName], name: tableName, schema: {}, } From 0739e07a5f546385f925219ce6fac923b5c5c01b Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Tue, 15 Jun 2021 13:47:08 +0100 Subject: [PATCH 2/2] merge --- packages/server/src/api/controllers/row/external.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index d91ea7cdaf..1025365cdd 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -123,9 +123,11 @@ exports.bulkDestroy = async ctx => { // TODO: this can probably be optimised to a single SQL statement in the future let promises = [] for (let row of rows) { - promises.push(handleRequest(appId, DataSourceOperation.DELETE, tableId, { - id: row._id, - })) + promises.push( + handleRequest(appId, DataSourceOperation.DELETE, tableId, { + id: row._id, + }) + ) } await Promise.all(promises) ctx.body = { response: { ok: true }, rows }