From d60e7d13a34c52eded835f4d1d16c37abbe09ee7 Mon Sep 17 00:00:00 2001 From: vedchaudhari Date: Wed, 25 Nov 2026 14:47:54 +0530 Subject: [PATCH] sqlite: add database.createCollation() Expose sqlite3_create_collation_v2() so that SQL can sort and compare text with a JavaScript function, and so that databases whose schema uses a custom collation can be read. SQLite has no way for a collation to report an error. If the compare function throws, the statement is interrupted and the exception is thrown once the statement returns. If the statement was writing, its changes are rolled back, the same as SQLite does for any interrupted write. Fixes: https://github.com/nodejs/node/issues/63316 Signed-off-by: vedchaudhari --- doc/api/sqlite.md | 65 +++++ src/node_sqlite.cc | 169 ++++++++++- src/node_sqlite.h | 29 ++ test/parallel/test-sqlite-collation.js | 369 +++++++++++++++++++++++++ 4 files changed, 628 insertions(+), 4 deletions(-) create mode 100644 test/parallel/test-sqlite-collation.js diff --git a/doc/api/sqlite.md b/doc/api/sqlite.md index 422df94ea540..f2e51028bf6f 100644 --- a/doc/api/sqlite.md +++ b/doc/api/sqlite.md @@ -921,6 +921,68 @@ console.log(allUsers); // ] ``` +### `database.createCollation(name, compare)` + + + +* `name` {string} The name of the collation. Like other SQLite names, it is not + case-sensitive. +* `compare` {Function} The function to call to compare two strings. It + receives the two strings and returns a negative number if the first string + sorts before the second, a positive number if it sorts after, and zero if + they are equal. The result is converted to a number, as with the compare + function of [`Array.prototype.sort()`][], and `NaN` means equal. + +Creates a collation, or replaces an existing one with the same name. SQL can +then use it in `COLLATE` clauses and in table and index definitions. This +method is a wrapper around [`sqlite3_create_collation_v2()`][]. + +`compare` must always return the same result for the same two strings, as +SQLite relies on that to keep indexes consistent. + +SQLite has no way for a collation to report an error. If `compare` throws, the +statement that called it is stopped with [`sqlite3_interrupt()`][], and the +exception is thrown from the method that ran the statement. As with any +interrupted statement, if it was changing the database, its changes are rolled +back, along with the open transaction if there is one. Interrupting also stops +any other statement that is running on the same connection at the time. + +```cjs +const { Database } = require('node:sqlite'); + +const db = new Database(':memory:'); + +db.createCollation('locale', (a, b) => a.localeCompare(b)); +db.exec(` + CREATE TABLE artists (name TEXT); + INSERT INTO artists VALUES ('Zebra'), ('Ágætis'), ('apple'); +`); + +console.log( + db.prepare('SELECT name FROM artists ORDER BY name COLLATE locale').all(), +); +// Prints: [ { name: 'Ágætis' }, { name: 'apple' }, { name: 'Zebra' } ] +``` + +```mjs +import { Database } from 'node:sqlite'; + +const db = new Database(':memory:'); + +db.createCollation('locale', (a, b) => a.localeCompare(b)); +db.exec(` + CREATE TABLE artists (name TEXT); + INSERT INTO artists VALUES ('Zebra'), ('Ágætis'), ('apple'); +`); + +console.log( + db.prepare('SELECT name FROM artists ORDER BY name COLLATE locale').all(), +); +// Prints: [ { name: 'Ágætis' }, { name: 'apple' }, { name: 'Zebra' } ] +``` + ### `database.createModule(name, options)`