Skip to content

Commit

Permalink
Use createMany in Seed database example (#1776)
Browse files Browse the repository at this point in the history
* Replace example with createMany

* change comments + add an example for all databases

Co-authored-by: David Price <[email protected]>
  • Loading branch information
Simon Gagnon and thedavidprice authored Feb 23, 2021
1 parent 6d604c1 commit f9524ac
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions packages/create-redwood-app/template/api/db/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,34 @@ const db = new PrismaClient()

async function main() {
// https://www.prisma.io/docs/guides/prisma-guides/seed-database
//
// Seed data is database data that needs to exist for your app to run.
// Ideally this file should be idempotent: running it multiple times
// will result in the same database state (usually by checking for the
// existence of a record before trying to create it). For example:
/*
const result = await db.user.createMany({
data: [
{ email: "[email protected]" },
{ email: "[email protected]" },
{ email: "[email protected]" },
{ email: "[email protected]" },
],
skipDuplicates: true, // Supported with Postgres database
})
console.log(`Created ${result.count} users!`)
*/
// Note: createMany creates multiple records in a transaction.
// To enable this feature, add createMany to previewFeatures in your schema.
// See: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#createmany-preview
// Note: createMany is not supported by SQLite.
//
// const existing = await db.user.findMany({ where: { email: '[email protected]' }})
// if (!existing.length) {
// await db.user.create({ data: { name: 'Admin', email: '[email protected]' }})
// }
// Example without createMany (supported by all databases):
/*
const existing = await db.user.findMany({ where: { email: '[email protected]' }})
if (!existing.length) {
await db.user.create({ data: { name: 'Admin', email: '[email protected]' }})
}
*/

console.info('No data to seed. See api/db/seed.js for info.')
}
Expand Down

0 comments on commit f9524ac

Please sign in to comment.