-
Notifications
You must be signed in to change notification settings - Fork 219
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
samwillis
force-pushed
the
samwillis/extension-api
branch
from
June 21, 2024 13:05
b8668a1
to
5c9ec28
Compare
samwillis
force-pushed
the
samwillis/live-queries
branch
from
June 21, 2024 13:06
7165eb8
to
d720c48
Compare
samwillis
force-pushed
the
samwillis/extension-api
branch
from
June 21, 2024 13:10
5c9ec28
to
c0f5766
Compare
samwillis
force-pushed
the
samwillis/live-queries
branch
4 times, most recently
from
June 21, 2024 20:54
1f0f7e0
to
a678da0
Compare
Merged
samwillis
force-pushed
the
samwillis/extension-api
branch
from
June 25, 2024 21:31
db0bd3e
to
0523b6d
Compare
any updates for this PR ? |
samwillis
force-pushed
the
samwillis/live-queries
branch
from
July 13, 2024 12:27
a678da0
to
9217de5
Compare
samwillis
force-pushed
the
samwillis/live-queries
branch
from
July 24, 2024 08:24
9217de5
to
671e446
Compare
samwillis
force-pushed
the
samwillis/live-queries
branch
from
July 24, 2024 09:27
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 👍
samwillis
force-pushed
the
samwillis/live-queries
branch
from
July 25, 2024 18:49
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: