corpus

self-hosted listenbrainz and last.fm proxy
Log | Files | Refs | README | LICENSE

Db.js (1185B)


      1 import { DuckDBInstance } from "@duckdb/node-api";
      2 import { createHash } from "crypto";
      3 
      4 export const sha256 = (str) => {
      5 	return createHash("sha256").update(str).digest("hex");
      6 };
      7 
      8 export const connectImpl = (path, cb) => () => {
      9 	DuckDBInstance.create(path)
     10 		.then((instance) => instance.connect())
     11 		.then((conn) => cb(null)(conn)())
     12 		.catch((e) => cb(e)(null)());
     13 };
     14 
     15 export const runImpl = (conn, sql, params, cb) => () => {
     16 	conn
     17 		.run(sql, params)
     18 		.then(() => cb(null)())
     19 		.catch((e) => cb(e)());
     20 };
     21 
     22 export const checkpointImpl = (conn, cb) => () => {
     23 	conn
     24 		.run("CHECKPOINT")
     25 		.then(() => cb(null)())
     26 		.catch((e) => cb(e)());
     27 };
     28 
     29 // DuckDB returns BIGINT as BigInt, which JSON.stringify doesn't support.
     30 // Pure transformation: produce new row objects rather than mutating in place.
     31 const convertBigInts = (row) =>
     32 	Object.fromEntries(
     33 		Object.entries(row).map(([k, v]) => [
     34 			k,
     35 			typeof v === "bigint" ? Number(v) : v,
     36 		]),
     37 	);
     38 
     39 export const allImpl = (conn, sql, params, cb) => () => {
     40 	conn
     41 		.run(sql, params)
     42 		.then((result) => result.getRowObjectsJS())
     43 		.then((rows) => {
     44 			cb(null)(rows.map(convertBigInts))();
     45 		})
     46 		.catch((e) => cb(e)(null)());
     47 };