Skip to content

Commit

Permalink
fix: empty object all results
Browse files Browse the repository at this point in the history
  • Loading branch information
markwylde committed Dec 24, 2024
1 parent a55834d commit 11dbd77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type DoubleDb = {
patch: (id: string, newDocument: Partial<Document>) => Promise<Document>;
remove: (id: string) => Promise<void>;
read: (id: string) => Promise<Document | undefined>;
query: (queryObject: object) => Promise<Document[]>;
query: (queryObject?: object) => Promise<Document[]>;
close: () => Promise<void>;
batchInsert: (documents: Document[]) => Promise<Document[]>;
upsert: (id: string, document: Document) => Promise<Document>;
Expand Down Expand Up @@ -327,9 +327,11 @@ async function createDoubleDb(dataDirectory: string): Promise<DoubleDb> {
return db.del(id);
}

async function query(queryObject: object): Promise<Document[]> {
if (!isObject(queryObject)) {
throw new Error('doubledb.query: queryObject must be an object');
async function query(queryObject?: object): Promise<Document[]> {
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<string>();
Expand Down
11 changes: 11 additions & 0 deletions test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down

0 comments on commit 11dbd77

Please sign in to comment.