Jayzus
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Ok I've got a first pass at the plugin. It works both in React and outside, it supports a list function and mutation functions, and all the offline first stuff works. It requires an
id
field in the table schema. And it requires that create
and update
have the same parameters as list
, or at least any fields that you might change need to be included in update
. Otherwise I think everything should just work. Please let me know if I missed anything or you find any issues. And I will gladly accept PRs :).
I'm making the plugin in a separate repo for now for easier testing, then I'll move it to the legend-state repo when it's ready.
The plugin: https://github.com/jmeistrich/convex-legendstate/blob/main/src/lib/convex.ts
Example usage: https://github.com/jmeistrich/convex-legendstate/blob/main/src/Chat/Chat.tsx
And some things I need help with @sujayakar :
1. How to do patch with a custom id field?
2. How to do changesSince with the token
3. Should we support a "get" function which would return a single result? Or should we always expect a list function that returns an array?
4. Can we use the crud helpers from "convex-helpers/server/crud" or do we need something different since we need the "id" field?
5. Should it always use onChange or have an option to run only once?
6. Docs: How to setup crud functions
7. Docs: How to setup your database with custom "id" function109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
I talked to @sujayakar earlier and we have a good plan. I'll have something for you to play with today.
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Types should be fixed in beta.5
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Sure, that'd be great! I'm free from 11am to 8pm in France time. Today or any other day, let me know 🙂
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
I think that would mean that it didn't infer the types from the query correctly. Can you share a full example?
I would like to make sure that the Query plugin is working in general but for convex it'll be much better to base on the crud plugin.
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Wow you've been busy! When you've got some time let's have a call and chat about the right way to go? I'm not in a rush, still have lots of work to do to go from beta to full release 🙂
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
That should be fine if the user creates crud queries for use in observables. And of course you could still use more complex queries outside of the observables.
But basically for Legend-State's model we need to be able to list records, update them in the observable, and then call functions to create/update with that data.
Does it seem like that's a reasonable constraint, at least for the part of the data that people want to use through observables in a local-first way? If not then Legend State may not be a good fit 😢. And then I think you'd need a more custom solution for doing local-first using Convex's mutations?
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Btw if you want to learn more about legend-state's sync there's a great new video: https://youtu.be/xkWvDG6uEfk
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
And for ids, Legend State can just operate on an "id" field instead of "_id" with no problem. The complexity (if any) would be more on the Convex side. How hard is it to make functions with their own "id" field work normally? Like how would a patch work - would they have to be given the custom "id" as args, do a lookup to get the real "_id" from it, and then patch with the real id?
But if we can just have some example code in our docs to follow some pattern to use a custom "id", and you're happy with that, then that's all good on our end 👍
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
So the way Legend-State normally works with database sync plugins is that you'd have an observable synced with a
list
function to get rows of items, and that fills out the observable with all of the items. Then if you modify a value in any one of the items it calls an update
function to update that item in the database. And if you add a new item to the object (with a new key) it calls a create
function to add it to the database.
That does kind of restrict you to having the list
/create
/update
functions all working with the same type of element. If list
functions return a document plus extra data, it is possible for the create
/update
function to pick out the fields to update that it cares about, but that would require some extra user code.
The way we normally handle this in our apps is that functions which return joined or embedded data just don't sync both directions. And that's totally fine 👍
Does that vibe with the way people use Convex?109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Keel is the one I was just referring to - it supports including id in the creation functions
Supabase also allows id in creation functions
Firebase has no concept of id anyway so we can just set it
Pocketbase (we don't have a plugin yet) supports id in create too
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
And then we use the same id creation library (ksuid) client side to match the server side id format
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
They ended up adding a feature to optionally include an id when creating
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
I tried that with a different database a while ago and it got very messy - all of the queries would need to be updated to take that new id field instead of the normal one, all of the features where you can find items by id don't work, etc... It's doable but creates a bunch of weird edge cases that make it not really work 😅
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Hmm, this bit "replaced with the true ID once the server assigns it" makes our observability and caching nearly impossible. On successful sync we'd need a complex migration to adjust old ids to new ids in the in-memory objects, the observable subscriptions, the queued pending changes, the persisted cache.
Is there any way we can create ids client-side in a way that they won't change after sync? If not I think we're at an impasse 😦
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Hmm, we may have a bit of a blocker. Because Legend-State usage is to set the state first and have the state sync itself automatically, and because it needs to work while offline, we need to be able to generate an id client side and push that to the server. So a mutation would look like this:
We set the new object into state which then internally triggers the mutation in the ConvexClient.
Is that possible somehow?
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
I see that _creationTime is set automatically - is there an easy way to set an _updateTime automatically when data is mutated? Legend State has a feature that uses the local cache to be able to sync only diffs since the previous sync. That relies on being able to query for "items updated after x".
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
That would be great! I'd like to get more up to speed on Convex too 🙂
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Oh lol nice 😂
109 replies
CCConvex Community
•Created by Jayzus on 9/27/2024 in #support-community
Legend-State plugin for local-first
Oh no! I saw you on the speaker list, so you're doing a remote talk?
109 replies