node-ctypes - v2.17.1
    Preparing search index...

    SQLite UDFs

    A lightweight SQLite3 wrapper built directly on node-ctypes that loads the system's SQLite shared library (libsqlite3) and exposes custom scalar UDFs, multi-argument aggregates, and custom collations written in JavaScript — with zero native SQLite packages or node-gyp builds.

    Platforms: Windows x64 & ARM64 (sqlite3.dll bundled in win32-x64/BUILD.md and win32-arm64/BUILD.md, or preinstalled winsqlite3.dll), macOS (preinstalled libsqlite3.dylib), Linux (libsqlite3.so.0) · Library: sqlite.js · Requires: on Linux libsqlite3-0 (or sqlite3); macOS and Windows ship ready out of the box (override via SQLITE3_LIB)

    node examples/cross-platform/sqlite/demo_sqlite_udf.js
    
    import { Database } from "./sqlite.js";

    const db = new Database(":memory:");

    // 1. Scalar UDF: unlocks SQLite's REGEXP operator
    db.createFunction("REGEXP", 2, (pattern, text) =>
    pattern != null && text != null && new RegExp(pattern, "u").test(text) ? 1 : 0,
    );

    // 2. Aggregate UDF: median() per GROUP BY bucket
    db.createAggregate("median", 1, {
    init: () => [],
    step: (values, x) => { if (typeof x === "number") values.push(x); },
    finalize: (values) => {
    if (!values.length) return null;
    values.sort((a, b) => a - b);
    const mid = Math.floor(values.length / 2);
    return values.length % 2 ? values[mid] : (values[mid - 1] + values[mid]) / 2;
    },
    });

    // 3. Custom Collation: numeric-aware natural sorting via Intl.Collator
    const collator = new Intl.Collator("en", { numeric: true, sensitivity: "base" });
    db.createCollation("NATURAL_SORT", (a, b) => collator.compare(a, b));

    Without the wrapper, calling sqlite3_create_function_v2 directly with node-ctypes looks almost line-for-line like Python ctypes:

    import { CDLL, CFUNCTYPE, POINTER, byref, c_char_p, c_int, c_int64, c_void_p, util } from "node-ctypes";

    const lib = new CDLL(util.find_library("sqlite3"));
    const XFUNC = CFUNCTYPE(null, c_void_p, c_int, POINTER(c_void_p));
    const XFINAL = CFUNCTYPE(null, c_void_p);

    lib.sqlite3_open.argtypes = [c_char_p, POINTER(c_void_p)];
    lib.sqlite3_open.restype = c_int;
    lib.sqlite3_create_function_v2.argtypes = [c_void_p, c_char_p, c_int, c_int, c_void_p, XFUNC, XFUNC, XFINAL, c_void_p];
    lib.sqlite3_create_function_v2.restype = c_int;
    lib.sqlite3_value_int64.argtypes = [c_void_p];
    lib.sqlite3_value_int64.restype = c_int64;
    lib.sqlite3_result_int64.argtypes = [c_void_p, c_int64];

    const db = new c_void_p();
    lib.sqlite3_open(":memory:", byref(db));

    const square = XFUNC((ctx, _argc, argv) => {
    const x = lib.sqlite3_value_int64(argv[0]);
    lib.sqlite3_result_int64(ctx, x * x);
    });
    lib.sqlite3_create_function_v2(db, "sq", 1, 1, null, square, XFUNC(0), XFINAL(0), null);

    CDLL / WinDLL + util.find_library("sqlite3") · CFUNCTYPE / WINFUNCTYPE callbacks (xFunc, xStep, xFinal, xCompare) with XFUNC(0) / XFINAL(0) null-function arguments · POINTER(c_void_p) out-parameters with byref() and argv[i] array indexing · c_int64.from_address() over sqlite3_aggregate_context · memoryview_at() for zero-copy UTF-8 text and binary BLOB slices · string_at() + sqlite3_free() for error messages · new c_void_p(-1n) for SQLITE_TRANSIENT