From 0cf316d2d6e33d7eeeb8a096e5bd5ec8715e97c5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 15 Jun 2021 13:50:41 +0100 Subject: [PATCH] Some quick updates after testing. --- .../src/api/controllers/row/external.js | 22 +++++++++++-------- .../server/src/api/controllers/row/utils.js | 2 +- packages/server/src/integrations/base/sql.js | 4 ++-- .../src/integrations/microsoftSqlServer.js | 2 +- packages/server/src/integrations/mysql.js | 2 +- .../server/src/integrations/plus/postgres.js | 2 +- packages/server/src/integrations/postgres.js | 2 +- 7 files changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index d91ea7cdaf..4b3cd0c2c5 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -53,6 +53,10 @@ async function handleRequest( entityId: tableName, operation, }, + resource: { + // not specifying any fields means "*" + fields: [], + }, filters: idFilters != null ? idFilters : filters, sort, paginate, @@ -68,7 +72,7 @@ exports.patch = async ctx => { const id = inputs._id // don't save the ID to db delete inputs._id - ctx.body = await handleRequest(appId, DataSourceOperation.UPDATE, tableId, { + return handleRequest(appId, DataSourceOperation.UPDATE, tableId, { id, row: inputs, }) @@ -81,7 +85,7 @@ exports.save = async ctx => { return exports.patch(ctx) } const tableId = ctx.params.tableId - ctx.body = await handleRequest(appId, DataSourceOperation.CREATE, tableId, { + return handleRequest(appId, DataSourceOperation.CREATE, tableId, { row: inputs, }) } @@ -96,14 +100,14 @@ exports.fetchView = async ctx => { exports.fetch = async ctx => { const appId = ctx.appId const tableId = ctx.params.tableId - ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId) + return handleRequest(appId, DataSourceOperation.READ, tableId) } exports.find = async ctx => { const appId = ctx.appId const id = ctx.params.rowId const tableId = ctx.params.tableId - ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, { + return handleRequest(appId, DataSourceOperation.READ, tableId, { id, }) } @@ -111,7 +115,7 @@ exports.find = async ctx => { exports.destroy = async ctx => { const appId = ctx.appId const tableId = ctx.params.tableId - ctx.body = await handleRequest(appId, DataSourceOperation.DELETE, tableId, { + return handleRequest(appId, DataSourceOperation.DELETE, tableId, { id: ctx.request.body._id, }) } @@ -128,7 +132,7 @@ exports.bulkDestroy = async ctx => { })) } await Promise.all(promises) - ctx.body = { response: { ok: true }, rows } + return { response: { ok: true }, rows } } exports.search = async ctx => { @@ -153,7 +157,7 @@ exports.search = async ctx => { [params.sort]: direction, } } - ctx.body = await handleRequest(appId, DataSourceOperation.READ, tableId, { + return handleRequest(appId, DataSourceOperation.READ, tableId, { filters: query, sort, paginate: paginateObj, @@ -162,10 +166,10 @@ exports.search = async ctx => { exports.validate = async ctx => { // can't validate external right now - maybe in future - ctx.body = { valid: true } + return { valid: true } } exports.fetchEnrichedRow = async ctx => { // TODO: How does this work - ctx.throw(501, "Not implemented") + throw "Not Implemented" } diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.js index a26ef85988..06c2e3faf8 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.js @@ -18,7 +18,7 @@ validateJs.extend(validateJs.validators.datetime, { exports.makeExternalQuery = async (appId, json) => { const datasourceId = json.endpoint.datasourceId - const database = new CouchDB(ctx.appId) + const database = new CouchDB(appId) const datasource = await database.get(datasourceId) const Integration = integrations[datasource.source] // query is the opinionated function diff --git a/packages/server/src/integrations/base/sql.js b/packages/server/src/integrations/base/sql.js index 4901574ffc..20e722d0db 100644 --- a/packages/server/src/integrations/base/sql.js +++ b/packages/server/src/integrations/base/sql.js @@ -3,8 +3,6 @@ const { DataSourceOperation, SortDirection } = require("../../constants") const BASE_LIMIT = 5000 function addFilters(query, filters) { - // if all or specified in filters, then everything is an or - const allOr = !!filters.allOr function iterate(structure, fn) { for (let [key, value] of Object.entries(structure)) { fn(key, value) @@ -13,6 +11,8 @@ function addFilters(query, filters) { if (!filters) { return query } + // if all or specified in filters, then everything is an or + const allOr = !!filters.allOr if (filters.string) { iterate(filters.string, (key, value) => { const fnc = allOr ? "orWhere" : "where" diff --git a/packages/server/src/integrations/microsoftSqlServer.js b/packages/server/src/integrations/microsoftSqlServer.js index 9e5203548f..488fda94b1 100644 --- a/packages/server/src/integrations/microsoftSqlServer.js +++ b/packages/server/src/integrations/microsoftSqlServer.js @@ -53,7 +53,7 @@ const SCHEMA = { async function internalQuery(client, sql) { try { - return await client.query(sql) + return await client.query(sql.sql, sql.bindings) } catch (err) { throw new Error(err) } diff --git a/packages/server/src/integrations/mysql.js b/packages/server/src/integrations/mysql.js index b54aedd7fa..0907a83f4c 100644 --- a/packages/server/src/integrations/mysql.js +++ b/packages/server/src/integrations/mysql.js @@ -57,7 +57,7 @@ function internalQuery(client, query) { // Node MySQL is callback based, so we must wrap our call in a promise return new Promise((resolve, reject) => { client.connect() - return client.query(query, (error, results) => { + return client.query(query.sql, query.bindings, (error, results) => { if (error) { reject(error) } else { diff --git a/packages/server/src/integrations/plus/postgres.js b/packages/server/src/integrations/plus/postgres.js index 56a490f1a1..43e93b9e94 100644 --- a/packages/server/src/integrations/plus/postgres.js +++ b/packages/server/src/integrations/plus/postgres.js @@ -119,7 +119,7 @@ class PostgresPlus extends Sql { async query(json) { const operation = this._operation(json).toLowerCase() const sql = this._query(json) - const response = await this.client.query(sql) + const response = await this.client.query(sql.sql, sql.bindings) return response.rows.length ? response.rows : [{ [operation]: true }] } } diff --git a/packages/server/src/integrations/postgres.js b/packages/server/src/integrations/postgres.js index 72b02431be..54cbcbe62a 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) + return await client.query(sql.sql, sql.bindings) } catch (err) { throw new Error(err) }