Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
23 changes: 22 additions & 1 deletion promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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<F>): Promise<ReturnType<F>>;
* default(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
* deferred(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
* immediate(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
* exclusive(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
* }}
*/
transaction(fn) {
if (typeof fn !== "function")
Expand Down
Loading