Skip to content

Instantly share code, notes, and snippets.

@porsager
Created November 23, 2019 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porsager/9c948225bcab6831c84576030436142f to your computer and use it in GitHub Desktop.
Save porsager/9c948225bcab6831c84576030436142f to your computer and use it in GitHub Desktop.
const Pgp = require('pg-promise')
, config = require('./config')
const pgp = new Pgp(options)
, db = pgp(config.postgresUrl)
module.exports = function(x, ...args) {
if (!Array.isArray(x) || !Array.isArray(x.raw))
throw new Error('db queries must be done as template strings')
let add = 0
return db.query(x.reduce((acc, x, i) =>
acc += (
Array.isArray(args[i - 1])
? args[i - 1].map((x) =>
Array.isArray(x)
? '(' + x.map(x => '$(w.' + (-1 + i + add++) + ')').join(',') + ')'
: '$(w.' + (-1 + i + add++) + ')'
).join(',')
: ('$(w.' + (-1 + i) + ')')
)
+ x
), { w: args.flat(Infinity) })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment