GhostBob
GhostBob4mo ago

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?
No description
No description
14 Replies
sshader
sshader4mo ago
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.
ian
ian4mo ago
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)
GhostBob
GhostBobOP4mo ago
@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?
GhostBob
GhostBobOP4mo ago
@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?
No description
No description
ballingt
ballingt4mo ago
@GhostBob your syntax isn't quite right here, it's
convex dev --run dbSeed:seed
# or
convex run dbSeed:seed
convex dev --run dbSeed:seed
# or
convex run dbSeed:seed
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?
GhostBob
GhostBobOP4mo ago
Could not find function for 'dbSeed:seed'. Did you forget to run npx convex dev or npx convex deploy?
ballingt
ballingt4mo ago
If you run npx convex dashboard and look at that dashboard, do you see those functions in the functions tab?
GhostBob
GhostBobOP4mo ago
No description
GhostBob
GhostBobOP4mo ago
from dashboard - everything works fine
GhostBob
GhostBobOP4mo ago
No description
ballingt
ballingt4mo ago
Is this code still accurate?
No description
GhostBob
GhostBobOP4mo ago
Worked!!! by running -> npx convex run dbSeed/seed:seed
ballingt
ballingt4mo ago
it looks like you want
npx convex run dbSeed/seed:default
npx convex run dbSeed/seed:default
like Ian said oh it a named export now? cool, glad it's working
GhostBob
GhostBobOP4mo ago
i tested different variations Thank you!!

Did you find this page helpful?