Prisma1: Example for different seeding workflows

Created on 11 Jun 2018  路  7Comments  路  Source: prisma/prisma1

Most helpful comment

We have it working with a setup like this:

seed.ts:

const db: Prisma = new Prisma({
  secret: process.env.PRISMA_SECRET,
  endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma API (value set in `.env`)
});

const seedConfig = {
  numberOfUsers: 200,
  ...
};

const setup = async () => {
  for (let index = 0; index < seedConfig.numberOfUsers; index++) {
    const user = await db.mutation.createUser(
      ...
    );
  }

  ... seed other models, too
}

// Remember to call the setup method in the end
setup();

prisma.yml:

seed:
  run: yarn ts-node ./database/seed/seed.ts

Hope it helps someone

All 7 comments

Hello

I'm starting a new project for automating the seeding on multiples db with _javascript_ and knex.js, please have a look at this and let me hear your recommends, thanks

here is a code example:

//creating and seeding process
ks.createAndSeed(userTableModel, 10).then(() => {
    //creating the table automatically & seeding ...
    ks.createAndSeed_close(roleTableModel, 10, (table) => { //closes the connection after process
        //creating the table with knex.js fn & and seeding ...
        table.increments(),
            table.string('name'),
            table.string('category'),
            table.timestamps(true, true)
    }).then(()) =>{
        //do something here...
  }
})

One code to seed them all!

Thanks for sharing @2rhop, can you elaborate how this is relevant for Prisma? 馃檪

with knex-seeder you can use its api for seeding multiples database, as its based on knex.js, supports many db conn, this is from de knex.js docs

The primary target environment for Knex is Node.js, you will need to install the knex library, and then install the appropriate database library: pg for PostgreSQL and Amazon Redshift, mysql for MySQL or MariaDB, sqlite3 for SQLite3, or mssql for MSSQL.

I guess that Prisma can uses the knex-seeder API for make some seeds into a db or generate some fake data for other purposes

We have it working with a setup like this:

seed.ts:

const db: Prisma = new Prisma({
  secret: process.env.PRISMA_SECRET,
  endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma API (value set in `.env`)
});

const seedConfig = {
  numberOfUsers: 200,
  ...
};

const setup = async () => {
  for (let index = 0; index < seedConfig.numberOfUsers; index++) {
    const user = await db.mutation.createUser(
      ...
    );
  }

  ... seed other models, too
}

// Remember to call the setup method in the end
setup();

prisma.yml:

seed:
  run: yarn ts-node ./database/seed/seed.ts

Hope it helps someone

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 10 days if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ragnorc picture ragnorc  路  3Comments

notrab picture notrab  路  3Comments

sorenbs picture sorenbs  路  3Comments

marktani picture marktani  路  3Comments

tbrannam picture tbrannam  路  3Comments