diff --git a/src/bin.ts b/src/bin.ts index c4e0fe024..135f43813 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -196,7 +196,6 @@ if (process.env['NODE_ENV'] !== 'production') { observer.onReadStart = () => { prevEndpoints = JSON.stringify(Object.keys(db.data).sort()) } - observer.onReadEnd = (data) => { if (data === null) { return diff --git a/src/service.ts b/src/service.ts index bcf4c1184..82ab4d86c 100644 --- a/src/service.ts +++ b/src/service.ts @@ -113,31 +113,31 @@ function randomId(): string { return randomBytes(2).toString('hex') } -function ensureItemsHaveIds(items: Item[]): Item[] { - return items.map((item) => { +function fixItemsIds(items: Item[]) { + items.forEach((item) => { + if (typeof item['id'] === 'number') { + item['id'] = item['id'].toString() + } if (item['id'] === undefined) { - return { ...item, id: randomId() } + item['id'] = randomId() } - return item }) } // Ensure all items have an id -function ensureAllItemsHaveIds(data: Data): Data { - return Object.entries(data).reduce( - (acc, [key, value]) => ({ - ...acc, - [key]: Array.isArray(value) ? ensureItemsHaveIds(value) : value, - }), - {}, - ) +function fixAllItemsIds(data: Data) { + Object.values(data).forEach((value) => { + if (Array.isArray(value)) { + fixItemsIds(value) + } + }) } export class Service { #db: Low constructor(db: Low) { - db.data = ensureAllItemsHaveIds(db.data) + fixAllItemsIds(db.data) this.#db = db }