Add tests, schema sync safeguards, and query/handler improvements
- Add bun test suite: SQL generator and value unit tests, plus MariaDB integration tests gated on BUN_MARIADB_TEST_DB - schema: add --dry-run and require confirmation (or --yes) before dropping tables/columns; block unapproved destructive statements - Always AND targetId with the query, regardless of searchOperator - Initialize lazily on first call; init throws instead of exiting - Generate IN (?, ?, ...) lists for IN / NOT IN - Share value binding between insert and update (booleans, embeddings, function values, "" -> NULL for non-text columns) - Keep Buffers and convert BigInt/Date without a JSON round-trip - DbSQL: report success for empty SELECT results - Run update() in a transaction with row locking - Key the client cache on the full connection config - Consolidate identifier quoting, drop dead code, merge column lookup - Read CLI version from package.json, pin @types/bun, chmod CLI on compile - Rebuild dist Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Vendored
+6
-15
@@ -1,27 +1,17 @@
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
function quoteIdentifier(identifier) {
|
||||
return `\`${identifier.replace(/`/g, "``")}\``;
|
||||
}
|
||||
import { quoteIdentifier } from "../../utils/sql-identifier";
|
||||
export default async function DbDelete({ table, query, targetId, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: Number(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
});
|
||||
const whereClause = sqlObj.string.match(/WHERE .*/)?.[0];
|
||||
const whereClause = sqlObj.where;
|
||||
if (!whereClause) {
|
||||
return {
|
||||
success: false,
|
||||
@@ -34,6 +24,7 @@ export default async function DbDelete({ table, query, targetId, config, }) {
|
||||
let sql = `DELETE FROM ${quoteIdentifier(table)} ${whereClause}`;
|
||||
sql += ` RETURNING *`;
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = sqlObj.whereValues;
|
||||
const res = await dbHandler({
|
||||
query: sql,
|
||||
values: sqlObj.values,
|
||||
|
||||
+7
-10
@@ -1,5 +1,6 @@
|
||||
import _ from "lodash";
|
||||
import generateTypeDefinition from "./db-generate-type-defs";
|
||||
import resolveTable from "../schema/resolve-table";
|
||||
export default function dbSchemaToType({ config, dbSchema, }) {
|
||||
let datasquirelSchema = dbSchema;
|
||||
if (!datasquirelSchema)
|
||||
@@ -14,16 +15,12 @@ export default function dbSchemaToType({ config, dbSchema, }) {
|
||||
const defNames = [];
|
||||
const schemas = dbTablesSchemas
|
||||
.map((table) => {
|
||||
let final_table = _.cloneDeep(table);
|
||||
if (final_table.parentTableName) {
|
||||
const parent_table = dbTablesSchemas.find((t) => t.tableName === final_table.parentTableName);
|
||||
if (parent_table) {
|
||||
final_table = _.merge(parent_table, {
|
||||
tableName: final_table.tableName,
|
||||
tableDescription: final_table.tableDescription,
|
||||
});
|
||||
}
|
||||
}
|
||||
const has_parent = Boolean(table.parentTableName &&
|
||||
dbTablesSchemas.find((t) => t.tableName === table.parentTableName));
|
||||
// Merge parent fields into a copy; never mutate the parent schema
|
||||
const final_table = has_parent
|
||||
? resolveTable(table, datasquirelSchema)
|
||||
: _.cloneDeep(table);
|
||||
const defObj = generateTypeDefinition({
|
||||
paradigm: "TypeScript",
|
||||
table: final_table,
|
||||
|
||||
Vendored
+5
-16
@@ -1,25 +1,13 @@
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
function quoteIdentifier(identifier) {
|
||||
return `\`${identifier.replace(/`/g, "``")}\``;
|
||||
}
|
||||
export default async function DbSelect({ table, query, count, targetId, config, }) {
|
||||
let sqlObj = null;
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: Number(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
sqlObj = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
});
|
||||
const res = await dbHandler({
|
||||
query: sqlObj.string,
|
||||
@@ -49,8 +37,9 @@ export default async function DbSelect({ table, query, count, targetId, config,
|
||||
};
|
||||
if (count) {
|
||||
const countSqlObject = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
count,
|
||||
});
|
||||
const countSql = `SELECT COUNT(*) AS count FROM (${countSqlObject.string}) AS c`;
|
||||
|
||||
Vendored
+3
-2
@@ -1,11 +1,12 @@
|
||||
import type { DBResponseObject, SQLInsertGenValueType } from "../../types";
|
||||
import type { BunMariaDBConfig, DBResponseObject, SQLInsertGenValueType } from "../../types";
|
||||
type Params = {
|
||||
sql: string;
|
||||
values?: SQLInsertGenValueType[];
|
||||
config?: BunMariaDBConfig;
|
||||
};
|
||||
export default function DbSQL<T extends {
|
||||
[k: string]: any;
|
||||
} = {
|
||||
[k: string]: any;
|
||||
}>({ sql, values }: Params): Promise<DBResponseObject<T>>;
|
||||
}>({ sql, values, config }: Params): Promise<DBResponseObject<T>>;
|
||||
export {};
|
||||
|
||||
Vendored
+3
-5
@@ -1,11 +1,12 @@
|
||||
import dbHandler from "../db-handler";
|
||||
export default async function DbSQL({ sql, values }) {
|
||||
export default async function DbSQL({ sql, values, config }) {
|
||||
try {
|
||||
const trimmedSql = sql.trim();
|
||||
const isSelect = trimmedSql.match(/^select/i);
|
||||
const res = await dbHandler({
|
||||
query: trimmedSql,
|
||||
values: values,
|
||||
config,
|
||||
});
|
||||
if (!res.success) {
|
||||
return {
|
||||
@@ -23,12 +24,9 @@ export default async function DbSQL({ sql, values }) {
|
||||
}
|
||||
const payload = isSelect ? (res.payload || []) : undefined;
|
||||
const single_res = isSelect ? payload?.[0] : res.single_res;
|
||||
const singleRaw = res.single_res;
|
||||
return {
|
||||
...res,
|
||||
success: isSelect
|
||||
? Boolean(single_res) || Boolean(payload?.[0])
|
||||
: true,
|
||||
success: true,
|
||||
payload,
|
||||
single_res,
|
||||
debug: {
|
||||
|
||||
Vendored
+92
-58
@@ -1,37 +1,38 @@
|
||||
import dbHandler from "../db-handler";
|
||||
import _ from "lodash";
|
||||
import dbHandler, { resolveClient } from "../db-handler";
|
||||
import sqlGenerator from "../../utils/sql-generator";
|
||||
import sanitizeHtmlFields from "../../utils/sanitize-html-fields";
|
||||
function quoteIdentifier(identifier) {
|
||||
return `\`${identifier.replace(/`/g, "``")}\``;
|
||||
import { quoteIdentifier } from "../../utils/sql-identifier";
|
||||
import sqlValuePlaceholder from "../../utils/sql-value-placeholder";
|
||||
import grabTableFields from "../../utils/grab-table-fields";
|
||||
/** Thrown inside the transaction to roll back and return a response */
|
||||
class UpdateAbort extends Error {
|
||||
response;
|
||||
constructor(response) {
|
||||
super(response.msg || response.error || "Update aborted");
|
||||
this.response = response;
|
||||
}
|
||||
}
|
||||
export default async function DbUpdate({ table, data, query, targetId, config, }) {
|
||||
let sqlObj = { string: "", values: [] };
|
||||
let sqlObj = {
|
||||
string: "",
|
||||
values: [],
|
||||
where: "",
|
||||
whereValues: [],
|
||||
};
|
||||
try {
|
||||
let finalQuery = query || {};
|
||||
if (targetId) {
|
||||
finalQuery = _.merge(finalQuery, {
|
||||
query: {
|
||||
id: {
|
||||
value: Number(targetId),
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
const finalQuery = query || {};
|
||||
const sqlQueryObj = sqlGenerator({
|
||||
tableName: quoteIdentifier(table),
|
||||
tableName: table,
|
||||
genObject: finalQuery,
|
||||
targetId,
|
||||
});
|
||||
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
|
||||
const whereClause = sqlQueryObj.where;
|
||||
if (!whereClause) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `No WHERE clause`,
|
||||
};
|
||||
}
|
||||
let values = [];
|
||||
let sql = ``;
|
||||
sql += `UPDATE ${quoteIdentifier(table)} SET`;
|
||||
const sanitizedData = sanitizeHtmlFields({
|
||||
table,
|
||||
data,
|
||||
@@ -41,48 +42,81 @@ export default async function DbUpdate({ table, data, query, targetId, config, }
|
||||
updated_at: Date.now(),
|
||||
...sanitizedData,
|
||||
};
|
||||
const keys = Object.keys(finalData);
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
const key = keys[i];
|
||||
if (!key)
|
||||
continue;
|
||||
const isLast = i == keys.length - 1;
|
||||
sql += ` ${quoteIdentifier(key)}=?`;
|
||||
values.push(finalData[key] ?? null);
|
||||
if (!isLast) {
|
||||
sql += `,`;
|
||||
const fieldsMap = new Map(grabTableFields(table).map((f) => [f.fieldName, f]));
|
||||
const setParts = [];
|
||||
const setValues = [];
|
||||
for (const key of Object.keys(finalData)) {
|
||||
const { placeholder, value } = sqlValuePlaceholder({
|
||||
value: finalData[key],
|
||||
field: fieldsMap.get(key),
|
||||
});
|
||||
setParts.push(`${quoteIdentifier(key)}=${placeholder}`);
|
||||
setValues.push(value);
|
||||
}
|
||||
const CLIENT = resolveClient(config);
|
||||
if (!CLIENT) {
|
||||
throw new Error(`Couldn't grab MariaDB Client.`);
|
||||
}
|
||||
/**
|
||||
* Lock the matching rows, update them by id, and re-fetch them by id,
|
||||
* all in one transaction. Re-fetching by id still works when the update
|
||||
* changes a column used in the WHERE clause.
|
||||
*/
|
||||
return await CLIENT.begin(async (tx) => {
|
||||
const matched_res = await dbHandler({
|
||||
query: `SELECT id FROM ${quoteIdentifier(table)} ${whereClause} FOR UPDATE`,
|
||||
values: sqlQueryObj.whereValues,
|
||||
client: tx,
|
||||
});
|
||||
if (!matched_res.success) {
|
||||
throw new UpdateAbort({
|
||||
...matched_res,
|
||||
success: false,
|
||||
debug: { sqlObj: sqlQueryObj },
|
||||
});
|
||||
}
|
||||
}
|
||||
sql += ` ${whereClause}`;
|
||||
values = [...values, ...sqlQueryObj.values];
|
||||
const res = await dbHandler({
|
||||
query: sql,
|
||||
values: values,
|
||||
config,
|
||||
const matched_ids = (matched_res.payload || [])
|
||||
.map((row) => row.id)
|
||||
.filter((id) => id !== undefined && id !== null);
|
||||
if (matched_ids.length === 0) {
|
||||
throw new UpdateAbort({
|
||||
success: false,
|
||||
msg: "No rows matched the update query",
|
||||
payload: [],
|
||||
debug: { sqlObj: sqlQueryObj },
|
||||
});
|
||||
}
|
||||
const idPlaceholders = matched_ids.map(() => "?").join(",");
|
||||
const sql = `UPDATE ${quoteIdentifier(table)} SET ${setParts.join(", ")} WHERE id IN (${idPlaceholders})`;
|
||||
const values = [...setValues, ...matched_ids];
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = values;
|
||||
const res = await dbHandler({
|
||||
query: sql,
|
||||
values: values,
|
||||
client: tx,
|
||||
});
|
||||
if (!res.success) {
|
||||
throw new UpdateAbort({ ...res, debug: { sqlObj } });
|
||||
}
|
||||
const updated_res = await dbHandler({
|
||||
query: `SELECT * FROM ${quoteIdentifier(table)} WHERE id IN (${idPlaceholders})`,
|
||||
values: matched_ids,
|
||||
client: tx,
|
||||
});
|
||||
return {
|
||||
...updated_res,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
db_res: res.db_res,
|
||||
};
|
||||
});
|
||||
if (res.error) {
|
||||
return res;
|
||||
}
|
||||
sqlObj.string = sql;
|
||||
sqlObj.values = values;
|
||||
let updated_sql = ``;
|
||||
let updated_sql_values = [];
|
||||
updated_sql += `SELECT * FROM ${quoteIdentifier(table)} ${whereClause}`;
|
||||
updated_sql_values = [...sqlQueryObj.values];
|
||||
const updated_res = await dbHandler({
|
||||
query: updated_sql,
|
||||
values: updated_sql_values,
|
||||
config,
|
||||
});
|
||||
return {
|
||||
...updated_res,
|
||||
debug: {
|
||||
sqlObj,
|
||||
},
|
||||
db_res: res.db_res,
|
||||
};
|
||||
}
|
||||
catch (error) {
|
||||
if (error instanceof UpdateAbort) {
|
||||
return error.response;
|
||||
}
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
|
||||
Reference in New Issue
Block a user