Add default returning record on db insert and update
This commit is contained in:
Vendored
+6
-1
@@ -7,11 +7,16 @@ type Params<Schema extends {
|
|||||||
table: Table;
|
table: Table;
|
||||||
data: Schema[];
|
data: Schema[];
|
||||||
update_on_duplicate?: boolean;
|
update_on_duplicate?: boolean;
|
||||||
|
/**
|
||||||
|
* Append `RETURNING *` so the inserted record(s) come back in
|
||||||
|
* `payload`/`singleRes`. Defaults to `true`.
|
||||||
|
*/
|
||||||
|
returning?: boolean;
|
||||||
config?: BunSQLiteConfig;
|
config?: BunSQLiteConfig;
|
||||||
};
|
};
|
||||||
export default function DbInsert<Schema extends {
|
export default function DbInsert<Schema extends {
|
||||||
[k: string]: any;
|
[k: string]: any;
|
||||||
} = {
|
} = {
|
||||||
[k: string]: any;
|
[k: string]: any;
|
||||||
}, Table extends string = string>({ table, data, update_on_duplicate, config, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
}, Table extends string = string>({ table, data, update_on_duplicate, returning, config, }: Params<Schema, Table>): Promise<APIResponseObject<Schema>>;
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
Vendored
+31
-3
@@ -1,8 +1,9 @@
|
|||||||
import dbHandler from "../db-handler";
|
import dbHandler from "../db-handler";
|
||||||
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
import sqlInsertGenerator from "../../utils/sql-insert-generator";
|
||||||
import grabDuplicateSafeInsertSql from "../grab-duplicate-safe-insert-sql";
|
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;
|
let sqlObj = null;
|
||||||
|
const useReturning = returning;
|
||||||
try {
|
try {
|
||||||
const finalData = data.map((d) => ({
|
const finalData = data.map((d) => ({
|
||||||
created_at: Date.now(),
|
created_at: Date.now(),
|
||||||
@@ -15,9 +16,19 @@ export default async function DbInsert({ table, data, update_on_duplicate, confi
|
|||||||
data: finalData,
|
data: finalData,
|
||||||
}) || null;
|
}) || null;
|
||||||
let sql = sqlObj?.query || "";
|
let sql = sqlObj?.query || "";
|
||||||
|
if (!sql) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
msg: "No insert data provided",
|
||||||
|
debug: { sqlObj },
|
||||||
|
};
|
||||||
|
}
|
||||||
if (update_on_duplicate && data[0]) {
|
if (update_on_duplicate && data[0]) {
|
||||||
sql = await grabDuplicateSafeInsertSql({ data, table, sql });
|
sql = await grabDuplicateSafeInsertSql({ data, table, sql });
|
||||||
}
|
}
|
||||||
|
if (useReturning) {
|
||||||
|
sql += " RETURNING *";
|
||||||
|
}
|
||||||
(sqlObj || {}).query = sql;
|
(sqlObj || {}).query = sql;
|
||||||
const res = dbHandler({
|
const res = dbHandler({
|
||||||
query: sql,
|
query: sql,
|
||||||
@@ -27,9 +38,26 @@ export default async function DbInsert({ table, data, update_on_duplicate, confi
|
|||||||
if (!res.success) {
|
if (!res.success) {
|
||||||
return { ...res, debug: { sqlObj } };
|
return { ...res, debug: { sqlObj } };
|
||||||
}
|
}
|
||||||
|
if (!useReturning) {
|
||||||
|
return {
|
||||||
|
success: Boolean(res.postInsertReturn?.insertId),
|
||||||
|
postInsertReturn: res.postInsertReturn,
|
||||||
|
debug: {
|
||||||
|
sqlObj,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const rows = res.payload || [];
|
||||||
return {
|
return {
|
||||||
success: Boolean(res.postInsertReturn?.insertId),
|
success: rows.length > 0,
|
||||||
postInsertReturn: res.postInsertReturn,
|
payload: rows,
|
||||||
|
singleRes: rows[0] ?? null,
|
||||||
|
postInsertReturn: {
|
||||||
|
affectedRows: rows.length,
|
||||||
|
insertId: rows.length
|
||||||
|
? Number(rows[rows.length - 1]?.id)
|
||||||
|
: undefined,
|
||||||
|
},
|
||||||
debug: {
|
debug: {
|
||||||
sqlObj,
|
sqlObj,
|
||||||
},
|
},
|
||||||
|
|||||||
Vendored
+7
-1
@@ -8,11 +8,17 @@ type Params<Schema extends {
|
|||||||
data: Schema;
|
data: Schema;
|
||||||
query?: ServerQueryParam<Schema>;
|
query?: ServerQueryParam<Schema>;
|
||||||
targetId?: number | string;
|
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;
|
config?: BunSQLiteConfig;
|
||||||
};
|
};
|
||||||
export default function DbUpdate<Schema extends {
|
export default function DbUpdate<Schema extends {
|
||||||
[k: string]: any;
|
[k: string]: any;
|
||||||
} = {
|
} = {
|
||||||
[k: string]: any;
|
[k: string]: any;
|
||||||
}, Table extends string = string>({ table, data, query, targetId, config, }: Params<Schema, Table>): Promise<APIResponseObject>;
|
}, Table extends string = string>({ table, data, query, targetId, returning, config, }: Params<Schema, Table>): Promise<APIResponseObject<Schema>>;
|
||||||
export {};
|
export {};
|
||||||
|
|||||||
Vendored
+28
-3
@@ -1,15 +1,20 @@
|
|||||||
import dbHandler from "../db-handler";
|
import dbHandler from "../db-handler";
|
||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
|
import init from "../../functions/init";
|
||||||
import sqlGenerator from "../../utils/sql-generator";
|
import sqlGenerator from "../../utils/sql-generator";
|
||||||
import { sqlValue } from "../../utils/sql-insert-generator";
|
import { sqlValue } from "../../utils/sql-insert-generator";
|
||||||
import { sqlIdent } from "../../utils/sql-safe";
|
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 = {
|
let sqlObj = {
|
||||||
string: "",
|
string: "",
|
||||||
values: [],
|
values: [],
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
const finalQuery = query || {};
|
const finalQuery = query || {};
|
||||||
|
const { dbSchema } = init();
|
||||||
|
const isVectorTable = dbSchema.tables.find((t) => t.tableName === table)?.isVector ===
|
||||||
|
true;
|
||||||
|
const useReturning = returning && !isVectorTable;
|
||||||
const sqlQueryObj = sqlGenerator({
|
const sqlQueryObj = sqlGenerator({
|
||||||
tableName: table,
|
tableName: table,
|
||||||
genObject: _.pick(finalQuery, ["query", "searchOperator"]),
|
genObject: _.pick(finalQuery, ["query", "searchOperator"]),
|
||||||
@@ -36,6 +41,9 @@ export default async function DbUpdate({ table, data, query, targetId, config, }
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
sql += ` ${whereClause}`;
|
sql += ` ${whereClause}`;
|
||||||
|
if (useReturning) {
|
||||||
|
sql += " RETURNING *";
|
||||||
|
}
|
||||||
values = [...values, ...sqlQueryObj.where.values];
|
values = [...values, ...sqlQueryObj.where.values];
|
||||||
sqlObj.string = sql;
|
sqlObj.string = sql;
|
||||||
sqlObj.values = values;
|
sqlObj.values = values;
|
||||||
@@ -43,9 +51,26 @@ export default async function DbUpdate({ table, data, query, targetId, config, }
|
|||||||
if (!res.success) {
|
if (!res.success) {
|
||||||
return { ...res, debug: { sqlObj } };
|
return { ...res, debug: { sqlObj } };
|
||||||
}
|
}
|
||||||
|
if (!useReturning) {
|
||||||
|
return {
|
||||||
|
success: Boolean(res.postInsertReturn?.affectedRows),
|
||||||
|
postInsertReturn: res.postInsertReturn,
|
||||||
|
debug: {
|
||||||
|
sqlObj,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const rows = res.payload || [];
|
||||||
return {
|
return {
|
||||||
success: Boolean(res.postInsertReturn?.affectedRows),
|
success: rows.length > 0,
|
||||||
postInsertReturn: res.postInsertReturn,
|
payload: rows,
|
||||||
|
singleRes: rows[0] ?? null,
|
||||||
|
postInsertReturn: {
|
||||||
|
affectedRows: rows.length,
|
||||||
|
insertId: rows.length
|
||||||
|
? Number(rows[rows.length - 1]?.id)
|
||||||
|
: undefined,
|
||||||
|
},
|
||||||
debug: {
|
debug: {
|
||||||
sqlObj,
|
sqlObj,
|
||||||
},
|
},
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@moduletrace/bun-sqlite",
|
"name": "@moduletrace/bun-sqlite",
|
||||||
"version": "1.2.0",
|
"version": "1.2.1",
|
||||||
"description": "Schema-driven SQLite manager for Bun",
|
"description": "Schema-driven SQLite manager for Bun",
|
||||||
"author": "Benjamin Toby",
|
"author": "Benjamin Toby",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
|
|||||||
Reference in New Issue
Block a user