@vanit-co/sql-ts 0.9.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -2
- package/dist/index.js +4 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -305,7 +305,7 @@ rows.map(row => ({
|
|
|
305
305
|
Builds a parameterised `INSERT` statement. Accepts one or more row objects. Column names from the first row are used; table and column names are properly quoted identifiers (never bind params).
|
|
306
306
|
|
|
307
307
|
```ts
|
|
308
|
-
import { schema, insert } from '@vanit-co/sql-ts'
|
|
308
|
+
import { schema, insert, raw } from '@vanit-co/sql-ts'
|
|
309
309
|
|
|
310
310
|
const users = schema({ table: 'users', columns: ['id', 'email'] })
|
|
311
311
|
|
|
@@ -321,14 +321,21 @@ const q2 = insert(users,
|
|
|
321
321
|
)
|
|
322
322
|
q2.text // insert into "users" ("id" ,"email") values ($1 ,$2) ,($3 ,$4)
|
|
323
323
|
q2.values // [1, 'alice@example.com', 2, 'bob@example.com']
|
|
324
|
+
|
|
325
|
+
// Raw SQL expressions instead of bind params
|
|
326
|
+
const q3 = insert(users, { id: raw("nextval('users_id_seq')"), email: 'alice@example.com' })
|
|
327
|
+
q3.text // insert into "users" ("id" ,"email") values (nextval('users_id_seq') ,$1)
|
|
328
|
+
q3.values // ['alice@example.com']
|
|
324
329
|
```
|
|
325
330
|
|
|
331
|
+
Column objects used as values are resolved to quoted identifiers rather than bound. They render unprefixed, since the statement has no alias in scope.
|
|
332
|
+
|
|
326
333
|
### `update(table, colsVals)`
|
|
327
334
|
|
|
328
335
|
Builds a parameterised `UPDATE ... SET ...` statement (without a `WHERE` clause - compose that separately using `concat`).
|
|
329
336
|
|
|
330
337
|
```ts
|
|
331
|
-
import { schema, update } from '@vanit-co/sql-ts'
|
|
338
|
+
import { schema, update, raw } from '@vanit-co/sql-ts'
|
|
332
339
|
|
|
333
340
|
const users = schema({ table: 'users', columns: ['id', 'email'] })
|
|
334
341
|
|
|
@@ -340,6 +347,16 @@ q.values // ['new@example.com']
|
|
|
340
347
|
const q2 = update(users, { id: 99, email: 'updated@example.com' })
|
|
341
348
|
q2.text // update "users" set "id" = $1 ,"email" = $2
|
|
342
349
|
q2.values // [99, 'updated@example.com']
|
|
350
|
+
|
|
351
|
+
// Raw SQL expressions instead of bind params
|
|
352
|
+
const q3 = update(users, { id: raw('id + 1'), email: 'updated@example.com' })
|
|
353
|
+
q3.text // update "users" set "id" = id + 1 ,"email" = $1
|
|
354
|
+
q3.values // ['updated@example.com']
|
|
355
|
+
|
|
356
|
+
// Column objects as values, resolved to quoted identifiers
|
|
357
|
+
const q4 = update(users, { email: users.id })
|
|
358
|
+
q4.text // update "users" set "email" = "id"
|
|
359
|
+
q4.values // []
|
|
343
360
|
```
|
|
344
361
|
|
|
345
362
|
---
|
|
@@ -481,6 +498,8 @@ q.text // SELECT COUNT(*) AS total FROM users
|
|
|
481
498
|
q.values // []
|
|
482
499
|
```
|
|
483
500
|
|
|
501
|
+
`raw` works anywhere a value is accepted, including the column values passed to `insert` and `update`.
|
|
502
|
+
|
|
484
503
|
---
|
|
485
504
|
|
|
486
505
|
## Prepared statements: `preparedStatementName`
|
package/dist/index.js
CHANGED
|
@@ -140,6 +140,7 @@ const sql = buildTag(pure);
|
|
|
140
140
|
const select = buildTag(prefix);
|
|
141
141
|
const selectAs = buildTag(alias);
|
|
142
142
|
const empty = sql ``;
|
|
143
|
+
const value = (v) => v && v[SYM_COLUMN] ? identifier(v[SYM_COLUMN].name) : v;
|
|
143
144
|
const insert = (table, ...colsVals) => {
|
|
144
145
|
const tableName = table[SYM_TABLE].name;
|
|
145
146
|
const columns = Object.keys(colsVals[0]).filter(c => colsVals[0][c] !== undefined);
|
|
@@ -147,14 +148,14 @@ const insert = (table, ...colsVals) => {
|
|
|
147
148
|
const colBinds = columns.map(c => identifier(c));
|
|
148
149
|
const [valStrings, valBinds] = colsVals.reduce(([ss, bs], row, rowIdx) => {
|
|
149
150
|
const rowStrings = columns.map((_, colIdx) => colIdx === 0 ? (rowIdx === 0 ? ') values (' : ') ,(') : ' ,');
|
|
150
|
-
return [[...ss, ...rowStrings], [...bs, ...columns.map(c => row[c])]];
|
|
151
|
+
return [[...ss, ...rowStrings], [...bs, ...columns.map(c => value(row[c]))]];
|
|
151
152
|
}, [[], []]);
|
|
152
153
|
return result(fragment(['insert into ', ...colStrings, ...valStrings, ')'], [identifier(tableName), ...colBinds, ...valBinds]));
|
|
153
154
|
};
|
|
154
155
|
const update = (table, colsVals) => {
|
|
155
156
|
const tableName = table[SYM_TABLE].name;
|
|
156
|
-
const entries = Object.entries(colsVals).filter(([,
|
|
157
|
-
const [strings, binds] = entries.reduce(([ss, bs], [colName,
|
|
157
|
+
const entries = Object.entries(colsVals).filter(([, v]) => v !== undefined);
|
|
158
|
+
const [strings, binds] = entries.reduce(([ss, bs], [colName, v], i) => [[...ss, i === 0 ? ' set ' : ' ,', ' = '], [...bs, identifier(colName), value(v)]], [['update '], [identifier(tableName)]]);
|
|
158
159
|
return result(fragment([...strings, ''], binds));
|
|
159
160
|
};
|
|
160
161
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vanit-co/sql-ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "A TypeScript SQL query builder using tagged template literals. Write plain SQL with safe, automatic parameter binding and properly quoted identifiers. No DSL to learn, no magic, no ORM.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "./dist/index.js",
|