From f9524ac61bbf9c2621620ba3f853849a37364c41 Mon Sep 17 00:00:00 2001 From: Simon Gagnon Date: Tue, 23 Feb 2021 17:08:47 -0500 Subject: [PATCH] Use createMany in Seed database example (#1776) * Replace example with createMany * change comments + add an example for all databases Co-authored-by: David Price --- .../template/api/db/seed.js | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/packages/create-redwood-app/template/api/db/seed.js b/packages/create-redwood-app/template/api/db/seed.js index bec5d1021bb8..c4c78f65c235 100644 --- a/packages/create-redwood-app/template/api/db/seed.js +++ b/packages/create-redwood-app/template/api/db/seed.js @@ -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: "alice@example.com" }, + { email: "mark@example.com" }, + { email: "jackie@example.com" }, + { email: "bob@example.com" }, + ], + 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: 'admin@email.com' }}) - // if (!existing.length) { - // await db.user.create({ data: { name: 'Admin', email: 'admin@email.com' }}) - // } + // Example without createMany (supported by all databases): + /* + const existing = await db.user.findMany({ where: { email: 'admin@email.com' }}) + if (!existing.length) { + await db.user.create({ data: { name: 'Admin', email: 'admin@email.com' }}) + } + */ console.info('No data to seed. See api/db/seed.js for info.') }