@vanit-co/sql-ts 0.8.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 +24 -5
- 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
|
@@ -114,29 +114,48 @@ const alias = transformer(tableWithAlias, (s, c) => [
|
|
|
114
114
|
...prefixedColumn(s, c),
|
|
115
115
|
[' as ', identifier(`${c.prefix}_${c.name}`)]
|
|
116
116
|
]);
|
|
117
|
+
const dropUndefined = (strings, binds) => {
|
|
118
|
+
const outStrings = [];
|
|
119
|
+
const outBinds = [];
|
|
120
|
+
let carry = '';
|
|
121
|
+
strings.forEach((s, i) => {
|
|
122
|
+
if (i === strings.length - 1)
|
|
123
|
+
outStrings.push(carry + s);
|
|
124
|
+
else if (binds[i] === undefined)
|
|
125
|
+
carry = carry + s;
|
|
126
|
+
else {
|
|
127
|
+
outStrings.push(carry + s);
|
|
128
|
+
outBinds.push(binds[i]);
|
|
129
|
+
carry = '';
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
return [outStrings, outBinds];
|
|
133
|
+
};
|
|
117
134
|
const buildTag = (fn) => (strings, ...binds) => {
|
|
118
|
-
const [
|
|
135
|
+
const [ss, bs] = dropUndefined(strings, binds);
|
|
136
|
+
const [s, v] = ramda.transpose(zipLongest(ss, bs).flatMap(fn));
|
|
119
137
|
return result(fragment(s, v ?? []));
|
|
120
138
|
};
|
|
121
139
|
const sql = buildTag(pure);
|
|
122
140
|
const select = buildTag(prefix);
|
|
123
141
|
const selectAs = buildTag(alias);
|
|
124
142
|
const empty = sql ``;
|
|
143
|
+
const value = (v) => v && v[SYM_COLUMN] ? identifier(v[SYM_COLUMN].name) : v;
|
|
125
144
|
const insert = (table, ...colsVals) => {
|
|
126
145
|
const tableName = table[SYM_TABLE].name;
|
|
127
|
-
const columns = Object.keys(colsVals[0]);
|
|
146
|
+
const columns = Object.keys(colsVals[0]).filter(c => colsVals[0][c] !== undefined);
|
|
128
147
|
const colStrings = columns.map((_, i) => i === 0 ? ' (' : ' ,');
|
|
129
148
|
const colBinds = columns.map(c => identifier(c));
|
|
130
149
|
const [valStrings, valBinds] = colsVals.reduce(([ss, bs], row, rowIdx) => {
|
|
131
150
|
const rowStrings = columns.map((_, colIdx) => colIdx === 0 ? (rowIdx === 0 ? ') values (' : ') ,(') : ' ,');
|
|
132
|
-
return [[...ss, ...rowStrings], [...bs, ...columns.map(c => row[c]
|
|
151
|
+
return [[...ss, ...rowStrings], [...bs, ...columns.map(c => value(row[c]))]];
|
|
133
152
|
}, [[], []]);
|
|
134
153
|
return result(fragment(['insert into ', ...colStrings, ...valStrings, ')'], [identifier(tableName), ...colBinds, ...valBinds]));
|
|
135
154
|
};
|
|
136
155
|
const update = (table, colsVals) => {
|
|
137
156
|
const tableName = table[SYM_TABLE].name;
|
|
138
|
-
const entries = Object.entries(colsVals);
|
|
139
|
-
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)]]);
|
|
140
159
|
return result(fragment([...strings, ''], binds));
|
|
141
160
|
};
|
|
142
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",
|