Skip to content

Commit 9e58c31

Browse files
authored
docs: fix and add jsdocs to promise.js for tsc inference (#205)
We noticed that many of the types in the tsc-generated `promise.d.ts` definitions were either missing or incorrect. This attempts to address some of those issues through updating JSDocs. In particular, correcting the Database constructor to type the parameter as NativeDb allows tsc to correctly infer more method return types. Here is `promise.d.ts` after a local run of tsc with these changes applied: ```ts import Authorization = require("./auth"); /** * Database represents a connection that can prepare and execute SQL statements. */ export class Database { /** * Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created. * * @constructor * @param {NativeDb} db - Database object */ constructor(db: NativeDb); db: NativeDb; memory: boolean; sync(): Promise<import("./index.js").SyncResult>; syncUntil(replicationIndex: any): void; /** * Prepares a SQL statement for execution. * * @param {string} sql - The SQL statement string to prepare. */ prepare(sql: string): Promise<Statement>; /** * Returns a function that executes the given function in a transaction. * * @param {function} fn - The function to wrap in a transaction. */ transaction(fn: Function): (...bindParameters: any[]) => Promise<any>; /** * Execute a pragma statement * @param {string} source - The pragma statement to execute, without the `PRAGMA` prefix. * @param {object} [options] - Options object. * @param {boolean} [options.simple] - If true, return a single value for single-column results. */ pragma(source: string, options?: { simple?: boolean | undefined; }): Promise<object>; backup(filename: any, options: any): void; serialize(options: any): void; function(name: any, options: any, fn: any): void; aggregate(name: any, options: any): void; table(name: any, factory: any): void; authorizer(rules: any): void; authorizer(hook: any): this; /** * Loads an extension into the database * @param {Parameters<NativeDb['loadExtension']>} args - Arguments to pass to the underlying loadExtension method */ loadExtension(path: string, entryPoint?: string | null | undefined): void; maxWriteReplicationIndex(): number; /** * Executes a SQL statement. * * @param {string} sql - The SQL statement string to execute. */ exec(sql: string): Promise<void>; /** * Interrupts the database connection. */ interrupt(): void; /** * Closes the database connection. */ close(): void; /** * Toggle 64-bit integer support. * @param {boolean} [toggle] - Whether to use safe integers by default. */ defaultSafeIntegers(toggle?: boolean): this; unsafeMode(...args: any[]): void; } import SqliteError = require("./sqlite-error.js"); /** * Statement represents a prepared SQL statement that can be executed. */ export class Statement { /** * @param {NativeStatement} stmt */ constructor(stmt: NativeStatement); stmt: NativeStatement; /** * Toggle raw mode. * * @param {boolean} [raw] - Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled. */ raw(raw?: boolean): this; /** * Toggle pluck mode. * * @param {boolean} [pluckMode] - Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled. */ pluck(pluckMode?: boolean): this; /** * Toggle query timing. * * @param {boolean} [timingMode] - Enable or disable query timing. If you don't pass the parameter, query timing is enabled. */ timing(timingMode?: boolean): this; get reader(): void; /** * Executes the SQL statement and returns an info object. */ run(...bindParameters: any[]): Promise<object>; /** * Executes the SQL statement and returns the first row. * * @param bindParameters - The bind parameters for executing the statement. */ get(...bindParameters: any[]): Promise<object>; /** * Executes the SQL statement and returns an iterator to the resulting rows. * * @param bindParameters - The bind parameters for executing the statement. */ iterate(...bindParameters: any[]): Promise<{ next(): any; [Symbol.asyncIterator](): { next(): any; }; }>; /** * Executes the SQL statement and returns an array of the resulting rows. * * @param bindParameters - The bind parameters for executing the statement. */ all(...bindParameters: any[]): Promise<any[]>; /** * Interrupts the statement. */ interrupt(): this; /** * Returns the columns in the result set returned by this prepared statement. */ columns(): unknown[]; /** * Toggle 64-bit integer support. */ safeIntegers(toggle: any): this; } /** * Creates a new database connection. * * @param {string} path - Path to the database file. * @param {NativeOptions} opts - Options. */ export function connect(path: string, opts: NativeOptions): Promise<Database>; import { Database as NativeDb } from "./index.js"; import type { Statement as NativeStatement } from './index.js'; import type { Options as NativeOptions } from './index.js'; export { Authorization, SqliteError }; ```
2 parents 31bed66 + 0bd2a39 commit 9e58c31

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

promise.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ const { Database: NativeDb, connect: nativeConnect } = require("./index.js");
44
const SqliteError = require("./sqlite-error.js");
55
const Authorization = require("./auth");
66

7+
/**
8+
* @import {Options as NativeOptions, Statement as NativeStatement} from './index.js'
9+
*/
10+
711
function convertError(err) {
812
// Handle errors from Rust with JSON-encoded message
913
if (typeof err.message === 'string') {
@@ -32,7 +36,7 @@ function convertError(err) {
3236
* Creates a new database connection.
3337
*
3438
* @param {string} path - Path to the database file.
35-
* @param {object} opts - Options.
39+
* @param {NativeOptions} opts - Options.
3640
*/
3741
const connect = async (path, opts) => {
3842
const db = await nativeConnect(path, opts);
@@ -47,7 +51,7 @@ class Database {
4751
* Creates a new database connection. If the database file pointed to by `path` does not exists, it will be created.
4852
*
4953
* @constructor
50-
* @param {string} path - Path to the database file.
54+
* @param {NativeDb} db - Database object
5155
*/
5256
constructor(db) {
5357
this.db = db;
@@ -124,6 +128,12 @@ class Database {
124128
return properties.default.value;
125129
}
126130

131+
/**
132+
* Execute a pragma statement
133+
* @param {string} source - The pragma statement to execute, without the `PRAGMA` prefix.
134+
* @param {object} [options] - Options object.
135+
* @param {boolean} [options.simple] - If true, return a single value for single-column results.
136+
*/
127137
async pragma(source, options) {
128138
if (options == null) options = {};
129139
if (typeof source !== 'string') throw new TypeError('Expected first argument to be a string');
@@ -161,6 +171,10 @@ class Database {
161171
}
162172
}
163173

174+
/**
175+
* Loads an extension into the database
176+
* @param {Parameters<NativeDb['loadExtension']>} args - Arguments to pass to the underlying loadExtension method
177+
*/
164178
loadExtension(...args) {
165179
try {
166180
this.db.loadExtension(...args);
@@ -211,6 +225,7 @@ class Database {
211225

212226
/**
213227
* Toggle 64-bit integer support.
228+
* @param {boolean} [toggle] - Whether to use safe integers by default.
214229
*/
215230
defaultSafeIntegers(toggle) {
216231
this.db.defaultSafeIntegers(toggle);
@@ -226,14 +241,17 @@ class Database {
226241
* Statement represents a prepared SQL statement that can be executed.
227242
*/
228243
class Statement {
244+
/**
245+
* @param {NativeStatement} stmt
246+
*/
229247
constructor(stmt) {
230248
this.stmt = stmt;
231249
}
232250

233251
/**
234252
* Toggle raw mode.
235253
*
236-
* @param raw Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled.
254+
* @param {boolean} [raw] - Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled.
237255
*/
238256
raw(raw) {
239257
this.stmt.raw(raw);
@@ -243,7 +261,7 @@ class Statement {
243261
/**
244262
* Toggle pluck mode.
245263
*
246-
* @param pluckMode Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
264+
* @param {boolean} [pluckMode] - Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
247265
*/
248266
pluck(pluckMode) {
249267
this.stmt.pluck(pluckMode);
@@ -253,7 +271,7 @@ class Statement {
253271
/**
254272
* Toggle query timing.
255273
*
256-
* @param timing Enable or disable query timing. If you don't pass the parameter, query timing is enabled.
274+
* @param {boolean} [timingMode] - Enable or disable query timing. If you don't pass the parameter, query timing is enabled.
257275
*/
258276
timing(timingMode) {
259277
this.stmt.timing(timingMode);

0 commit comments

Comments
 (0)