Skip to content

Commit cb5a439

Browse files
authored
docs: add more better-sqlite3-compatible types to libsql/promise declaration (#206)
Follow-up to #205 After upgrading to the latest release, we found that there were still a couple of types that were mismatched or missing compared to better-sqlite3. In particular: - The return type for `Database.transaction` was missing the fields attached to the function (e.g. `immediate`, `deferred`) - `Database.inTransaction` was not present (because it's assigned as a getter via defineProperty) - `Statement.run` returned `object` instead of `RunResult`
2 parents 39559b3 + 53d272e commit cb5a439

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export declare class Statement {
153153
*
154154
* * `params` - The parameters to bind to the statement.
155155
*/
156-
run(params?: unknown | undefined | null): object
156+
run(params?: unknown | undefined | null): RunResult
157157
/**
158158
* Executes a SQL statement and returns the first row.
159159
*

promise.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ const Authorization = require("./auth");
88
* @import {Options as NativeOptions, Statement as NativeStatement} from './index.js'
99
*/
1010

11+
/**
12+
* better-sqlite3-compatible types for db.transaction()
13+
* @typedef {(...params: any[]) => unknown} VariableArgFunction
14+
*/
15+
/**
16+
* @template {VariableArgFunction} F
17+
* @typedef {F extends (...args: infer A) => unknown ? A : never} ArgumentTypes
18+
*/
19+
1120
function convertError(err) {
1221
// Handle errors from Rust with JSON-encoded message
1322
if (typeof err.message === 'string') {
@@ -56,6 +65,10 @@ class Database {
5665
constructor(db) {
5766
this.db = db;
5867
this.memory = this.db.memory
68+
69+
/** @type boolean */
70+
this.inTransaction;
71+
5972
Object.defineProperties(this, {
6073
inTransaction: {
6174
get() {
@@ -94,7 +107,15 @@ class Database {
94107
/**
95108
* Returns a function that executes the given function in a transaction.
96109
*
97-
* @param {function} fn - The function to wrap in a transaction.
110+
* @template {VariableArgFunction} F
111+
* @param {F} fn - The function to wrap in a transaction.
112+
* @returns {{
113+
* (...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
114+
* default(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
115+
* deferred(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
116+
* immediate(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
117+
* exclusive(...bindParameters: ArgumentTypes<F>): Promise<ReturnType<F>>;
118+
* }}
98119
*/
99120
transaction(fn) {
100121
if (typeof fn !== "function")

0 commit comments

Comments
 (0)