From 53d272e8138f5f6353b98535a2bf1c78be6da735 Mon Sep 17 00:00:00 2001 From: Albert Xing Date: Thu, 9 Oct 2025 16:37:17 -0700 Subject: [PATCH] docs: more types for promise wrapper --- index.d.ts | 2 +- promise.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 2384f77..15b33b4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -153,7 +153,7 @@ export declare class Statement { * * * `params` - The parameters to bind to the statement. */ - run(params?: unknown | undefined | null): object + run(params?: unknown | undefined | null): RunResult /** * Executes a SQL statement and returns the first row. * diff --git a/promise.js b/promise.js index 378e8fe..1c9e673 100644 --- a/promise.js +++ b/promise.js @@ -8,6 +8,15 @@ const Authorization = require("./auth"); * @import {Options as NativeOptions, Statement as NativeStatement} from './index.js' */ +/** + * better-sqlite3-compatible types for db.transaction() + * @typedef {(...params: any[]) => unknown} VariableArgFunction + */ +/** + * @template {VariableArgFunction} F + * @typedef {F extends (...args: infer A) => unknown ? A : never} ArgumentTypes + */ + function convertError(err) { // Handle errors from Rust with JSON-encoded message if (typeof err.message === 'string') { @@ -56,6 +65,10 @@ class Database { constructor(db) { this.db = db; this.memory = this.db.memory + + /** @type boolean */ + this.inTransaction; + Object.defineProperties(this, { inTransaction: { get() { @@ -94,7 +107,15 @@ class Database { /** * Returns a function that executes the given function in a transaction. * - * @param {function} fn - The function to wrap in a transaction. + * @template {VariableArgFunction} F + * @param {F} fn - The function to wrap in a transaction. + * @returns {{ + * (...bindParameters: ArgumentTypes): Promise>; + * default(...bindParameters: ArgumentTypes): Promise>; + * deferred(...bindParameters: ArgumentTypes): Promise>; + * immediate(...bindParameters: ArgumentTypes): Promise>; + * exclusive(...bindParameters: ArgumentTypes): Promise>; + * }} */ transaction(fn) { if (typeof fn !== "function")