diff --git a/README.md b/README.md index 421d19a0..b2d32216 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ ### Installation ```bash -$ npm install postgres +$ npm install postgres@github:DanielFGray/postgres.js ``` ### Usage @@ -342,6 +342,23 @@ select * from users select * from users where user_id = $1 ``` +### Dynamic fragments +Multiple fragments can be joined together with `sql.join` to create more complex dynamic queries: + +```js +// this could be built dynamically +let expressions = [sql`name is not null`, sql`age > 50`] +// simple join: +sql`select * from from users where ${ + sql.join` and `(expressions) +}` +// dynamic join +const separator = params.or === 1 ? sql.join` or ` : sql.join` and ` +sql`select * from from users where ${ + separator(expressions) +}` +``` + ### SQL functions Using keywords or calling functions dynamically is also possible by using ``` sql`` ``` fragments. ```js diff --git a/src/index.js b/src/index.js index 0573e2bc..cedb5093 100644 --- a/src/index.js +++ b/src/index.js @@ -98,6 +98,7 @@ function Postgres(a, b) { notify, array, json, + join, file }) @@ -318,6 +319,15 @@ function Postgres(a, b) { return new Parameter(x, 3802) } + function join(sep) { + return xs => xs.flatMap((x, i) => { + if (i === 0) return x + const s = new Builder([sep], []) + if (Array.isArray(x)) return [s, ...x] + return [s, x] + }) + } + function array(x, type) { if (!Array.isArray(x)) return array(Array.from(arguments)) diff --git a/tests/index.js b/tests/index.js index bf81b036..5c1618fd 100644 --- a/tests/index.js +++ b/tests/index.js @@ -2469,6 +2469,14 @@ t('Supports arrays of fragments', async() => { ] }) +t("Joins fragments with a separator", () => { + const fs = [sql`a = ${1}`, sql`b = ${"test"}`] + return [ + sql`select * from t where ${ sql.join` and `(fs) }`.describe().string, + 'select * from t where a = $1 and b = $2' + ] +}); + t('Does not try rollback when commit errors', async() => { let notice = null const sql = postgres({ ...options, onnotice: x => notice = x }) diff --git a/types/index.d.ts b/types/index.d.ts index 8989ff47..74c00234 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -702,6 +702,8 @@ declare namespace postgres { file(path: string | Buffer | URL | number, options?: { cache?: boolean | undefined } | undefined): PendingQuery; file(path: string | Buffer | URL | number, args: (ParameterOrJSON)[], options?: { cache?: boolean | undefined } | undefined): PendingQuery; json(value: JSONValue): Parameter; + join(a: TemplateStringsArray): (p: PendingQuery[]) => PendingQuery + join(a: TemplateStringsArray): (...p: PendingQuery[]) => PendingQuery reserve(): Promise> }