docs: Update seeding example to require adapter in Prisma ORM v7#7471
docs: Update seeding example to require adapter in Prisma ORM v7#7471kageyama0 wants to merge 3 commits intoprisma:mainfrom
Conversation
- Add adapter initialization in seed.ts example - Update dependencies to include @prisma/adapter-pg, pg, and dotenv - Add info callout explaining v7 requirement - Fixes PrismaClientInitializationError when running db seed in v7
WalkthroughDocumentation updated to demonstrate Prisma v7 adapter-based seeding: import Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (2)
150-158:⚠️ Potential issue | 🟡 MinorThe
Poolconnection is never closed — addpool.end()alongsideprisma.$disconnect().When using a manually-created
pg.Pool, the pool holds open connections even afterprisma.$disconnect(). For a seed script that should exit cleanly, the pool should also be torn down. Without this, the Node.js process may hang instead of exiting.Proposed fix
main() .then(async () => { await prisma.$disconnect() + await pool.end() }) .catch(async (e) => { console.error(e) await prisma.$disconnect() + await pool.end() process.exit(1) })
224-278:⚠️ Potential issue | 🟠 MajorThis JavaScript example also needs the v7 adapter treatment.
As noted above, this tab still uses
const prisma = new PrismaClient()on line 226 without passing an adapter. In Prisma v7, this will fail withPrismaClientInitializationError. The same adapter pattern (usingrequiresyntax) should be applied here, along with the corresponding info callout and updatednpm installcommand (currently missing from this tab as well — line 280 onwards doesn't list the adapter packages).
🤖 Fix all issues with AI agents
In `@content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx`:
- Around line 94-98: The JavaScript example still constructs PrismaClient
without an adapter (const prisma = new PrismaClient()), so update the JS tab to
mirror the new Prisma v7 callout: show initializing PrismaClient with an adapter
(or accelerateUrl) using CommonJS require syntax and demonstrate the PostgreSQL
adapter pattern (or add a short note that the same adapter/accelerateUrl setup
applies). Ensure references to PrismaClient, adapter, and accelerateUrl are
present in the JS example so the documented breaking change and fix are
reflected for CommonJS users.
- Around line 161-163: Update the dev dependency list to include the missing
TypeScript types for pg by adding "@types/pg" to the npm install command (so the
line with "npm install -D typescript tsx `@types/node` `@prisma/adapter-pg` pg
dotenv" becomes "... pg dotenv `@types/pg`" or equivalent); this ensures
TypeScript can resolve the "pg" module when the seed script imports "Pool" from
"pg" and prevents the "Could not find a declaration file for module 'pg'" error.
🧹 Nitpick comments (1)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (1)
100-108: Consider usingtsas the language identifier for this TypeScript file.Line 100 uses
```js file=seed.ts— since the file isseed.tsand the content uses TypeScript-style ESM imports, usingtsas the language identifier would give readers proper syntax highlighting cues and be more consistent with the file extension.
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx
Outdated
Show resolved
Hide resolved
- Add pool.end() in both success and error handlers for TypeScript and JavaScript examples - Update JavaScript tab to use adapter pattern (PrismaPg) - Add @types/pg to dependencies - Add info callout to JavaScript example explaining v7 requirement - Add dotenv import to JavaScript example Addresses all CodeRabbit feedback
|
Thanks for the review! I've addressed all the feedback:
All examples now follow the same pattern:
Re: the |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (2)
232-294:⚠️ Potential issue | 🟠 MajorMissing dependency installation step in the JavaScript tab.
The TypeScript tab (line 163-165) explicitly tells users to install
@prisma/adapter-pg,pg,@types/pg, anddotenvas dev dependencies. The JavaScript tab now uses the same packages (@prisma/adapter-pg,pg,dotenv) but has no corresponding installation step — it jumps straight from the seed script to configuringprisma.config.ts.A JS user following this guide will hit
Cannot find module '@prisma/adapter-pg'on their firstprisma db seedrun.Proposed fix — add a dependency step between the seed.js code block and the prisma.config.ts step
Add a new step 3 (and renumber the subsequent steps) after line 294:
``` +3. Add `@prisma/adapter-pg`, `pg`, and `dotenv` as development dependencies: + ``` + npm install -D `@prisma/adapter-pg` pg dotenv + ``` + -3. Add the `seed` field to your `prisma.config.ts` file: +4. Add the `seed` field to your `prisma.config.ts` file:
345-356:⚠️ Potential issue | 🟡 MinorRaw SQL chaining example drops
pool.end()from the lifecycle handlers.This snippet is presented as a replacement for the chain at the end of the seed file. If a user adopts it verbatim after setting up the adapter pattern from the earlier section, they'll lose the
pool.end()calls, leaving the connection pool open.Since this section is meant to build on the seed.js example above, it should include
pool.end()for consistency:Proposed fix
main() .then(rawSql) .then(async () => { await prisma.$disconnect() + await pool.end() }) .catch(async (e) => { console.error(e) await prisma.$disconnect() + await pool.end() process.exit(1) })
🧹 Nitpick comments (1)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (1)
408-441: User-defined arguments example lacks adapter initialization — could confuse v7 users.This standalone snippet uses
prisma.$disconnect()but never shows howprismais constructed. After the earlier sections go to great lengths to explain that v7 requires an adapter, a user landing on this section might be confused about whether the adapter pattern applies here too.Consider adding a brief note (even a one-liner comment like
// Initialize PrismaClient with adapter as shown above) or a reference back to the adapter setup section, so readers understand the same initialization requirement applies.
- Split dependencies into common (typescript, tsx, @types/node, dotenv) and database-specific (@prisma/adapter-pg, pg, @types/pg for PostgreSQL) - Apply to both TypeScript and JavaScript tabs - Makes it clearer which packages are needed for each database - Users can easily swap PostgreSQL packages for their database of choice
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (2)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (2)
363-374:⚠️ Potential issue | 🟡 MinorDownstream promise-chain examples are now inconsistent — missing
pool.end().Now that the primary seed examples establish the adapter + pool pattern, users who follow the "raw SQL" section will replace their promise chain with this one — which drops
pool.end(). The same issue applies to the "user-defined arguments" example at lines 450–458. Both will cause the process to hang after seeding.Since these sections instruct users to modify the end of the seed file, they should reflect the current cleanup pattern.
Proposed fix for the raw SQL chain (lines 363–374)
main() .then(rawSql) .then(async () => { await prisma.$disconnect() + await pool.end() }) .catch(async (e) => { console.error(e) await prisma.$disconnect() + await pool.end() process.exit(1) })Proposed fix for user-defined arguments (lines 450–458)
main() .then(async () => { await prisma.$disconnect() + await pool.end() }) .catch(async (e) => { console.error(e) await prisma.$disconnect() + await pool.end() process.exit(1) })
177-194:⚠️ Potential issue | 🟡 MinorDuplicate step number: two "step 5"s in the TypeScript tab.
Lines 177 and 194 are both numbered as step 5. The second one ("To seed the database…") should be step 6.
Proposed fix
-5. To seed the database, run the `db seed` CLI command: +6. To seed the database, run the `db seed` CLI command:
🧹 Nitpick comments (1)
content/200-orm/300-prisma-migrate/300-workflows/10-seeding.mdx (1)
106-106: Consider using the template literal pattern for the connection string for docs consistency.Other Prisma database adapter documentation examples use a
const connectionString = \${process.env.DATABASE_URL}`` pattern before passing it to the Pool constructor. This same convention appears at line 247 in the JS tab as well. It's a small consistency nit, but keeping patterns uniform across the docs helps users recognize the same approach everywhere.Proposed fix (TS example, line 106)
+ const connectionString = `${process.env.DATABASE_URL}` - const pool = new Pool({ connectionString: process.env.DATABASE_URL }) + const pool = new Pool({ connectionString })Proposed fix (JS example, line 247)
+ const connectionString = `${process.env.DATABASE_URL}` - const pool = new Pool({ connectionString: process.env.DATABASE_URL }) + const pool = new Pool({ connectionString })Based on learnings: "In Prisma docs, when showing examples of instantiating driver adapters with connection strings from environment variables, use the template literal pattern
const connectionString = \${process.env.DATABASE_URL}`` for consistency across all database adapter examples."
Summary
This PR updates the seeding documentation to reflect that Prisma ORM v7 requires an
adapteroraccelerateUrloption when initializing PrismaClient.Changes
seed.tsexample to include adapter initialization using@prisma/adapter-pg@prisma/adapter-pg,pg, anddotenvBackground
In Prisma ORM v7, running
new PrismaClient()without options throws:This is a breaking change from v6 that is not currently documented in the seeding guide, causing confusion for developers migrating to v7.
Related Issues
PrismaClientInitializationErrorwhen runningprisma db seedTesting
Tested with:
@prisma/adapter-pgv7.3.0Summary by CodeRabbit