Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using a query and cursor together when calling client.feed() #298

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions __tests__/integration/feed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventSource>`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", () => {
Expand Down
9 changes: 0 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventSource>(tokenOrQuery).then((res) => res.data)
Expand Down
Loading