Query builders and ORM boundaries
Local baseline: Node.js 22.16.0. Actual local SQLite 3.49.1 via experimental node:sqlite. No third-party packages or external connections.
Try the local example
import {DatabaseSync} from 'node:sqlite';const db=new DatabaseSync(':memory:');try { db.exec("CREATE TABLE book(id INTEGER PRIMARY KEY,title TEXT, internal_note TEXT); INSERT INTO book VALUES(1,'Rivers','staff only')"); const repository={find(id){const row=db.prepare('SELECT id,title FROM book WHERE id=?').get(id);return row?{id:row.id,title:row.title}:null;}}; console.log(JSON.stringify(repository.find(1))); console.log(repository.find(2));}finally{db.close();}Run node main.mjs in a disposable directory with the files shown. Expected standard output:
{"id":1,"title":"Rivers"}nullAn experimental warning is expected on stderr for this local version.
How it works
A repository exposes an application-level operation, for example finding a book by ID. Drizzle expresses a selection and predicate in SQL-like TypeScript; Prisma uses a generated client and model operations; TypeORM offers entity mappings and repositories. Their transaction and migration APIs differ. The example uses a narrow repository over a real SQLite statement. The separate Drizzle function illustrates the same contract with an already configured PostgreSQL database and schema. Verify emitted SQL and parameter handling rather than trusting a library name. Client generation, package setup and database-specific behavior need separate tests.
Integration example — staged
Static candidate only. Exact package and server versions, dependency/advisory review, integration and compilation NOT RUN. Use only a reviewed disposable fixture; no real connection settings.
import {eq} from 'drizzle-orm';import {pgTable,integer,text} from 'drizzle-orm/pg-core';import type {NodePgDatabase} from 'drizzle-orm/node-postgres';const books=pgTable('book',{id:integer('id').primaryKey(),title:text('title').notNull()});export async function findBook(db:NodePgDatabase,id:number){ const rows=await db.select({id:books.id,title:books.title}).from(books).where(eq(books.id,id)); return rows[0]??null;}Partial repository with preconfigured Drizzle PostgreSQL database, matching schema and pinned dependencies required. NOT RUN.
Common mistake
Do not return whole entities when the caller only needs a public projection. ORM type inference does not replace database constraints or resource authorization.
Practice
Add a price column to the fixture and keep the public query limited to ID and title.
Documentation: primary source. Candidate lookup: node-query-builders-and-orm-boundaries-lookup. Prerequisite: node-transactions-and-rollback (platform route unverified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Query builders and ORM boundaries — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
What is still needed with an ORM?
Why select only id and title?
What is a repository boundary useful for?