Handle pushed down predicates in Electric collection #618
Mergedkevin-dp(GitHub profile, opens in a new tab)merged 5 commits into
query-driven-sync fromrefs/pull/618/headmerged by samwillis(GitHub profile, opens in a new tab) - @@ -0,0 +65,21 @@65+ }66+ return exp.path[0]!67+ case `func`:68+ return compileFunction(exp, params)69+ default:70+ throw new Error(`Unknown expression type`)71+ }72+}73+74+function compileOrderBy(orderBy: IR.OrderBy, params: Array<unknown>): string {75+ const compiledOrderByClauses = orderBy.map((clause: IR.OrderByClause) =>76+ compileOrderByClause(clause, params)77+ )78+ return compiledOrderByClauses.join(`,`)79+}80+81+function compileOrderByClause(82+ clause: IR.OrderByClause,83+ params: Array<unknown>84+): string {85+ // TODO: what to do with stringSort and locale?@@ -439,0 +826,21 @@826+ .orderBy(({ user }) => user.age, `asc`)827+ .limit(3),828+ })829+830+ await new Promise((resolve) => setTimeout(resolve, 0))831+832+ // Should have requested snapshot for limit 3833+ expect(mockRequestSnapshot).toHaveBeenCalledWith(834+ expect.objectContaining({835+ limit: 3,836+ orderBy: `age NULLS FIRST`,837+ })838+ )839+840+ // After receiving Bob and Charlie, the collection now has 3 users (Alice + Bob + Charlie)841+ // but it still requests 2 more... TODO: check if this is correct?842+ expect(callArgs(1)).toMatchObject({843+ limit: 2,844+ orderBy: `age NULLS FIRST`,845+ })846+ })
- @@ -468,3 +468,14 @@468468 })469469 if (autoIndex === `eager`) {470470 it(`should load more data via requestSnapshot when creating live query with higher limit`, async () => {471+ // Create a new electric collection with on-demand syncMode for this test472+ vi.clearAllMocks()473+474+ const testSubscriber = vi.fn<(messages: Array<Message<User>>) => void>()475+ mockSubscribe.mockImplementation((callback) => {476+ testSubscriber.mockImplementation(callback)477+ return () => {}478+ })479+480+ const testElectricCollection = createCollection({481+ ...electricCollectionOptions({@@ -471,8 +483,18 @@483+ shapeOptions: {484+ url: `http://test-url`,485+ params: { table: `users` },486+ },487+ syncMode: `on-demand`,488+ getKey: (user: User) => user.id,489+ }),490+ startSync: true,491+ autoIndex: `eager` as const,492+ })493+471494 mockRequestSnapshot.mockResolvedValue({472495 data: [],473496 })474497475498 // Initial sync with limited data476- simulateInitialSync()477- expect(electricCollection.status).toBe(`ready`)478- expect(electricCollection.size).toBe(4)499+ testSubscriber([500+ ...sampleUsers.map((user) => ({@@ -468,3 +468,8 @@468468 })469469 if (autoIndex === `eager`) {470470 it(`should load more data via requestSnapshot when creating live query with higher limit`, async () => {471+ // Create a new electric collection with on-demand syncMode for this test472+ vi.clearAllMocks()473+474+ const testSubscriber = vi.fn<(messages: Array<Message<User>>) => void>()475+ mockSubscribe.mockImplementation((callback) => {@@ -596,0 +741,21 @@741+ value: sampleUsers[2]!, // Charlie742+ },743+ ],744+ })745+746+ // Create live query that needs more data747+ createLiveQueryCollection({748+ id: `progressive-live-query`,749+ startSync: true,750+ query: (q) =>751+ q752+ .from({ user: electricCollection })753+ .orderBy(({ user }) => user.id, `asc`)754+ .limit(3),755+ })756+757+ // Wait for the live query to process758+ await new Promise((resolve) => setTimeout(resolve, 0))759+760+ // Should have requested more data from Electric761+ expect(mockRequestSnapshot).toHaveBeenCalled()
- @@ -281,3 +303,5 @@281303 } {282304 const seenTxids = new Store<Set<Txid>>(new Set([]))283305 const seenSnapshots = new Store<Array<PostgresSnapshot>>([])306+ const syncMode = config.syncMode ?? `eager`307+ const finalSyncMode = syncMode === `progressive` ? `on-demand` : syncMode@@ -653,3 +681,8 @@653681654682 const stream = new ShapeStream({655683 ...shapeOptions,684+ // In on-demand mode, we only want to sync changes, so we set the log to `changes_only`685+ log: syncMode === `on-demand` ? `changes_only` : undefined,686+ // In on-demand mode, we only need the changes from the point of time the collection was created687+ // so we default to `now` when there is no saved offset.688+ offset: shapeOptions.offset
samwillis(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 80284c2b983261e11439b311f00d558f6c7f9ec1
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR