sqlstack 1.0.19 → 1.0.20
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/package.json +1 -1
- package/readme.md +61 -18
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -199,46 +199,89 @@ sqlstack adds a small set of macros that expand into standard SQL, so you keep f
|
|
|
199
199
|
#### `:insert(...)`
|
|
200
200
|
|
|
201
201
|
```sql
|
|
202
|
-
--
|
|
203
|
-
INSERT INTO
|
|
202
|
+
-- posts/createPost.sql
|
|
203
|
+
INSERT INTO posts :insert(id, author_id, title, body, status = 'published');
|
|
204
204
|
```
|
|
205
205
|
|
|
206
206
|
```ts
|
|
207
|
-
await repo.
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
207
|
+
await repo.createPost({
|
|
208
|
+
id: 'post_1',
|
|
209
|
+
author_id: 'user_1',
|
|
210
|
+
title: 'Hello world',
|
|
211
|
+
body: 'First post!',
|
|
212
|
+
// status: undefined → defaults to 'published' from SQL literal
|
|
212
213
|
});
|
|
213
214
|
```
|
|
214
215
|
|
|
215
216
|
```sql
|
|
216
217
|
-- expands to
|
|
217
|
-
INSERT INTO
|
|
218
|
-
VALUES (?, ?, ?, '
|
|
218
|
+
INSERT INTO posts (id, author_id, title, body, status)
|
|
219
|
+
VALUES (?, ?, ?, ?, 'published');
|
|
219
220
|
```
|
|
220
221
|
|
|
222
|
+
**Additional behaviors and rules**
|
|
223
|
+
|
|
224
|
+
- Columns whose values are `undefined` are skipped; at least one non-literal column must be defined or a literal must be present.
|
|
225
|
+
- Pure literal expressions (e.g. `created_at = now()`) are allowed and included even with no params.
|
|
226
|
+
- `null` values are allowed and are bound as `NULL`.
|
|
227
|
+
- Column order in the generated `INSERT` follows the order listed in `:insert(...)`, regardless of object key order.
|
|
228
|
+
- `:insert()` only works with **named parameters**; using positional args throws.
|
|
229
|
+
|
|
230
|
+
#### `:batch_insert(...)`
|
|
231
|
+
|
|
232
|
+
Use `:batch_insert(...)` to build an efficient multi-row `VALUES` clause from one or more array arguments (plus optional scalars):
|
|
233
|
+
|
|
234
|
+
```sql
|
|
235
|
+
-- comments/createComments.sql
|
|
236
|
+
INSERT INTO comments (post_id, author_id, body)
|
|
237
|
+
:batch_insert(post_id, comment.author_id, comment.body);
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
```ts
|
|
241
|
+
await repo.createComments({
|
|
242
|
+
post_id: 'post_1',
|
|
243
|
+
comment: [
|
|
244
|
+
{ author_id: 'user_1', body: 'Nice post!' },
|
|
245
|
+
{ author_id: 'user_2', body: 'Subscribed.' },
|
|
246
|
+
],
|
|
247
|
+
});
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
```sql
|
|
251
|
+
-- expands (MySQL/SQLite)
|
|
252
|
+
INSERT INTO comments (post_id, author_id, body)
|
|
253
|
+
VALUES (?, ?, ?), (?, ?, ?);
|
|
254
|
+
|
|
255
|
+
-- expands (Postgres)
|
|
256
|
+
INSERT INTO comments (post_id, author_id, body)
|
|
257
|
+
VALUES ($1, $2, $3), ($4, $5, $6);
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
- At least one argument must resolve to an array; all array arguments must have the same length and be non-empty.
|
|
261
|
+
- Arguments can be scalars (reused for every row) or dot-paths into nested objects/arrays (e.g. `comment.body`, `root.ids`).
|
|
262
|
+
- `:batch_insert()` only works with **named parameters**, not positional ones.
|
|
263
|
+
|
|
221
264
|
#### `:update(...)`
|
|
222
265
|
|
|
223
266
|
```sql
|
|
224
|
-
--
|
|
225
|
-
UPDATE
|
|
226
|
-
:update(updated_at = now(),
|
|
267
|
+
-- posts/updatePost.sql
|
|
268
|
+
UPDATE posts
|
|
269
|
+
:update(updated_at = now(), title, body)
|
|
227
270
|
WHERE id = :id;
|
|
228
271
|
```
|
|
229
272
|
|
|
230
273
|
```ts
|
|
231
|
-
await repo.
|
|
232
|
-
id:
|
|
233
|
-
|
|
234
|
-
//
|
|
274
|
+
await repo.updatePost({
|
|
275
|
+
id: 'post_1',
|
|
276
|
+
title: 'Updated title',
|
|
277
|
+
// body: undefined → not updated
|
|
235
278
|
});
|
|
236
279
|
```
|
|
237
280
|
|
|
238
281
|
```sql
|
|
239
282
|
-- expands to
|
|
240
|
-
UPDATE
|
|
241
|
-
SET updated_at = now(),
|
|
283
|
+
UPDATE posts
|
|
284
|
+
SET updated_at = now(), title = ?
|
|
242
285
|
WHERE id = ?;
|
|
243
286
|
```
|
|
244
287
|
|