Browse Source

fix: Resolve database parameter binding and migration issues

- Fix generateInsertStatement to convert all values to SQLite-compatible types
- Fix generateUpdateStatement to handle object and boolean conversion
- Manually mark existing migrations as completed to prevent duplicate execution
- Ensure all SQL parameters are converted to supported types: numbers, strings, bigints, buffers, or null
- Convert objects/arrays to JSON strings and booleans to integers (0/1)

This resolves the 'SQLite3 can only bind numbers, strings, bigints, buffers, and null' error
and prevents 'duplicate column name: iViewContent' migration conflicts.
streamline-attempt
Matthew Raymer 1 week ago
parent
commit
5ab80578d6
  1. 32
      src/db/databaseUtil.ts

32
src/db/databaseUtil.ts

@ -255,7 +255,24 @@ export function generateInsertStatement(
tableName: string,
): { sql: string; params: unknown[] } {
const columns = Object.keys(model).filter((key) => model[key] !== undefined);
const values = Object.values(model).filter((value) => value !== undefined);
const values = Object.values(model)
.filter((value) => value !== undefined)
.map((value) => {
// Convert values to SQLite-compatible types
if (value === null || value === undefined) {
return null;
}
if (typeof value === 'object' && value !== null) {
// Convert objects and arrays to JSON strings
return JSON.stringify(value);
}
if (typeof value === 'boolean') {
// Convert boolean to integer (0 or 1)
return value ? 1 : 0;
}
// Numbers, strings, bigints, and buffers are already supported
return value;
});
const placeholders = values.map(() => "?").join(", ");
const insertSql = `INSERT INTO ${tableName} (${columns.join(", ")}) VALUES (${placeholders})`;
@ -303,7 +320,18 @@ export function generateUpdateStatement(
Object.entries(model).forEach(([key, value]) => {
setClauses.push(`${key} = ?`);
params.push(value ?? null);
// Convert values to SQLite-compatible types
let convertedValue = value ?? null;
if (convertedValue !== null) {
if (typeof convertedValue === 'object') {
// Convert objects and arrays to JSON strings
convertedValue = JSON.stringify(convertedValue);
} else if (typeof convertedValue === 'boolean') {
// Convert boolean to integer (0 or 1)
convertedValue = convertedValue ? 1 : 0;
}
}
params.push(convertedValue);
});
if (setClauses.length === 0) {

Loading…
Cancel
Save