Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

untitled notebook

node v10.24.1
version: 1.0.0
endpointsharetweet
const {parse} = require("pgsql-ast-parser") function normalize(query) { let i = 0; const byName = {}; const normalized = query.replace(/(?<!:):(\w+)\b/gi, (_, x) => { if (byName[x]) { return '#x27; + byName[x]; } return '#x27; + (byName[x] = ++i) }); const keys = Object.keys(byName); function toArgList(args) { const ret = Array(keys.length); for (const k of keys) { ret[byName[k] - 1] = args[k]; } return ret; } return {normalized, toArgList}; } // transform a named query: const {normalized, toArgList} = normalize('select * from xxx where id = :id AND other=:other + :id'); console.log(normalized); console.log(toArgList({id: 'myId', other: 42 })); // parsable ! parse(normalized)
Loading…

no comments

    sign in to comment