diff --git a/src/db/databaseUtil.ts b/src/db/databaseUtil.ts index aca9933d..477b212f 100644 --- a/src/db/databaseUtil.ts +++ b/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) {