-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use createMany in Seed database example (#1776)
* Replace example with createMany * change comments + add an example for all databases Co-authored-by: David Price <[email protected]>
- Loading branch information
1 parent
6d604c1
commit f9524ac
Showing
1 changed file
with
23 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.') | ||
} | ||
|