SQL drivers and parameters
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 NOT NULL)'); const insert=db.prepare('INSERT INTO book VALUES (?,?)'); insert.run(1,"Reader's Guide"); insert.run(2,'Rivers'); const find=db.prepare('SELECT id FROM book WHERE title = ?'); console.log(find.get("Reader's Guide").id); console.log(find.get('Missing') === undefined);} finally {db.close();}Run node main.mjs in a disposable directory with the files shown. Expected standard output:
1trueAn experimental warning is expected on stderr for this local version.
How it works
SQLite uses ? placeholders in this example; PostgreSQL through pg commonly uses $1, $2 and a values array. A placeholder is for a value, not a table name or an ORDER BY column. Choose dynamic identifiers from an allowlist of fixed SQL fragments. In the actual in-memory SQLite fixture, a primary key identifies each book and a prepared statement selects one row by title. The synchronous DatabaseSync API blocks the calling JavaScript thread during operations; do not infer async PostgreSQL performance from this example.
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.
// Partial pg repository function: supplied approved pool, existing book table.export async function findBook(pool, title) { const result = await pool.query('SELECT id, title FROM book WHERE title = $1', [title]); return result.rows;}Returns matching rows; requires pg and a synthetic PostgreSQL schema. NOT RUN.
Common mistake
Do not concatenate external strings into SQL or use binding as a substitute for authorization. Even a safe query must restrict results to records the caller may see.
Practice
Query a missing title and check that get() returns undefined. Try a title containing an apostrophe as ordinary data.
Documentation: primary source. Candidate lookup: node-sql-drivers-and-parameters-lookup. Prerequisite: node-typed-api-contracts (platform route unverified).
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SQL drivers and parameters — knowledge check
Apply the lesson’s contracts and identify common boundary mistakes.
What should contain a user-provided title?
Can a placeholder select an arbitrary table name?
What does DatabaseSync imply?