Chore: remove extendedqueries file
This commit is contained in:
parent
64e07fa8bd
commit
eb5732003e
2 changed files with 64 additions and 175 deletions
85
lib/db.js
85
lib/db.js
|
|
@ -4,34 +4,77 @@ const _ = require('lodash/fp')
|
||||||
const psqlUrl = require('../lib/options').postgresql
|
const psqlUrl = require('../lib/options').postgresql
|
||||||
const logger = require('./logger')
|
const logger = require('./logger')
|
||||||
const eventBus = require('./event-bus')
|
const eventBus = require('./event-bus')
|
||||||
const extendedQueries = require('./extendedQueries')
|
|
||||||
|
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 _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))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const getSchema = () => 'public'
|
||||||
|
const getDefaultSchema = () => 'ERROR_SCHEMA'
|
||||||
|
|
||||||
|
const searchPathWrapper = (t, cb) => {
|
||||||
|
return t.none('SET search_path TO $1:name', [getSchema()])
|
||||||
|
.then(cb.bind(t, t))
|
||||||
|
.then(res => {
|
||||||
|
return t.none('SET search_path TO $1:name', [getDefaultSchema()]).then(() => {
|
||||||
|
return res
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
return t.none('SET search_path TO $1:name', [getDefaultSchema()]).then(() => {
|
||||||
|
throw err
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const pgp = Pgp({
|
const pgp = Pgp({
|
||||||
pgNative: true,
|
pgNative: true,
|
||||||
|
schema: 'ERROR_SCHEMA',
|
||||||
extend (obj, dbContext) {
|
extend (obj, dbContext) {
|
||||||
obj._taskEx = function () {
|
obj.__taskEx = function (cb, throwOnError = true) {
|
||||||
const args = pgp.utils.taskArgs(arguments)
|
const args = pgp.utils.taskArgs(arguments)
|
||||||
const { schema } = args.options
|
const schema = getSchema()
|
||||||
delete args.options.schema
|
if (!schema && throwOnError) {
|
||||||
if (schema) {
|
|
||||||
return obj.task.call(this, args.options, t => {
|
|
||||||
return t.none('SET search_path TO $1:name', [schema])
|
|
||||||
.then(args.cb.bind(t, t))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return Promise.reject(new Error('No schema selected, cannot complete query'))
|
return Promise.reject(new Error('No schema selected, cannot complete query'))
|
||||||
|
} else if (!schema) {
|
||||||
|
return Promise.resolve('No schema selected, cannot complete query')
|
||||||
}
|
}
|
||||||
obj.$query = (query, values, qrm) => extendedQueries.query(obj, query, values, qrm)
|
return obj.task.call(this, args.options, t => searchPathWrapper(t, cb))
|
||||||
obj.$result = (query, variables, cb, thisArg) => extendedQueries.result(obj, query, variables, cb, thisArg)
|
}
|
||||||
obj.$many = (query, variables) => extendedQueries.many(obj, query, variables)
|
obj.$query = (query, values, qrm, throwOnError) => obj.__taskEx(t => t.query(query, values, qrm), throwOnError)
|
||||||
obj.$manyOrNone = (query, variables) => extendedQueries.manyOrNone(obj, query, variables)
|
obj.$result = (query, variables, cb, thisArg) => obj.__taskEx(t => t.result(query, variables, cb, thisArg))
|
||||||
obj.$oneOrNone = (query, variables) => extendedQueries.oneOrNone(obj, query, variables)
|
obj.$many = (query, variables) => obj.__taskEx(t => t.many(query, variables))
|
||||||
obj.$one = (query, variables) => extendedQueries.one(obj, query, variables)
|
obj.$manyOrNone = (query, variables) => obj.__taskEx(t => t.manyOrNone(query, variables))
|
||||||
obj.$none = (query, variables) => extendedQueries.none(obj, query, variables)
|
obj.$oneOrNone = (query, variables) => obj.__taskEx(t => t.oneOrNone(query, variables))
|
||||||
obj.$any = (query, variables) => extendedQueries.any(obj, query, variables)
|
obj.$one = (query, variables) => obj.__taskEx(t => t.one(query, variables))
|
||||||
|
obj.$none = (query, variables) => obj.__taskEx(t => t.none(query, variables))
|
||||||
|
obj.$any = (query, variables) => obj.__taskEx(t => t.any(query, variables))
|
||||||
// when opts is not defined "cb" occupies the "opts" spot of the arguments
|
// when opts is not defined "cb" occupies the "opts" spot of the arguments
|
||||||
obj.$tx = (opts, cb) => typeof opts === 'function' ? extendedQueries.tx(obj, {}, opts) : extendedQueries.tx(obj, opts, cb)
|
obj.$tx = (opts, cb) => typeof opts === 'function' ? _tx(obj, {}, opts) : _tx(obj, opts, cb)
|
||||||
obj.$task = (opts, cb) => typeof opts === 'function' ? extendedQueries.task(obj, {}, opts) : extendedQueries.task(obj, opts, cb)
|
obj.$task = (opts, cb) => typeof opts === 'function' ? _task(obj, {}, opts) : _task(obj, opts, cb)
|
||||||
},
|
},
|
||||||
error: (err, e) => {
|
error: (err, e) => {
|
||||||
if (e.cn) logger.error('Database not reachable.')
|
if (e.cn) logger.error('Database not reachable.')
|
||||||
|
|
@ -43,7 +86,7 @@ const pgp = Pgp({
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const db = extendedQueries.stripDefaultDbFuncs(pgp(psqlUrl))
|
const db = stripDefaultDbFuncs(pgp(psqlUrl))
|
||||||
|
|
||||||
eventBus.subscribe('log', args => {
|
eventBus.subscribe('log', args => {
|
||||||
if (process.env.SKIP_SERVER_LOGS) return
|
if (process.env.SKIP_SERVER_LOGS) return
|
||||||
|
|
|
||||||
|
|
@ -1,154 +0,0 @@
|
||||||
const logger = require('./logger')
|
|
||||||
|
|
||||||
// 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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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, variables, qrm).then(res => {
|
|
||||||
return t.none('set search_path to $1~', [getDefaultSchema()]).then(() => {
|
|
||||||
return res
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
none,
|
|
||||||
many,
|
|
||||||
oneOrNone,
|
|
||||||
one,
|
|
||||||
result,
|
|
||||||
query,
|
|
||||||
tx,
|
|
||||||
task,
|
|
||||||
stripDefaultDbFuncs
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue