Dev HelpersOpen library
← Back

Register a SQL function

Register a SQL function using Node.js.

Open this example in the interactive library

Source code

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.function("twice", (n) => n * 2),
  db.prepare("SELECT twice(3) AS n").get()),
);

Sample output

[Object: null prototype] { n: 6 }