From bbcbff9b4d829545baaf9012c67d9384d4e94ff5 Mon Sep 17 00:00:00 2001 From: Allon Murienik Date: Sat, 8 Nov 2025 17:01:50 +0200 Subject: [PATCH] doc: add CJS code snippets in `sqlite.md` Refs: https://github.com/nodejs/node/issues/60394 PR-URL: https://github.com/nodejs/node/pull/60395 Reviewed-By: Colin Ihrig Reviewed-By: Luigi Pinca --- doc/api/sqlite.md | 51 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 15f365da2ad..f0fea941ef4 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -473,6 +473,33 @@ console.log(allUsers); // ] ``` +```cjs +const { DatabaseSync } = require('node:sqlite'); + +const db = new DatabaseSync(':memory:'); +const sql = db.createTagStore(); + +db.exec('CREATE TABLE users (id INT, name TEXT)'); + +// Using the 'run' method to insert data. +// The tagged literal is used to identify the prepared statement. +sql.run`INSERT INTO users VALUES (1, 'Alice')`; +sql.run`INSERT INTO users VALUES (2, 'Bob')`; + +// Using the 'get' method to retrieve a single row. +const id = 1; +const user = sql.get`SELECT * FROM users WHERE id = ${id}`; +console.log(user); // { id: 1, name: 'Alice' } + +// Using the 'all' method to retrieve all rows. +const allUsers = sql.all`SELECT * FROM users ORDER BY id`; +console.log(allUsers); +// [ +// { id: 1, name: 'Alice' }, +// { id: 2, name: 'Bob' } +// ] +``` + ### `database.createSession([options])`