gwilliamnn
gwilliamnn2y ago

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
ballingt
ballingt2y ago
Yup you'll need a function, since you don't know your IDs until they've been inserted.
ian
ian2y ago
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 running
gwilliamnn
gwilliamnnOP2y ago
Nice! Could you tell me more example when I should use the internalMutation or actions? I'm kinda confuse with this new ways to do
ian
ian2y ago
You mean when seeding data or mutation vs. action in general? Or internal vs. non-internal (public)?
gwilliamnn
gwilliamnnOP2y ago
internalMutation in general like, I can't find this on documentation...
ian
ian2y ago
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 dashboard
ian
ian2y ago
ian
ian2y ago
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 ones
ian
ian12mo ago
For 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
ian
ian12mo ago
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...

Did you find this page helpful?