Skip to content

A local-first personal finance app

[AI] Allow null in SQL query parameter types via shared SqlParam type (#8493)

Matiss Janis Aboltins134e11b7058b
diff --git a/packages/loot-core/src/platform/server/sqlite/index.electron.ts b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
index bec7169b69..b038e4e51e 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.electron.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
@@ -6,8 +6,11 @@ import { getDataDir, readFile, removeFile } from '#platform/server/fs';
import { logger } from '#platform/server/log';
import { normalise } from './normalise';
+import type { SqlParam } from './types';
import { unicodeLike } from './unicodeLike';
+export type { SqlParam } from './types';
+
function verifyParamTypes(sql, arr) {
arr.forEach(val => {
if (typeof val !== 'string' && typeof val !== 'number' && val !== null) {
@@ -35,7 +38,7 @@ export function prepare(db, sql) {
export function runQuery(
db: SQL.Database,
sql: string | SQL.Statement,
- params: (string | number)[] = [],
+ params: SqlParam[] = [],
fetchAll = false,
) {
if (params) {
diff --git a/packages/loot-core/src/platform/server/sqlite/index.ts b/packages/loot-core/src/platform/server/sqlite/index.ts
index 64d2a4d154..950f3ddf53 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.ts
@@ -5,8 +5,11 @@ import type { Database, SqlJsStatic, Statement } from '@jlongster/sql.js';
import { logger } from '#platform/server/log';
import { normalise } from './normalise';
+import type { SqlParam } from './types';
import { unicodeLike } from './unicodeLike';
+export type { SqlParam } from './types';
+
// Types exported from sql.js (and Emscripten) are incomplete, so we need to redefine them here
type FSStream = (typeof FS)['FSStream'] & {
node: (typeof FS)['FSNode'] & {
@@ -70,10 +73,7 @@ export function _getModule() {
return SQL;
}
-function verifyParamTypes(
- sql: string | Statement,
- arr: (string | number)[] = [],
-) {
+function verifyParamTypes(sql: string | Statement, arr: SqlParam[] = []) {
arr.forEach(val => {
if (typeof val !== 'string' && typeof val !== 'number' && val !== null) {
throw new Error('Invalid field type ' + val + ' for sql ' + sql);
@@ -88,19 +88,19 @@ export function prepare(db: Database, sql: string) {
export function runQuery(
db: Database,
sql: string | Statement,
- params?: (string | number)[],
+ params?: SqlParam[],
fetchAll?: false,
): { changes: unknown };
export function runQuery<T>(
db: Database,
sql: string | Statement,
- params: (string | number)[],
+ params: SqlParam[],
fetchAll: true,
): T[];
export function runQuery<T>(
db: Database,
sql: string | Statement,
- params: (string | number)[] = [],
+ params: SqlParam[] = [],
fetchAll = false,
): T[] | { changes: unknown } {
if (params) {
diff --git a/packages/loot-core/src/platform/server/sqlite/types.ts b/packages/loot-core/src/platform/server/sqlite/types.ts
new file mode 100644
index 0000000000..601c6c5f17
--- /dev/null
+++ b/packages/loot-core/src/platform/server/sqlite/types.ts
@@ -0,0 +1,2 @@
+// Value types SQLite accepts as bound query parameters
+export type SqlParam = string | number | null;
diff --git a/packages/loot-core/src/server/db/index.test.ts b/packages/loot-core/src/server/db/index.test.ts
index 93b1ab6058..e8404e59ca 100644
--- a/packages/loot-core/src/server/db/index.test.ts
+++ b/packages/loot-core/src/server/db/index.test.ts
@@ -30,6 +30,22 @@ async function getTransactions(latestDate) {
// validated in the same event loop and it's same to not await)
describe('Database', () => {
+ test('all and first accept null params', async () => {
+ await db.insertAccount({ id: 'foo', name: 'bar' });
+
+ const rows = await db.all<{ id: string }>(
+ 'SELECT id FROM accounts WHERE official_name IS ?',
+ [null],
+ );
+ expect(rows).toEqual([{ id: 'foo' }]);
+
+ const row = await db.first<{ id: string }>(
+ 'SELECT id FROM accounts WHERE official_name IS ?',
+ [null],
+ );
+ expect(row).toEqual({ id: 'foo' });
+ });
+
test('inserting a category works', async () => {
await db.insertCategoryGroup({ id: 'group1', name: 'group1' });
await db.insertCategory({
diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts
index ae73a5083f..60caaf9dbb 100644
--- a/packages/loot-core/src/server/db/index.ts
+++ b/packages/loot-core/src/server/db/index.ts
@@ -116,19 +116,19 @@ export async function loadClock() {
// Functions
export function runQuery(
sql: string | Statement,
- params?: Array<string | number>,
+ params?: sqlite.SqlParam[],
fetchAll?: false,
): { changes: unknown };
export function runQuery<T>(
sql: string | Statement,
- params: Array<string | number> | undefined,
+ params: sqlite.SqlParam[] | undefined,
fetchAll: true,
): T[];
export function runQuery<T>(
sql: string | Statement,
- params: (string | number)[],
+ params: sqlite.SqlParam[],
fetchAll: boolean,
) {
if (fetchAll) {
@@ -171,18 +171,18 @@ export function asyncTransaction(fn: () => Promise<void>) {
// This function is marked as async because `runQuery` is no longer
// async. We return a promise here until we've audited all the code to
// make sure nothing calls `.then` on this.
-export async function all<T>(sql: string, params?: (string | number)[]) {
+export async function all<T>(sql: string, params?: sqlite.SqlParam[]) {
return runQuery<T>(sql, params, true);
}
-export async function first<T>(sql, params?: (string | number)[]) {
+export async function first<T>(sql, params?: sqlite.SqlParam[]) {
const arr = runQuery<T>(sql, params, true);
return arr.length === 0 ? null : arr[0];
}
// The underlying sql system is now sync, but we can't update `first` yet
// without auditing all uses of it
-export function firstSync<T>(sql, params?: (string | number)[]) {
+export function firstSync<T>(sql, params?: sqlite.SqlParam[]) {
const arr = runQuery<T>(sql, params, true);
return arr.length === 0 ? null : arr[0];
}
@@ -190,7 +190,7 @@ export function firstSync<T>(sql, params?: (string | number)[]) {
// This function is marked as async because `runQuery` is no longer
// async. We return a promise here until we've audited all the code to
// make sure nothing calls `.then` on this.
-export async function run(sql, params?: (string | number)[]) {
+export async function run(sql, params?: sqlite.SqlParam[]) {
return runQuery(sql, params);
}
diff --git a/upcoming-release-notes/allow-null-sql-query-params.md b/upcoming-release-notes/allow-null-sql-query-params.md
new file mode 100644
index 0000000000..f61be02247
--- /dev/null
+++ b/upcoming-release-notes/allow-null-sql-query-params.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+Allow `null` in database query parameter types via a shared `SqlParam` type