-
Notifications
You must be signed in to change notification settings - Fork 236
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
Live Query API #104
Merged
Merged
Live Query API #104
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b8668a1
to
5c9ec28
Compare
7165eb8
to
d720c48
Compare
5c9ec28
to
c0f5766
Compare
1f0f7e0
to
a678da0
Compare
Merged
db0bd3e
to
0523b6d
Compare
any updates for this PR ? |
a678da0
to
9217de5
Compare
9217de5
to
671e446
Compare
2ed0f7e
to
2b636ea
Compare
thruflo
approved these changes
Jul 24, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great to me 👍
2b636ea
to
6ea99b2
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This added a new "Live Query" plugin (our first one).
To use the extension it needs adding to the PGlite instance when creating it:
There are three methods on the
live
namespace:live.query()
for basic live queries. Less machinery in PG so quicker for small results sets and narrow rows.live.incrementalQuery()
for incremental queries. It materialises the full result set on each update in js. Perfect for feeding into Reactlive.changes()
a lower level api that emits the changes (insert/update/delete) that can then be mapped to mutation in a UI or other datastore.query()
This is very similar to a standard query, but takes an additional callback that receives the results whenever they change:
The returned value from the call is an object with this interface:
pglite/packages/pglite/src/live/interface.ts
Lines 49 to 53 in a678da0
initialResults
is the initial results set (also sent to the callbackunsubscribe
allow you to unsubscribe from the live queryrefresh
allows you to force a refresh of the queryInternally it watches for the tables that the query depends on, and reruns the query whenever they are changes.
incrementalQuery()
Similar to above, but maintains a temp table inside of Postgres of the previous state. When the tables it depends on change the query is re-run and diffed with the last state. Only the changes from the last version of the query are copied from WASM into JS.
It requires an additional
key
argument, the name of a column (often a PK) to key the diff on.The returned value is of the same type as the
query
method above.live.changes()
A lower level API that is the backend for the
incrementalQuery
, it emits the change that have happed. It again requires akey
to key the diff on:the returned value from the call is defined by this interface:
pglite/packages/pglite/src/live/interface.ts
Lines 55 to 60 in a678da0
The results passed to the callback are array of
Change
objects:pglite/packages/pglite/src/live/interface.ts
Lines 62 to 80 in a678da0
Each change has it's new values as part of the object along with:
__changed_columns__
the columns names that were changes__op__
the operation that is required to update the state (INSERT, UPDATE, DELETE)__after__
thekey
of the row that this row should be after, it will be included in__changed_columns__
if it has been changed.To do: