Merge branch 'feature/opinionated-sql' of github.com:Budibase/budibase into feature/opinionated-sql
This commit is contained in:
commit
577c62814c
|
@ -88,7 +88,7 @@ async function handleRequest(
|
||||||
row = inputProcessing(row, table)
|
row = inputProcessing(row, table)
|
||||||
if (
|
if (
|
||||||
operation === DataSourceOperation.DELETE &&
|
operation === DataSourceOperation.DELETE &&
|
||||||
Object.keys(filters).length === 0
|
(filters == null || Object.keys(filters).length === 0)
|
||||||
) {
|
) {
|
||||||
throw "Deletion must be filtered"
|
throw "Deletion must be filtered"
|
||||||
}
|
}
|
||||||
|
@ -112,12 +112,10 @@ async function handleRequest(
|
||||||
// we searched for rows in someway
|
// we searched for rows in someway
|
||||||
if (operation === DataSourceOperation.READ && Array.isArray(response)) {
|
if (operation === DataSourceOperation.READ && Array.isArray(response)) {
|
||||||
return outputProcessing(response, table)
|
return outputProcessing(response, table)
|
||||||
}
|
} else {
|
||||||
// append tableId back onto row if it exists
|
row = outputProcessing(response, table)[0]
|
||||||
if (row) {
|
|
||||||
row.tableId = table._id
|
|
||||||
}
|
|
||||||
return { row, table }
|
return { row, table }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.patch = async ctx => {
|
exports.patch = async ctx => {
|
||||||
|
@ -167,9 +165,11 @@ exports.find = async ctx => {
|
||||||
exports.destroy = async ctx => {
|
exports.destroy = async ctx => {
|
||||||
const appId = ctx.appId
|
const appId = ctx.appId
|
||||||
const tableId = ctx.params.tableId
|
const tableId = ctx.params.tableId
|
||||||
return handleRequest(appId, DataSourceOperation.DELETE, tableId, {
|
const id = ctx.request.body._id
|
||||||
id: breakRowIdField(ctx.request.body._id),
|
const { row } = await handleRequest(appId, DataSourceOperation.DELETE, tableId, {
|
||||||
|
id,
|
||||||
})
|
})
|
||||||
|
return { response: { ok: true }, row }
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.bulkDestroy = async ctx => {
|
exports.bulkDestroy = async ctx => {
|
||||||
|
|
|
@ -65,8 +65,8 @@ module.exports.run = async function ({ inputs, appId, apiKey, emitter }) {
|
||||||
},
|
},
|
||||||
request: {
|
request: {
|
||||||
body: {
|
body: {
|
||||||
rowId: inputs.id,
|
_id: inputs.id,
|
||||||
revId: inputs.revision,
|
_rev: inputs.revision,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
appId,
|
appId,
|
||||||
|
|
|
@ -60,7 +60,7 @@ function addFilters(query, filters) {
|
||||||
function buildCreate(knex, json) {
|
function buildCreate(knex, json) {
|
||||||
const { endpoint, body } = json
|
const { endpoint, body } = json
|
||||||
let query = knex(endpoint.entityId)
|
let query = knex(endpoint.entityId)
|
||||||
return query.insert(body)
|
return query.insert(body).returning("*")
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildRead(knex, json, limit) {
|
function buildRead(knex, json, limit) {
|
||||||
|
@ -98,14 +98,14 @@ function buildUpdate(knex, json) {
|
||||||
const { endpoint, body, filters } = json
|
const { endpoint, body, filters } = json
|
||||||
let query = knex(endpoint.entityId)
|
let query = knex(endpoint.entityId)
|
||||||
query = addFilters(query, filters)
|
query = addFilters(query, filters)
|
||||||
return query.update(body)
|
return query.update(body).returning("*")
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildDelete(knex, json) {
|
function buildDelete(knex, json) {
|
||||||
const { endpoint, filters } = json
|
const { endpoint, filters } = json
|
||||||
let query = knex(endpoint.entityId)
|
let query = knex(endpoint.entityId)
|
||||||
query = addFilters(query, filters)
|
query = addFilters(query, filters)
|
||||||
return query.delete()
|
return query.delete().returning("*")
|
||||||
}
|
}
|
||||||
|
|
||||||
class SqlQueryBuilder {
|
class SqlQueryBuilder {
|
||||||
|
|
|
@ -125,7 +125,7 @@ describe("SQL query builder", () => {
|
||||||
}))
|
}))
|
||||||
expect(query).toEqual({
|
expect(query).toEqual({
|
||||||
bindings: [45, "Michael"],
|
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({
|
expect(query).toEqual({
|
||||||
bindings: ["John", 1001],
|
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({
|
expect(query).toEqual({
|
||||||
bindings: [1001],
|
bindings: [1001],
|
||||||
sql: `delete from "${TABLE_NAME}" where "id" = $1`
|
sql: `delete from "${TABLE_NAME}" where "id" = $1 returning *`
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,20 @@
|
||||||
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
||||||
|
|
||||||
|
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
||||||
|
|
||||||
exports.isExternalTable = tableId => {
|
exports.isExternalTable = tableId => {
|
||||||
return tableId.includes(DocumentTypes.DATASOURCE)
|
return tableId.includes(DocumentTypes.DATASOURCE)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.buildExternalTableId = (datasourceId, tableName) => {
|
exports.buildExternalTableId = (datasourceId, tableName) => {
|
||||||
return `${datasourceId}${SEPARATOR}${tableName}`
|
return `${datasourceId}${DOUBLE_SEPARATOR}${tableName}`
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.breakExternalTableId = tableId => {
|
exports.breakExternalTableId = tableId => {
|
||||||
const parts = tableId.split(SEPARATOR)
|
const parts = tableId.split(DOUBLE_SEPARATOR)
|
||||||
let tableName = parts.pop()
|
let tableName = parts.pop()
|
||||||
let datasourceId = parts.join(SEPARATOR)
|
// if they need joined
|
||||||
|
let datasourceId = parts.join(DOUBLE_SEPARATOR)
|
||||||
return { datasourceId, tableName }
|
return { datasourceId, tableName }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue