Refacor electron app and add migrations

This commit is contained in:
2026-03-04 01:38:43 +01:00
parent 4e95ae77c5
commit be91b6dfe8
70 changed files with 1824 additions and 923 deletions

View File

@@ -0,0 +1,32 @@
/** Converts a boolean-like value to SQLite's 0/1 integer representation. */
export const boolToInt = (val: unknown) => (val ? 1 : 0);
/** Serialises an object to a JSON string, or returns null if the value is null/undefined. */
export const jsonOrNull = (val: unknown) => (val != null ? JSON.stringify(val) : null);
/** A map of field names to transform functions that handle special serialisation. */
export type TransformMap = Partial<Record<string, (val: unknown) => unknown>>;
/**
* Applies a partial `updates` object onto an existing entity.
*
* - Fields absent from `updates` (undefined) are skipped entirely.
* - Fields listed in `transforms` are passed through their transform function first.
* - All other fields are written as-is, falling back to null when the value is null/undefined.
*/
export function applyUpdates<T extends object>(
entity: T,
updates: Partial<Record<string, unknown>>,
transforms: TransformMap = {}
): void {
const target = entity as unknown as Record<string, unknown>;
for (const [key, value] of Object.entries(updates)) {
if (value === undefined)
continue;
const transform = transforms[key];
target[key] = transform ? transform(value) : (value ?? null);
}
}