Created
November 23, 2019 14:58
-
-
Save porsager/9c948225bcab6831c84576030436142f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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