Dev Helpers

Node.js / SQLite in Node

Read the first matching row

Dev Helpers · September 11, 2025

Reading one database row needs a defined ordering when more than one row matches. An absent match also deserves an explicit branch.

Worked example

import { DatabaseSync } from "node:sqlite";
const db = new DatabaseSync(":memory:");
db.exec(
  "CREATE TABLE jobs(id INTEGER PRIMARY KEY, name TEXT); INSERT INTO jobs(name) VALUES ('build'), ('test')",
);
console.log(db.prepare("SELECT name FROM jobs WHERE id=?").get(2));

Result

[Object: null prototype] { name: 'test' }

Implementation notes

A single-row accessor and a full result list have different missing-result behaviour. Normalize that difference at the repository boundary.

Try it yourself

Add two matching rows and run a query with a stable sort.

Open the Node.js example →

Editorial date September 11, 2025 · Published December 6, 2026

Related articles

Safely parse malformed input

Parsing user-provided JSON needs a failure path. Successful parsing alone does not establish that the result has the expected shape.

Checksum bytes with CRC32

CRC32 is useful for detecting accidental changes to a byte sequence. Record the input encoding so different producers compute the same checksum.

Attach details to a mark

Performance marks can carry details about the operation they describe. Small stable identifiers are easier to aggregate than large payloads.