Return inserted/updated records from insert and update handlers

Append RETURNING * to generated INSERT/UPDATE SQL so the affected
record(s) are returned in payload/singleRes. Vector tables skip RETURNING
on update because SQLite does not support UPDATE ... RETURNING on virtual
tables, falling back to postInsertReturn.

postInsertReturn (affectedRows/insertId) is still populated for backwards
compatibility. Adds a returning?: boolean opt-out (default true) and tests.
This commit is contained in:
2026-10-17 18:43:46 +01:00
parent 7c095f6f16
commit 695347944d
4 changed files with 317 additions and 23 deletions
+33 -15
View File
@@ -395,8 +395,8 @@ All methods return an `APIResponseObject<T>`:
```ts
{
success: boolean;
payload?: T[]; // array of rows (select)
singleRes?: T; // first row (select)
payload?: T[]; // array of rows (select; also insert/update)
singleRes?: T; // first row (select; also insert/update)
count?: number; // total count (when count: true)
postInsertReturn?: {
affectedRows?: number;
@@ -458,16 +458,22 @@ const res = await BunSQLite.select({
### Insert
```ts
BunSQLite.insert<T>({ table, data });
BunSQLite.insert<T>({ table, data, update_on_duplicate?, returning? });
```
| Parameter | Type | Description |
| --------- | -------- | ------------------------------ |
| `table` | `string` | Table name |
| `data` | `T[]` | Array of row objects to insert |
| Parameter | Type | Description |
| --------------------- | --------- | ---------------------------------------------------------------- |
| `table` | `string` | Table name |
| `data` | `T[]` | Array of row objects to insert |
| `update_on_duplicate` | `boolean` | Upsert via `ON CONFLICT ... DO UPDATE` |
| `returning` | `boolean` | Return the inserted record(s). Defaults to `true` |
`created_at` and `updated_at` are set automatically to `Date.now()`.
On success the inserted record(s) come back in `singleRes` (first row) and
`payload` (all rows, for multi-row inserts). `postInsertReturn` is still
populated for backwards compatibility.
**Example:**
```ts
@@ -479,38 +485,50 @@ const res = await BunSQLite.insert({
],
});
console.log(res.singleRes); // first inserted record (with its id)
console.log(res.payload); // every inserted record
console.log(res.postInsertReturn?.insertId); // last inserted row ID
```
Pass `returning: false` to skip `RETURNING` (e.g. when inserting very large
`BLOB`/vector values); the response then only contains `postInsertReturn`.
---
### Update
```ts
BunSQLite.update<T>({ table, data, query?, targetId? })
BunSQLite.update<T>({ table, data, query?, targetId?, returning? })
```
| Parameter | Type | Description |
| ---------- | --------------------- | --------------------------- |
| `table` | `string` | Table name |
| `data` | `Partial<T>` | Fields to update |
| `query` | `ServerQueryParam<T>` | WHERE clause conditions |
| `targetId` | `string \| number` | Shorthand to filter by `id` |
| Parameter | Type | Description |
| ---------- | --------------------- | -------------------------------------------------------- |
| `table` | `string` | Table name |
| `data` | `Partial<T>` | Fields to update |
| `query` | `ServerQueryParam<T>` | WHERE clause conditions |
| `targetId` | `string \| number` | Shorthand to filter by `id` |
| `returning`| `boolean` | Return the updated record(s). Defaults to `true` |
A WHERE clause is required. If no condition matches, `success` is `false`.
`updated_at` is set automatically to `Date.now()`.
On success the updated record(s) come back in `singleRes` (first row) and
`payload`. Vector tables fall back to `postInsertReturn` automatically, because
SQLite does not support `UPDATE ... RETURNING` on virtual tables.
**Examples:**
```ts
// Update by ID
await BunSQLite.update({
const res = await BunSQLite.update({
table: "users",
targetId: 1,
data: { first_name: "Alicia" },
});
console.log(res.singleRes); // the updated record
// Update with custom query
await BunSQLite.update({
table: "users",