Merge branch 'feature/opinionated-sql' of github.com:Budibase/budibase into feature/opinionated-sql

This commit is contained in:
Martin McKeaveney 2021-06-17 14:49:27 +01:00
commit 577c62814c
5 changed files with 22 additions and 19 deletions

View File

@ -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 => {

View File

@ -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,

View File

@ -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 {

View File

@ -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 *`
})
})

View File

@ -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 }
}