diff --git a/src/db/file/pushes.ts b/src/db/file/pushes.ts index 006a8be9b..3dfe956c7 100644 --- a/src/db/file/pushes.ts +++ b/src/db/file/pushes.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import _ from 'lodash'; import Datastore from '@seald-io/nedb'; import { Action } from '../../proxy/actions/Action'; import { toClass } from '../helper'; @@ -49,6 +48,16 @@ const defaultPushQuery: Partial = { type: 'push', }; +// Matches the JSON round trip in toClass: Date values become their toJSON() strings +const datesToJSON = (value: T): T => { + if (value instanceof Date) return value.toJSON() as T; + if (value && typeof value === 'object') { + const obj = value as Record; + for (const key of Object.keys(obj)) obj[key] = datesToJSON(obj[key]); + } + return value; +}; + export const getPushes = (query: Partial): Promise => { if (!query) query = defaultPushQuery; return new Promise((resolve, reject) => { @@ -60,11 +69,8 @@ export const getPushes = (query: Partial): Promise => { if (err) { reject(err); } else { - resolve( - _.chain(docs) - .map((x) => toClass(x, Action.prototype)) - .value(), - ); + // neDB's find() already deep-copies docs except Date values, so only those need detaching + resolve(docs.map((x) => Object.setPrototypeOf(datesToJSON(x), Action.prototype))); } }); }); diff --git a/test/db/file/pushes.test.ts b/test/db/file/pushes.test.ts index 1d1f215a3..58c6a0f53 100644 --- a/test/db/file/pushes.test.ts +++ b/test/db/file/pushes.test.ts @@ -98,4 +98,22 @@ describe('File DB - Pushes', () => { expect(pushesModule.db.findOne).toHaveBeenCalledWith({ id: pushId }, expect.any(Function)); }); }); + describe('getPushes', () => { + it('should return dates as strings detached from the stored document', async () => { + const id = 'test-push-dates'; + const timestamp = new Date('2026-01-02T03:04:05.000Z'); + await pushesModule.db.insertAsync({ id, type: 'push', rejection: { timestamp } }); + + try { + const [push] = (await pushesModule.getPushes({ id } as any)) as any[]; + expect(push.rejection.timestamp).toBe('2026-01-02T03:04:05.000Z'); + + push.rejection.timestamp = 'changed'; + const stored = await pushesModule.db.findOneAsync({ id }); + expect(stored.rejection.timestamp.toISOString()).toBe('2026-01-02T03:04:05.000Z'); + } finally { + await pushesModule.db.removeAsync({ id }, {}); + } + }); + }); });