Skip to content

Commit

Permalink
feat: Add convenience toArray method to QueryResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
icidasset committed Feb 5, 2025
1 parent 032906d commit e991e75
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/crdt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ export class CRDT<T extends DocTypes> {
return {
snapshot: (sinceOpts) => this.#snapshot<T>(sinceOpts, { waitFor }),
subscribe: (callback) => this.#subscribe<T>(callback),
toArray: (sinceOpts) => Array.fromAsync(this.#snapshot<T>(sinceOpts, { waitFor })),

live(opts?: { since?: ClockHead } & ChangesOptions) {
return stream<T>({ ...opts, futureOnly: false }, { waitFor });
},
Expand Down
2 changes: 2 additions & 0 deletions src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ export class Index<K extends IndexKeyType, T extends DocTypes, R extends DocFrag
return {
snapshot: (sinceOpts) => this.#snapshot(qryOpts, sinceOpts, { waitFor }),
subscribe: (callback) => this.#subscribe(callback),
toArray: (sinceOpts) => Array.fromAsync(this.#snapshot(qryOpts, sinceOpts, { waitFor })),

live(opts?: { since?: ClockHead }) {
return stream(qryOpts, { futureOnly: false, since: opts?.since }, { waitFor });
},
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ export interface QueryResponse<T extends DocTypes> {
future(): ReadableStream<{ doc: DocWithId<T>; marker: QueryStreamMarker }>;
/** Convenience function to consume a future stream. */
subscribe(callback: (doc: DocWithId<T>) => void): () => void;
/** Convenience function to get a full snapshot. */
toArray(opts?: { since?: ClockHead } & ChangesOptions): Promise<DocWithId<T>[]>;
}

type EmitFn = (k: IndexKeyType, v?: DocFragment) => void;
Expand Down
17 changes: 17 additions & 0 deletions tests/fireproof/streaming-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ describe("Streaming API", () => {
expect((doc as DocType).name).toBe("doc-extra");
}

async function testToArray<T extends DocBase>(queryResponse: QueryResponse<T>, amountOfDocs: number) {
const arr = await queryResponse.toArray();
expect(arr.length).toBe(amountOfDocs);
}

//////////////
// ALL DOCS //
//////////////
Expand Down Expand Up @@ -164,6 +169,10 @@ describe("Streaming API", () => {
it("test `subscribe` method", async () => {
await testSubscribe(lr.allDocs<DocType>());
});

it("test `toArray` method", async () => {
await testToArray(lr.allDocs<DocType>(), AMOUNT_OF_DOCS);
});
});

///////////
Expand Down Expand Up @@ -198,6 +207,10 @@ describe("Streaming API", () => {
it("test `subscribe` method", async () => {
await testSubscribe(lr.query<string, DocType>("name"));
});

it("test `toArray` method", async () => {
await testToArray(lr.query<string, DocType>("name"), AMOUNT_OF_DOCS);
});
});

// ADDITIONAL
Expand Down Expand Up @@ -239,6 +252,10 @@ describe("Streaming API", () => {
it("test `subscribe` method", async () => {
await testSubscribe(lr.query<string, DocType>("name"));
});

it("test `toArray` method", async () => {
await testToArray(lr.query<string, DocType>("additional"), AMOUNT_OF_ADDITIONAL_DOCS);
});
});
});
});

0 comments on commit e991e75

Please sign in to comment.