import { IBaseSqliteDialectConfig, Promisable, GenericSqliteDialect, BaseSqliteDialect } from 'kysely-generic-sqlite'; interface BaseDB { close: () => void; } interface CrSqliteDB extends BaseDB { execA: (sql: string, bind?: any[]) => Promise; execO: (sql: string, bind?: any[]) => Promise; onUpdate: (cb: (type: any, dbName: string, tblName: string, rowid: bigint) => void) => void; api: { changes: (db: number) => number; }; db: number; } interface CrSqliteDialectConfig extends IBaseSqliteDialectConfig { database: CrSqliteDB | (() => Promisable); } declare class CrSqliteDialect extends GenericSqliteDialect { /** * dialect for [vlcn.io/wasm](https://vlcn.io/js/wasm) */ constructor(config: CrSqliteDialectConfig); } /** * Should only be used to generate sql statements */ declare class EmptyDialect extends BaseSqliteDialect { constructor(); } type SQLiteValue = number | bigint | string | Uint8Array | null; type JSValue = boolean | SQLiteValue; type BindValues = JSValue | JSValue[] | Record; type NormalQueryResult = Record; type ExpandedQueryResult = Record; type QueryResult = NormalQueryResult | ExpandedQueryResult; interface RunResult { changes: number; lastInsertRowid: number | bigint; } interface QueryOptions { expand?: boolean; } interface NodeWasmDatabase extends BaseDB { run: (sql: string, values?: BindValues) => RunResult; all: (sql: string, values?: BindValues, options?: QueryOptions) => QueryResult[]; prepare: (sql: string) => Statement$1; } interface Statement$1 { iterate: (values?: BindValues, options?: QueryOptions) => IterableIterator; finalize: () => void; } interface NodeWasmDialectConfig extends IBaseSqliteDialectConfig { database: NodeWasmDatabase | (() => Promisable); } declare class NodeWasmDialect extends GenericSqliteDialect { /** * dialect for [node sqlite3 wasm](https://github.com/tndrle/node-sqlite3-wasm) */ constructor(config: NodeWasmDialectConfig); } interface OfficialWasmDB { /** Returns true if the database handle is open, else false. */ isOpen: () => boolean; /** Throws if the given DB has been closed. */ affirmOpen: () => this; /** * Finalizes all still-open statements which were opened by this object and * closes this database connection. This is a no-op if the db has already been * closed. After calling `close()`, {@link pointer} will resolve to * `undefined`, so that can be used to check whether the db instance is still * opened. * * If {@link onclose.before} is a function then it is called before any * close-related cleanup. If {@link onclose.after} is a function then it is * called after the db is closed but before auxiliary state like this.filename * is cleared. * * Both onclose handlers are passed this object as their only argument. If * this db is not opened, neither of the handlers are called. Any exceptions * the handlers throw are ignored because "destructors must not throw." * * Note that garbage collection of a db handle, if it happens at all, will * never trigger `close()`, so {@link onclose} handlers are not a reliable way * to implement close-time cleanup or maintenance of a db. */ close: () => void; /** * Returns the number of changes, as per `sqlite3_changes()` (if the first * argument is `false`) or `sqlite3_total_changes()` (if it's `true`). If the * 2nd argument is `true`, it uses `sqlite3_changes64()` or * `sqlite3_total_changes64()`, which will trigger an exception if this build * does not have `BigInt` support enabled. */ changes: (total?: boolean, sixtyFour?: boolean) => number; /** * Prepares the given SQL, `step()`s it one time, and returns an array * containing the values of the first result row. If it has no results, * `undefined` is returned. If passed a second argument other than * `undefined`, it is treated like an argument to * {@link PreparedStatement#bind}, so may be any type supported by that * function. Throws on error. */ selectArray: (sql: string, bind?: any[]) => any[] | undefined; /** * Works identically to {@link Database#selectArrays} except that each value in * the returned array is an object, as per the `"object"` rowMode option to * {@link Database#exec}. */ selectObjects: (sql: string, bind?: any[]) => { [columnName: string]: any; }[]; } interface OfficialWasmDialectConfig extends IBaseSqliteDialectConfig { database: OfficialWasmDB | (() => Promisable); } declare class OfficialWasmDialect extends GenericSqliteDialect { /** * dialect for [official wasm build](https://sqlite.org/wasm/doc/trunk/index.md) * * support bigint, recommend to use opfs * (see {@link https://sqlite.org/forum/forumpost/59097f57cbe647a2d1950fab93e7ab82dd24c1e384d38b90ec1e2f03a2a4e580 this} * and {@link https://sqlite.org/forum/forumpost/8f50dc99149a6cedade784595238f45aa912144fae81821d5f9db31965f754dd this}) * * you can also use [sqlite-wasm-esm](https://github.com/overtone-app/sqlite-wasm-esm) * * #### partial typescript support: * ```ts * /// * ``` * * @example * ```ts * import sqlite3InitModule from './jswasm/sqlite3-bundler-friendly' * const db = new Kysely({ * dialect: new OfficialSqliteWasmDialect({ * database: async () => { * const sqlite3 = (await sqlite3InitModule()).oo1 * if (!sqlite3) { * return Promise.reject('fail to load sqlite') * } * const path = '/test.db' * return sqlite3.OpfsDb * ? new sqlite3.OpfsDb(path) * : new sqlite3.DB(path) * }, * }), * }) * ``` * when using Origin-Private FileSystem, your server must response COOP and COEP in header, * ```ts * server.middlewares.use((_req, res, next) => { * res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp') * res.setHeader('Cross-Origin-Opener-Policy', 'same-origin') * next() * }) * ``` * @see https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API#origin_private_file_system * @see https://sqlite.org/wasm/doc/trunk/persistence.md#coop-coep */ constructor(config: OfficialWasmDialectConfig); } type SqlValue = number | string | Uint8Array | null; type ParamsObject = Record; type BindParams = SqlValue[] | ParamsObject | null; interface QueryExecResult { columns: string[]; values: SqlValue[][]; } interface SqlJSDB extends BaseDB { exec: (sql: string, params?: BindParams) => QueryExecResult[]; getRowsModified: () => number; prepare: (sql: string, params?: any[]) => Statement; } interface Statement { step: () => boolean; bind: (values?: any[]) => boolean; get: () => any[]; getAsObject: (params?: any[]) => any; free: () => boolean; } interface SqlJsDialectConfig extends IBaseSqliteDialectConfig { database: SqlJSDB | (() => Promisable); } declare class SqlJsDialect extends GenericSqliteDialect { /** * dialect for [sql.js](https://github.com/sql-js/sql.js) * * no support for bigint */ constructor(config: SqlJsDialectConfig); } export { CrSqliteDialect, type CrSqliteDialectConfig, EmptyDialect, NodeWasmDialect, type NodeWasmDialectConfig, type OfficialWasmDB, OfficialWasmDialect, type OfficialWasmDialectConfig, SqlJsDialect, type SqlJsDialectConfig };