Port bun-mariadb schema guard, targetId fix, lazy init and tests
- schema: add --dry-run (sync in a rolled-back transaction) and require confirmation (or --yes) before dropping tables or columns; unapproved destructive changes are refused - only rebuild tables whose CREATE TABLE statement changed - always AND targetId with the query, regardless of searchOperator - empty IN / NOT IN lists become 1 = 0 / 1 = 1 - initialize lazily on first call so importing never throws - read CLI version from package.json, chmod CLI on compile, pin @types/bun - add SQL generator, value and schema guard tests Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import sqlInsertGenerator, { sqlValue } from "../src/utils/sql-insert-generator";
|
||||
|
||||
describe("sqlValue", () => {
|
||||
test("booleans become 0 / 1", () => {
|
||||
expect(sqlValue(false)).toBe(0);
|
||||
expect(sqlValue(true)).toBe(1);
|
||||
});
|
||||
|
||||
test("0 and empty strings are kept", () => {
|
||||
expect(sqlValue(0)).toBe(0);
|
||||
expect(sqlValue("")).toBe("");
|
||||
});
|
||||
|
||||
test("undefined becomes NULL", () => {
|
||||
expect(sqlValue(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("sqlInsertGenerator", () => {
|
||||
test("builds a multi-row insert", () => {
|
||||
const res = sqlInsertGenerator({
|
||||
tableName: "items",
|
||||
data: [
|
||||
{ name: "a", active: false, qty: 0 },
|
||||
{ name: "b", note: "" },
|
||||
],
|
||||
});
|
||||
expect(res?.query).toBe(
|
||||
"INSERT INTO items (name,active,qty,note) VALUES (?,?,?,?),(?,?,?,?)",
|
||||
);
|
||||
expect(res?.values).toEqual(["a", 0, 0, null, "b", null, null, ""]);
|
||||
});
|
||||
|
||||
test("function values are called once", () => {
|
||||
let calls = 0;
|
||||
const res = sqlInsertGenerator({
|
||||
tableName: "items",
|
||||
data: [
|
||||
{
|
||||
emb: () => {
|
||||
calls++;
|
||||
return { value: "[1,2]", placeholder: "vec_f32(?)" };
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(calls).toBe(1);
|
||||
expect(res?.query).toBe("INSERT INTO items (emb) VALUES (vec_f32(?))");
|
||||
expect(res?.values).toEqual(["[1,2]"]);
|
||||
});
|
||||
|
||||
test("rejects injected column names", () => {
|
||||
expect(() =>
|
||||
sqlInsertGenerator({
|
||||
tableName: "items",
|
||||
data: [{ "name) VALUES (1); --": "x" }],
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user