diff --git a/packages/server/src/api/controllers/row/external.js b/packages/server/src/api/controllers/row/external.js index 7efcc11c75..58140faa7e 100644 --- a/packages/server/src/api/controllers/row/external.js +++ b/packages/server/src/api/controllers/row/external.js @@ -88,7 +88,7 @@ async function handleRequest( row = inputProcessing(row, table) if ( operation === DataSourceOperation.DELETE && - Object.keys(filters).length === 0 + (filters == null || Object.keys(filters).length === 0) ) { throw "Deletion must be filtered" } @@ -112,12 +112,10 @@ async function handleRequest( // we searched for rows in someway if (operation === DataSourceOperation.READ && Array.isArray(response)) { return outputProcessing(response, table) + } else { + row = outputProcessing(response, table)[0] + return { row, table } } - // append tableId back onto row if it exists - if (row) { - row.tableId = table._id - } - return { row, table } } exports.patch = async ctx => { @@ -167,9 +165,11 @@ exports.find = async ctx => { exports.destroy = async ctx => { const appId = ctx.appId const tableId = ctx.params.tableId - return handleRequest(appId, DataSourceOperation.DELETE, tableId, { - id: breakRowIdField(ctx.request.body._id), + const id = ctx.request.body._id + const { row } = await handleRequest(appId, DataSourceOperation.DELETE, tableId, { + id, }) + return { response: { ok: true }, row } } exports.bulkDestroy = async ctx => { diff --git a/packages/server/src/automations/steps/deleteRow.js b/packages/server/src/automations/steps/deleteRow.js index 9dde91fa30..94243fa03a 100644 --- a/packages/server/src/automations/steps/deleteRow.js +++ b/packages/server/src/automations/steps/deleteRow.js @@ -65,8 +65,8 @@ module.exports.run = async function ({ inputs, appId, apiKey, emitter }) { }, request: { body: { - rowId: inputs.id, - revId: inputs.revision, + _id: inputs.id, + _rev: inputs.revision, }, }, appId, diff --git a/packages/server/src/integrations/base/sql.js b/packages/server/src/integrations/base/sql.js index a99239cdd1..83a8e04aee 100644 --- a/packages/server/src/integrations/base/sql.js +++ b/packages/server/src/integrations/base/sql.js @@ -60,7 +60,7 @@ function addFilters(query, filters) { function buildCreate(knex, json) { const { endpoint, body } = json let query = knex(endpoint.entityId) - return query.insert(body) + return query.insert(body).returning("*") } function buildRead(knex, json, limit) { @@ -98,14 +98,14 @@ function buildUpdate(knex, json) { const { endpoint, body, filters } = json let query = knex(endpoint.entityId) query = addFilters(query, filters) - return query.update(body) + return query.update(body).returning("*") } function buildDelete(knex, json) { const { endpoint, filters } = json let query = knex(endpoint.entityId) query = addFilters(query, filters) - return query.delete() + return query.delete().returning("*") } class SqlQueryBuilder { diff --git a/packages/server/src/integrations/tests/sql.spec.js b/packages/server/src/integrations/tests/sql.spec.js index 80df7fe5ab..2b6badd92d 100644 --- a/packages/server/src/integrations/tests/sql.spec.js +++ b/packages/server/src/integrations/tests/sql.spec.js @@ -125,7 +125,7 @@ describe("SQL query builder", () => { })) expect(query).toEqual({ bindings: [45, "Michael"], - sql: `insert into "${TABLE_NAME}" ("age", "name") values ($1, $2)` + sql: `insert into "${TABLE_NAME}" ("age", "name") values ($1, $2) returning *` }) }) @@ -139,7 +139,7 @@ describe("SQL query builder", () => { })) expect(query).toEqual({ bindings: ["John", 1001], - sql: `update "${TABLE_NAME}" set "name" = $1 where "id" = $2` + sql: `update "${TABLE_NAME}" set "name" = $1 where "id" = $2 returning *` }) }) @@ -151,7 +151,7 @@ describe("SQL query builder", () => { })) expect(query).toEqual({ bindings: [1001], - sql: `delete from "${TABLE_NAME}" where "id" = $1` + sql: `delete from "${TABLE_NAME}" where "id" = $1 returning *` }) }) diff --git a/packages/server/src/integrations/utils.js b/packages/server/src/integrations/utils.js index 699995e63f..eefdbbadcb 100644 --- a/packages/server/src/integrations/utils.js +++ b/packages/server/src/integrations/utils.js @@ -1,17 +1,20 @@ const { DocumentTypes, SEPARATOR } = require("../db/utils") +const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` + exports.isExternalTable = tableId => { return tableId.includes(DocumentTypes.DATASOURCE) } exports.buildExternalTableId = (datasourceId, tableName) => { - return `${datasourceId}${SEPARATOR}${tableName}` + return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}` } exports.breakExternalTableId = tableId => { - const parts = tableId.split(SEPARATOR) + const parts = tableId.split(DOUBLE_SEPARATOR) let tableName = parts.pop() - let datasourceId = parts.join(SEPARATOR) + // if they need joined + let datasourceId = parts.join(DOUBLE_SEPARATOR) return { datasourceId, tableName } }