Database connection boundaries
What you need to know
Database access belongs on the server side of a Next.js application. A connection boundary controls where credentials live, which operations are allowed, and how resources are reused and released.
By the end, you can: choose local fixture and pooling strategy; keep credentials server-side.
Disposable Node SQLite query
// standalone .mjs, Node 22.16 authoring lab; experimental node:sqliteimport {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try { db.exec('CREATE TABLE books(id TEXT PRIMARY KEY NOT NULL,title TEXT NOT NULL)'); db.prepare('INSERT INTO books VALUES (?,?)').run('b1','Fern'); console.log(db.prepare('SELECT title FROM books WHERE id=?').get('b1').title);} finally {db.close();}Expected behavior: Fern. The database is in memory and closes even if the query fails.
How it works
A repository is a small interface to persistence, such as findBook(id). SQLite can be convenient for disposable local exercises because it can run in-process. PostgreSQL is a networked relational server and supports many concurrent clients with a different locking and isolation model. A local file is not automatically shared among deployed replicas; in-memory data disappears with the process. Use a supported driver in the Node runtime when it requires Node facilities. A bounded pool reuses database connections across requests within a process; do not create an unbounded pool per request, and account for all replicas, including old and new processes during a rolling release. For example, four processes with a pool maximum of five can occupy up to 20 connections, before other services or administrative headroom. A process-local pool does not enforce a fleet-wide limit. Keep connection strings in server configuration and avoid NEXT_PUBLIC variables for secrets. Parameter binding separates query text from values. It cannot bind SQL identifiers; select identifiers from a fixed allowlist instead. Prisma and Drizzle provide higher-level query and migration tools, but driver/runtime compatibility, generated SQL and database constraints still matter. Exact ORM versions and live PostgreSQL access are intentionally not selected in this batch.
A bounded PostgreSQL read boundary
// lib/books.ts — partial integration, reviewed pg dependency requiredimport 'server-only';import type {Pool} from 'pg';export async function findBook(pool:Pool,id:string) { const result=await pool.query('SELECT id,title FROM books WHERE id=$1',[id]); return result.rows[0] ?? null;}Expected behavior: The injected pool executes a parameterized query; missing records return null. Pool creation, configuration and shutdown are the application owner’s responsibility.
Common mistake
Do not import a database client into a Client Component or allocate a fresh unbounded pool for every render. Do not concatenate an input into SQL. A synchronous SQLite API can block the Node thread and is not a universal production choice.
Practice and verification
Choose a storage strategy for a local single-user reading list and for a replicated public catalog. Explain connection limits, backups, persistence and server-only credentials. Run the provided SQLite fixture; stage PostgreSQL until an approved service exists.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.
Further reading: Pg Queries · Pg Pool · Sqlite Node