Skip to content

Commit 5072079

Browse files
authored
Add dynamic table example per discussion in porsager#12 (porsager#56)
1 parent 6ef01f5 commit 5072079

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ sql.notify('news', JSON.stringify({ no: 'this', is: 'news' }))
227227

228228
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.
229229

230-
## Dynamic query helpers `sql() inside tagged template`
230+
## Dynamic query helpers - `sql()` inside tagged template
231231

232-
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.
232+
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.
233233

234234
#### Insert
235235

@@ -272,7 +272,6 @@ sql`
272272
sql(users, 'name', 'age')
273273
}
274274
`
275-
276275
```
277276

278277
#### Update
@@ -312,6 +311,20 @@ sql`
312311
select name, age from users
313312
```
314313

314+
#### Dynamic table name
315+
316+
```js
317+
318+
const table = 'users'
319+
320+
sql`
321+
select id from ${sql(table)}
322+
`
323+
324+
// Is translated into this query:
325+
select id from users
326+
```
327+
315328
#### Arrays `sql.array(Array)`
316329

317330
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.

0 commit comments

Comments
 (0)