Document local Prisma Postgres management in Node.js - #7175
Conversation
Added instructions for managing a local Prisma Postgres server programmatically using Node.js, including a runnable example.
Dangerous URL checkNo absolute URLs to prisma.io/docs found. |
Redirect checkThis PR probably requires the following redirects to be added to static/_redirects:
|
WalkthroughAdds a new documentation section demonstrating how to programmatically start, connect to, and stop a local Prisma Postgres server from Node.js using undocumented, unstable Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Pre-merge checks✅ Passed checks (3 passed)
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 |
Deploying docs with
|
| Latest commit: |
a36113a
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://d88cb8c1.docs-51g.pages.dev |
| Branch Preview URL: | https://programmatic-local-ppg-1.docs-51g.pages.dev |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
content/250-postgres/300-database/550-local-development.mdx (3)
170-200: Make the example runnable everywhere and ensure clean shutdowns
- Avoid top-level await (works only in ESM). Wrap in an async IIFE.
- Await and move
client.end()intofinally.- Guard
server.closeinstead of using non-null assertion.Apply this diff:
-```ts +```ts import { Client } from 'pg' import { unstable_startServer } from '@prisma/dev' import { getPort } from 'get-port-please' async function startLocalPrisma(name: string) { - const port = await getPort() - - return await unstable_startServer({ - name, // use a unique name if running tests in parallel - port, - databasePort: port + 1, - shadowDatabasePort: port + 2, - persistenceMode: 'stateless' - }) + const port = await getPort() + + return await unstable_startServer({ + name, // use a unique name if running tests in parallel + port, + databasePort: port + 1, + shadowDatabasePort: port + 2, + persistenceMode: 'stateless', + }) } -// Usage in tests -const server = await startLocalPrisma(`my-tests-${Date.now()}`) -try { - const client = new Client({ connectionString: server.database.connectionString }) - await client.connect() - - const res = await client.query(`SELECT 1 as "abba"`) - console.log(res.rows) - - client.end() -} finally { - await server.close!() -} +// Usage in tests +void (async () => { + const server = await startLocalPrisma(`my-tests-${Date.now()}`) + let client: Client | undefined + try { + client = new Client({ connectionString: server.database.connectionString }) + await client.connect() + const res = await client.query(`SELECT 1 as "abba"`) + console.log(res.rows) + } finally { + if (client) await client.end() + if (server.close) await server.close() + } +})().catch((e) => { + console.error(e) + process.exit(1) +})
175-184: Avoid assuming consecutive free portsDeriving ports with +1/+2 can race with other processes. Allocate each port independently.
Apply this diff:
async function startLocalPrisma(name: string) { - const port = await getPort() - - return await unstable_startServer({ - name, // use a unique name if running tests in parallel - port, - databasePort: port + 1, - shadowDatabasePort: port + 2, - persistenceMode: 'stateless', - }) + const port = await getPort() + const databasePort = await getPort() + const shadowDatabasePort = await getPort() + + return await unstable_startServer({ + name, // use a unique name if running tests in parallel + port, + databasePort, + shadowDatabasePort, + persistenceMode: 'stateless', + }) }
202-206: Add prerequisites to make the example truly runnableNote the required packages and ESM caveat for top-level await.
Apply this diff:
Notes: - Allocate unique ports and `name` values when running tests concurrently. - Use `server.database.connectionString` to connect with Postgres clients or ORMs. - This pattern is great for running tests that require a local database. +- Install dependencies: + + ```bash + npm i pg + npm i -D @prisma/dev get-port-please + ``` + +- If not using ESM (`"type": "module"`), prefer the async IIFE pattern shown to avoid top‑level await.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
content/250-postgres/300-database/550-local-development.mdx(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: runner / linkspector
- GitHub Check: Check internal links
48f76e6
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (3)
content/250-postgres/300-database/550-local-development.mdx (3)
214-218: Avoid assuming adjacent ports are free.Add a note that each port should be probed independently; +1/+2 can be occupied by other processes.
Apply this diff:
:::tip - -You can dynamically choose available ports using libraries like [`get-port-please`](https://www.npmjs.com/package/get-port-please) to avoid conflicts when running multiple instances. +You can dynamically choose available ports using libraries like [`get-port-please`](https://www.npmjs.com/package/get-port-please) to avoid conflicts when running multiple instances. Probe each required port independently—in busy environments, `port + 1`/`+ 2` may already be taken. :::
222-224: Tighten phrasing: clarify client types for the connection string.
server.database.connectionStringis a Postgres TCP URL; calling out “non‑Prisma ORMs” avoids confusion withprisma+postgres.Apply this diff:
-- Use `server.database.connectionString` to connect with Postgres clients or ORMs. +- Use `server.database.connectionString` to connect with Postgres clients or non‑Prisma ORMs.
206-213: ClarifydatabasePortsemantics and default behavior.
- Prisma ORM connects via the HTTP server (
port);databasePortis for the embedded Postgres process (used internally by Prisma server and by direct SQL clients).- Default is calculated as
port + 1(51214 whenportis 51213); update table to showport + 1(51214ifport=51213).
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
content/250-postgres/300-database/550-local-development.mdx(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: runner / linkspector
- GitHub Check: Cloudflare Pages
Added instructions for managing a local Prisma Postgres server programmatically using Node.js, including a runnable example.
Summary by CodeRabbit