Some quick updates after testing.

This commit is contained in:
mike12345567 2021-06-15 13:50:41 +01:00
parent a056176050
commit 765e38aafc
7 changed files with 20 additions and 16 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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