Example for seeding from a .js script

prisma

#1

Hey :slight_smile:

I can’t quite figure out how to seed the DB from a .js/.ts file instead of a seed.graphql file.

The docs state that one can provide a .js file to the seed prop in prisma.yml, but I can’t quite figure out how it’d work. Specifically, it’d be awesome if one could have access to the Prisma ctx.db and write mutations here with some fabulous bindings and auto completion :smiley:

Could someone perhaps help with an example of how this could be done with a sample mutation or two? Ideally, one would have an entry function in seed.ts like this:


function seedDatabase() {
    seedUsers()
    seedPosts()
    // ... etc
}

seedDatabase();

EDIT: If the example somehow could include a how-to on including graphql-faker in the seed, that’d be amazing

Thanks :slight_smile:


#2

Hey - I got mine to partially work and I have attached a pic. I removed some of the code otherwise it doesn’t necessarily have to be an async function

This runs successfully when I run node script.js in my terminal but when I run prisma seed I get this error message instead

Seeding based on yarn setup !
▸ cross_spawn_1.default is not a function

Maybe you can try it on your end and see if you have better luck


#3

@2cwe - But that starts up a new instance of Prisma, doesn’t it?


#4

yes but it’s linked to the same endpoint as the instance in the context that is passed to the server which in the end is the goal I guess.


#5

Getting the same error, @2wce

CLI output:

Seeding based on yarn ts-node ./database/seeding/seed.ts !
 ▸    cross_spawn_1.default is not a function

When using seed:run at least. If I use hooks:post-deploy to run the same script, the script gets called just fine. I think it might be a bug.

seed:
  # import: seed.graphql
  # run: yarn ts-node ./database/seeding/seed.ts

hooks:
  post-deploy:
    - graphql get-schema --project database
    - graphql codegen
    - yarn ts-node ./database/seeding/seed.ts

I would prefer not to use that approach, though, as it does not seem to be intended, and will not work with prisma seed

Friendly ping @nilan :smiley:

My seed.ts for reference:

const TAG = 'seed.ts';
console.log(TAG, `Hello from seed!\n`);

const seed = () => {
  console.log(TAG, `in function\n`);
};

seed();

Output from prisma version:
prisma/1.8.3 (darwin-x64) node-v10.2.1


#6
Seeding based on yarn ts-node ./database/seeding/seed.ts !
 ▸    cross_spawn_1.default is not a function

This is a bug. Can you please report it here: https://github.com/graphcool/prisma/? :slightly_smiling_face:


#7

This was fixed in the alpha channel here: https://github.com/prismagraphql/prisma/pull/2569 :slightly_smiling_face:

Thanks for bringing this up! :pray:


#8

Thanks, @nilan ! :slight_smile:

An example would still be great!


#9

Thanks I created an issue for different examples here: https://github.com/prismagraphql/prisma/issues/2618