From 1c347b896e61e4b3b6e6bce6212d3ae7f0f85d50 Mon Sep 17 00:00:00 2001 From: "E. Cooper" Date: Mon, 4 Nov 2024 14:21:52 -0800 Subject: [PATCH] Allow using a query and cursor together when calling client.feed() (#298) --- __tests__/integration/feed.test.ts | 40 ++++++++++++++++++++++++++++++ src/client.ts | 9 ------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/__tests__/integration/feed.test.ts b/__tests__/integration/feed.test.ts index 7426923b..70eb4422 100644 --- a/__tests__/integration/feed.test.ts +++ b/__tests__/integration/feed.test.ts @@ -136,6 +136,46 @@ describe("Client", () => { expect(pages).toHaveLength(3); expect(Array.from(pages[0].events)).toHaveLength(1); }); + + it("can resume from a cursor using a query", async () => { + // Query fragment to reuse in the feed + const query = fql`FeedTest.all().eventSource()`; + + // First document, which we don't want to include in the feed, but we'll + // use its transaction timestamp to resume from after we create our test + // documents. + const startAt = (await client.query(fql`FeedTest.create({ value: 1})`)) + .txn_ts; + + // Create a second batch of documents to include in the feed + await client.query( + fql`Set.sequence(0, 3).forEach(v => FeedTest.create({ value: v + 1}));`, + ); + + // Create a feed that will resume from the transaction timestamp of the + // first document we created above. + const feed = client.feed(query, { + ...defaultFeedConfig, + page_size: 1, + start_ts: startAt, + }); + + // Get the first page of events from the feed + const firstPage = await feed[Symbol.asyncIterator]().next(); + + // Create a second feed that will resume from the cursor of the first page + const feedWithCursor = client.feed(query, { + ...defaultFeedConfig, + cursor: firstPage.value.cursor, + }); + + // Get the second page of events from the feed + const pages = await fromAsync(feedWithCursor); + + // We should get a single page with 2 events in it + expect(pages).toHaveLength(1); + expect(Array.from(pages[0].events)).toHaveLength(2); + }); }); describe("FeedClient", () => { diff --git a/src/client.ts b/src/client.ts index a3644d9e..44ab3374 100644 --- a/src/client.ts +++ b/src/client.ts @@ -434,15 +434,6 @@ export class Client { ...options, }; - if ( - clientConfiguration.cursor !== undefined && - tokenOrQuery instanceof Query - ) { - throw new ClientError( - "The `cursor` configuration can only be used with a stream token.", - ); - } - const tokenOrGetToken = tokenOrQuery instanceof Query ? () => this.query(tokenOrQuery).then((res) => res.data)