Seed data with relations
Hy, I know has a way to seed with json file, but how can I do to seed using relation data?
Is possible just with json or I need create a function to seed using the api?
Thanks (sorry for too many questions)
10 Replies
Yup you'll need a function, since you don't know your IDs until they've been inserted.
The pattern I like is to make a
convex/init.ts
file like https://github.com/ianmacartney/streaming-chat-gpt/blob/main/convex/init.ts
And run it as part of dev
in package.json
scripts
:
https://github.com/ianmacartney/streaming-chat-gpt/blob/main/package.json
That way it can seed conditionally (I check if there's any document before writing) when it starts up, and means a new dev only needs to run npm run dev
to get seed data, backend, and frontend up and runningNice!
Could you tell me more example when I should use the internalMutation or actions?
I'm kinda confuse with this new ways to do
You mean when seeding data or mutation vs. action in general?
Or internal vs. non-internal (public)?
internalMutation in general
like, I can't find this on documentation...
internal mutations are your way of writing to the database, that aren't exposed as a public API. So clients won't be able to call them over the websocket, but you can call them from:
-
npx convex run
(CLI)
- npx convex dev --run
(CLI)
- from an action
or internalAction
- from the dashboardInternal Functions | Convex Developer Hub
Internal functions can only be called by other functions
So for things like seeding data, I wouldn't want (potentially malicious) clients to be able to call them, but I can call it from the CLI
Ooh also from the
scheduler
or via cron
jobs, forgot about those important onesFor anyone looking at this now, you should check out the sweet new snapshot import / export functionality to seed from prod data:
https://docs.convex.dev/database/import-export/export
Exporting Data Snapshots | Convex Developer Hub
Export your data out of Convex
I also wrote a post on seeding data for preview deployments: https://stack.convex.dev/seeding-data-for-preview-deployments
Seeding Data for Preview Deployments
Now that we've launched Preview Deployments on Convex, you can test out backend changes easier than ever. But you may want to seed your project with d...