Feat: abstract extended queries; strip out default funcs from db obj
This commit is contained in:
parent
471782cbd7
commit
081957c957
4 changed files with 694 additions and 632 deletions
|
|
@ -1,9 +1,36 @@
|
|||
const getSchema = () => 'public'
|
||||
const logger = require('./logger')
|
||||
|
||||
const getDefaultSchema = () => 'public'
|
||||
// TODO use asynclocalstorage
|
||||
const getSchema = () => {
|
||||
return 'public'
|
||||
}
|
||||
|
||||
const getDefaultSchema = () => {
|
||||
return 'ERROR_SCHEMA'
|
||||
}
|
||||
|
||||
const schemaNotFound = query => {
|
||||
logger.error(`Schema for query '${query}' has not been found and the query has returned null`)
|
||||
}
|
||||
|
||||
const stripDefaultDbFuncs = dbCtx => {
|
||||
return {
|
||||
ctx: dbCtx.ctx,
|
||||
query: dbCtx.$query,
|
||||
result: dbCtx.$result,
|
||||
many: dbCtx.$many,
|
||||
oneOrNone: dbCtx.$oneOrNone,
|
||||
one: dbCtx.$one,
|
||||
none: dbCtx.$none,
|
||||
any: dbCtx.$any,
|
||||
tx: dbCtx.$tx,
|
||||
task: dbCtx.$task
|
||||
}
|
||||
}
|
||||
|
||||
const any = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.any(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -15,6 +42,7 @@ const any = (obj, query, variables) => {
|
|||
|
||||
const none = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.none(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -26,6 +54,7 @@ const none = (obj, query, variables) => {
|
|||
|
||||
const one = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.one(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -37,6 +66,7 @@ const one = (obj, query, variables) => {
|
|||
|
||||
const oneOrNone = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.oneOrNone(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -48,6 +78,7 @@ const oneOrNone = (obj, query, variables) => {
|
|||
|
||||
const manyOrNone = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.manyOrNone(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -59,6 +90,7 @@ const manyOrNone = (obj, query, variables) => {
|
|||
|
||||
const many = (obj, query, variables) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.many(query, variables).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -70,6 +102,7 @@ const many = (obj, query, variables) => {
|
|||
|
||||
const result = (obj, query, variables, cb, thisArg) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) throw new Error(schemaNotFound(query))
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.result(query, variables, cb, thisArg).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
|
|
@ -79,10 +112,14 @@ const result = (obj, query, variables, cb, thisArg) => {
|
|||
})
|
||||
}
|
||||
|
||||
const query = (obj, query, values, qrm) => {
|
||||
const query = (obj, query, variables, qrm, throwOnError) => {
|
||||
const schema = getSchema()
|
||||
if (!schema) {
|
||||
if (throwOnError) throw new Error(schemaNotFound(query))
|
||||
return Promise.resolve(schemaNotFound(query))
|
||||
}
|
||||
return obj.taskEx({ schema }, t => {
|
||||
return t.query(query, values, qrm).then(res => {
|
||||
return t.query(query, variables, qrm).then(res => {
|
||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
||||
return res
|
||||
})
|
||||
|
|
@ -90,6 +127,18 @@ const query = (obj, query, values, qrm) => {
|
|||
})
|
||||
}
|
||||
|
||||
const tx = (obj, opts, cb) => {
|
||||
return obj.tx(opts, t => {
|
||||
return cb(stripDefaultDbFuncs(t))
|
||||
})
|
||||
}
|
||||
|
||||
const task = (obj, opts, cb) => {
|
||||
return obj.task(opts, t => {
|
||||
return cb(stripDefaultDbFuncs(t))
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
any,
|
||||
manyOrNone,
|
||||
|
|
@ -98,5 +147,8 @@ module.exports = {
|
|||
oneOrNone,
|
||||
one,
|
||||
result,
|
||||
query
|
||||
query,
|
||||
tx,
|
||||
task,
|
||||
stripDefaultDbFuncs
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue