From 7885e660258455934ab9e578e70ff5c37fdf16b7 Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Sun, 12 Apr 2020 20:17:15 +1200 Subject: [PATCH 1/2] Add dynamic table example per discussion in #12 --- README.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3e0a4424..799bda33 100644 --- a/README.md +++ b/README.md @@ -227,9 +227,9 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' })) This also means you cannot write dynamic queries or concat queries together by simple string manipulation. To enable dynamic queries in a safe way, the `sql` function doubles as a regular function which escapes any value properly. It also includes overloads for common cases of inserting, selecting, updating and querying. -## Dynamic query helpers `sql() inside tagged template` +## Dynamic query helpers - `sql()` inside tagged template -Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and where queries. +Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and `where` queries. #### Insert @@ -272,7 +272,6 @@ sql` sql(users, 'name', 'age') } ` - ``` #### Update @@ -312,6 +311,20 @@ sql` select name, age from users ``` +#### Dynamic table name + +```js + +const table = 'users' + +sql` + select id from ${sql(table)} +` + +// Is translated into this query: +select id from users +``` + #### Arrays `sql.array(Array)` PostgreSQL has a native array type which is similar to js arrays, but only allows the same type and shape for nested items. This method automatically infers the item type and serializes js arrays into PostgreSQL arrays. From c0ca5373a4b59701861b79c5d28dbdd17eb2816d Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Mon, 13 Apr 2020 10:30:51 +1200 Subject: [PATCH 2/2] Add dynamic table example per discussion in #12 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 799bda33..bee78b4a 100644 --- a/README.md +++ b/README.md @@ -229,7 +229,7 @@ This also means you cannot write dynamic queries or concat queries together by s ## Dynamic query helpers - `sql()` inside tagged template -Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic inserts, selects, updates and `where` queries. +Postgres.js has a safe, ergonomic way to aid you in writing queries. This makes it easier to write dynamic `insert`, `select` and `update` queries, and pass `where` parameters. #### Insert