Seeding DB from command line...
The goal is to seed the database (containing a table called "users") with a pre-defined global admin user. Email and password are taken from environment variables.
On the local machine, I have a .env.local file. The content of this file is also added to convex project settings in the cloud.
Created "seed" internalAction which uses node environment.
This seed action is calling internalQuery "get" which checks if the user exists in the db and if not, I'm calling "add" internalMutation.
How should I call my internalAction to seed the db?
Calling in the terminal following commands result in zero data in database:
npx convex run seed
npx convex run dbSeed:seed
npx convex run dbSeed.seed
How do I call seed internalAction from cli?
14 Replies
The way you're calling functions sounds right -- I'd add log lines in all your functions to get more information on what's happening (e.g.
console.log("users", users)
in your action).
One thing that might be an issue -- generally when you want to access env variables in Convex functions, they're accessed via process.env
and defined on your Convex deployments (either via npx convex env set
or in the dashboard under Settings > Environment Variables), and aren't defined in .env
or .env.local
files. I'd double check with some logging that you're getting the values for the env variables in your functions.I think you need to call “dbSeed/seed:seed”
Or since it’s a default export: “dbSeed/seed”
The docs have some notes on this naming convention (folder/file:fn)
@sshader Environment variables are ok. I can run the function from the Dashboard and it adds new user to the database - no problems at all. The question is - is it possible to do not from the Dashboard, but from terminal in VS code?
@ian my package.json looks like this: see image attached. By running the command: "npm run seed" - the seed internalAction is uploaded to the cloud and that's it, nothing happens - see another image. Is it actually possible to call the action from VS code terminal?
@GhostBob your syntax isn't quite right here, it's
You should be seeing an error when you run these?
Try
npx convex run dbSeed:seed
in the VSCode terminal directly
What's the error you see wen you run these?Could not find function for 'dbSeed:seed'. Did you forget to run
npx convex dev
or npx convex deploy
?If you run
npx convex dashboard
and look at that dashboard, do you see those functions in the functions tab?from dashboard - everything works fine
Is this code still accurate?
Worked!!! by running -> npx convex run dbSeed/seed:seed
it looks like you want
like Ian said
oh it a named export now?
cool, glad it's working
i tested different variations
Thank you!!