From 11dbd77be6a130851d967b10338db6d5c26a7579 Mon Sep 17 00:00:00 2001 From: Mark Wylde Date: Tue, 24 Dec 2024 21:39:10 +0000 Subject: [PATCH] fix: empty object all results --- src/index.ts | 10 ++++++---- test/query.ts | 11 +++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index dfecf44..3a2de4a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -28,7 +28,7 @@ export type DoubleDb = { patch: (id: string, newDocument: Partial) => Promise; remove: (id: string) => Promise; read: (id: string) => Promise; - query: (queryObject: object) => Promise; + query: (queryObject?: object) => Promise; close: () => Promise; batchInsert: (documents: Document[]) => Promise; upsert: (id: string, document: Document) => Promise; @@ -327,9 +327,11 @@ async function createDoubleDb(dataDirectory: string): Promise { return db.del(id); } - async function query(queryObject: object): Promise { - if (!isObject(queryObject)) { - throw new Error('doubledb.query: queryObject must be an object'); + async function query(queryObject?: object): Promise { + if (!queryObject || Object.keys(queryObject).length === 0) { + const allIds = await getAllIds(); + const results = await Promise.all([...allIds].map(id => read(id))); + return results.filter((doc): doc is Document => doc !== undefined); } let resultIds = new Set(); diff --git a/test/query.ts b/test/query.ts index 3c29b98..b2e44e9 100644 --- a/test/query.ts +++ b/test/query.ts @@ -15,6 +15,17 @@ async function setupTestDb() { return db; } +test('empty query', async () => { + const db = await setupTestDb(); + await db.insert({ value: 'alpha' }); + await db.insert({ value: 'beta' }); + const result = await db.query({}); + assert.strictEqual(result.length, 2); + assert.strictEqual(result[0].value, 'alpha'); + assert.strictEqual(result[1].value, 'beta'); + await db.close(); +}); + test('$sw operator on string', async () => { const db = await setupTestDb(); await db.insert({ value: 'alpha' });