diff --git a/dist/lib/sqlite/db-insert.d.ts b/dist/lib/sqlite/db-insert.d.ts index 2a74b11..1e626cb 100644 --- a/dist/lib/sqlite/db-insert.d.ts +++ b/dist/lib/sqlite/db-insert.d.ts @@ -7,11 +7,16 @@ type Params({ table, data, update_on_duplicate, config, }: Params): Promise; +}, Table extends string = string>({ table, data, update_on_duplicate, returning, config, }: Params): Promise>; export {}; diff --git a/dist/lib/sqlite/db-insert.js b/dist/lib/sqlite/db-insert.js index 8237dcc..e1ca892 100644 --- a/dist/lib/sqlite/db-insert.js +++ b/dist/lib/sqlite/db-insert.js @@ -1,8 +1,9 @@ import dbHandler from "../db-handler"; import sqlInsertGenerator from "../../utils/sql-insert-generator"; import grabDuplicateSafeInsertSql from "../grab-duplicate-safe-insert-sql"; -export default async function DbInsert({ table, data, update_on_duplicate, config, }) { +export default async function DbInsert({ table, data, update_on_duplicate, returning = true, config, }) { let sqlObj = null; + const useReturning = returning; try { const finalData = data.map((d) => ({ created_at: Date.now(), @@ -15,9 +16,19 @@ export default async function DbInsert({ table, data, update_on_duplicate, confi data: finalData, }) || null; let sql = sqlObj?.query || ""; + if (!sql) { + return { + success: false, + msg: "No insert data provided", + debug: { sqlObj }, + }; + } if (update_on_duplicate && data[0]) { sql = await grabDuplicateSafeInsertSql({ data, table, sql }); } + if (useReturning) { + sql += " RETURNING *"; + } (sqlObj || {}).query = sql; const res = dbHandler({ query: sql, @@ -27,9 +38,26 @@ export default async function DbInsert({ table, data, update_on_duplicate, confi if (!res.success) { return { ...res, debug: { sqlObj } }; } + if (!useReturning) { + return { + success: Boolean(res.postInsertReturn?.insertId), + postInsertReturn: res.postInsertReturn, + debug: { + sqlObj, + }, + }; + } + const rows = res.payload || []; return { - success: Boolean(res.postInsertReturn?.insertId), - postInsertReturn: res.postInsertReturn, + success: rows.length > 0, + payload: rows, + singleRes: rows[0] ?? null, + postInsertReturn: { + affectedRows: rows.length, + insertId: rows.length + ? Number(rows[rows.length - 1]?.id) + : undefined, + }, debug: { sqlObj, }, diff --git a/dist/lib/sqlite/db-update.d.ts b/dist/lib/sqlite/db-update.d.ts index a68c2ab..2e41c9e 100644 --- a/dist/lib/sqlite/db-update.d.ts +++ b/dist/lib/sqlite/db-update.d.ts @@ -8,11 +8,17 @@ type Params; targetId?: number | string; + /** + * Append `RETURNING *` so the updated record(s) come back in + * `payload`/`singleRes`. Defaults to `true`. Ignored for vector tables, + * where SQLite does not support `UPDATE ... RETURNING`. + */ + returning?: boolean; config?: BunSQLiteConfig; }; export default function DbUpdate({ table, data, query, targetId, config, }: Params): Promise; +}, Table extends string = string>({ table, data, query, targetId, returning, config, }: Params): Promise>; export {}; diff --git a/dist/lib/sqlite/db-update.js b/dist/lib/sqlite/db-update.js index c4b5e29..d1ad8c6 100644 --- a/dist/lib/sqlite/db-update.js +++ b/dist/lib/sqlite/db-update.js @@ -1,15 +1,20 @@ import dbHandler from "../db-handler"; import _ from "lodash"; +import init from "../../functions/init"; import sqlGenerator from "../../utils/sql-generator"; import { sqlValue } from "../../utils/sql-insert-generator"; import { sqlIdent } from "../../utils/sql-safe"; -export default async function DbUpdate({ table, data, query, targetId, config, }) { +export default async function DbUpdate({ table, data, query, targetId, returning = true, config, }) { let sqlObj = { string: "", values: [], }; try { const finalQuery = query || {}; + const { dbSchema } = init(); + const isVectorTable = dbSchema.tables.find((t) => t.tableName === table)?.isVector === + true; + const useReturning = returning && !isVectorTable; const sqlQueryObj = sqlGenerator({ tableName: table, genObject: _.pick(finalQuery, ["query", "searchOperator"]), @@ -36,6 +41,9 @@ export default async function DbUpdate({ table, data, query, targetId, config, } } } sql += ` ${whereClause}`; + if (useReturning) { + sql += " RETURNING *"; + } values = [...values, ...sqlQueryObj.where.values]; sqlObj.string = sql; sqlObj.values = values; @@ -43,9 +51,26 @@ export default async function DbUpdate({ table, data, query, targetId, config, } if (!res.success) { return { ...res, debug: { sqlObj } }; } + if (!useReturning) { + return { + success: Boolean(res.postInsertReturn?.affectedRows), + postInsertReturn: res.postInsertReturn, + debug: { + sqlObj, + }, + }; + } + const rows = res.payload || []; return { - success: Boolean(res.postInsertReturn?.affectedRows), - postInsertReturn: res.postInsertReturn, + success: rows.length > 0, + payload: rows, + singleRes: rows[0] ?? null, + postInsertReturn: { + affectedRows: rows.length, + insertId: rows.length + ? Number(rows[rows.length - 1]?.id) + : undefined, + }, debug: { sqlObj, }, diff --git a/package.json b/package.json index 846b800..2002195 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/bun-sqlite", - "version": "1.2.0", + "version": "1.2.1", "description": "Schema-driven SQLite manager for Bun", "author": "Benjamin Toby", "type": "module",