Skip to content

Conversation

albertxing
Copy link
Contributor

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:

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 };

@penberg penberg merged commit 9e58c31 into tursodatabase:main Oct 9, 2025
60 of 64 checks passed
penberg added a commit that referenced this pull request Oct 16, 2025
…aration (#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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants