chore: snapshot before performance optimization

This commit is contained in:
Sepehr Ramezani
2026-04-17 21:14:43 +02:00
parent b6a548acd8
commit 2eceb32fd4
95 changed files with 4357 additions and 1942 deletions

View File

@@ -34,18 +34,30 @@ export function deepEqual(a: unknown, b: unknown): boolean {
}
/**
* Parse a database note object into a typed Note
* Handles JSON string fields that are stored in the database
* Coerce a Prisma Json value into an array (or return fallback).
* Handles null, undefined, string (legacy JSON), object, etc.
*/
export function asArray<T = unknown>(val: unknown, fallback: T[] = []): T[] {
if (Array.isArray(val)) return val
if (typeof val === 'string') {
try { const p = JSON.parse(val); return Array.isArray(p) ? p : fallback } catch { return fallback }
}
return fallback
}
/**
* Parse a database note object into a typed Note.
* Guarantees array fields are always real arrays or null.
*/
export function parseNote(dbNote: any): Note {
return {
...dbNote,
checkItems: dbNote.checkItems ? JSON.parse(dbNote.checkItems) : null,
labels: dbNote.labels ? JSON.parse(dbNote.labels) : null,
images: dbNote.images ? JSON.parse(dbNote.images) : null,
links: dbNote.links ? JSON.parse(dbNote.links) : null,
embedding: dbNote.embedding ? JSON.parse(dbNote.embedding) : null,
sharedWith: dbNote.sharedWith ? JSON.parse(dbNote.sharedWith) : [],
checkItems: asArray(dbNote.checkItems, null as any) ?? null,
labels: asArray(dbNote.labels) || null,
images: asArray(dbNote.images) || null,
links: asArray(dbNote.links) || null,
embedding: asArray<number>(dbNote.embedding) || null,
sharedWith: asArray(dbNote.sharedWith),
size: dbNote.size || 'small',
}
}