UNPKG

7.62 kBTypeScriptView Raw
1/// <reference types="./vendor-typings/sqlite3" />
2import * as sqlite3 from 'sqlite3';
3import { ISqlite, IMigrate } from './interfaces';
4import { Statement } from './Statement';
5import MigrationParams = IMigrate.MigrationParams;
6/**
7 * Promisified wrapper for the sqlite3#Database interface.
8 */
9export declare class Database<Driver extends sqlite3.Database = sqlite3.Database, Stmt extends sqlite3.Statement = sqlite3.Statement> {
10 config: ISqlite.Config;
11 db: Driver;
12 constructor(config: ISqlite.Config);
13 /**
14 * Event handler when verbose mode is enabled.
15 * @see https://github.com/mapbox/node-sqlite3/wiki/Debugging
16 */
17 on(event: string, listener: any): void;
18 /**
19 * Returns the underlying sqlite3 Database instance
20 */
21 getDatabaseInstance(): Driver;
22 /**
23 * Opens the database
24 */
25 open(): Promise<void>;
26 /**
27 * Closes the database.
28 */
29 close(): Promise<void>;
30 /**
31 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaseconfigureoption-value
32 */
33 configure(option: ISqlite.ConfigureOption, value: any): any;
34 /**
35 * Runs the SQL query with the specified parameters. It does not retrieve any result data.
36 * The function returns the Database object for which it was called to allow for function chaining.
37 *
38 * @param {string} sql The SQL query to run.
39 *
40 * @param {any} [params, ...] When the SQL statement contains placeholders, you
41 * can pass them in here. They will be bound to the statement before it is
42 * executed. There are three ways of passing bind parameters: directly in
43 * the function's arguments, as an array, and as an object for named
44 * parameters. This automatically sanitizes inputs.
45 *
46 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaserunsql-param--callback
47 */
48 run(sql: ISqlite.SqlType, ...params: any[]): Promise<ISqlite.RunResult<Stmt>>;
49 /**
50 * Runs the SQL query with the specified parameters and resolves with
51 * with the first result row afterwards. If the result set is empty, returns undefined.
52 *
53 * The property names correspond to the column names of the result set.
54 * It is impossible to access them by column index; the only supported way is by column name.
55 *
56 * @param {string} sql The SQL query to run.
57 *
58 * @param {any} [params, ...] When the SQL statement contains placeholders, you
59 * can pass them in here. They will be bound to the statement before it is
60 * executed. There are three ways of passing bind parameters: directly in
61 * the function's arguments, as an array, and as an object for named
62 * parameters. This automatically sanitizes inputs.
63 *
64 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databasegetsql-param--callback
65 */
66 get<T = any>(sql: ISqlite.SqlType, ...params: any[]): Promise<T | undefined>;
67 /**
68 * Runs the SQL query with the specified parameters and calls the callback once for each result
69 * row. The parameters are the same as the Database#run function, with the following differences:
70 *
71 * If the result set succeeds but is empty, the callback is never called.
72 * In all other cases, the callback is called once for every retrieved row.
73 * The order of calls correspond exactly to the order of rows in the result set.
74 *
75 * There is currently no way to abort execution!
76 *
77 * The last parameter to each() *must* be a callback function.
78 *
79 * @example await db.each('SELECT * FROM x WHERE y = ?', 'z', (err, row) => {
80 * // row contains the row data
81 * // each() resolves when there are no more rows to fetch
82 * })
83 *
84 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaseeachsql-param--callback-complete
85 * @returns Promise<number> Number of rows returned
86 */
87 each<T = any>(sql: ISqlite.SqlType, callback: (err: any, row: T) => void): Promise<number>;
88 each<T = any>(sql: ISqlite.SqlType, param1: any, callback: (err: any, row: T) => void): Promise<number>;
89 each<T = any>(sql: ISqlite.SqlType, param1: any, param2: any, callback: (err: any, row: T) => void): Promise<number>;
90 each<T = any>(sql: ISqlite.SqlType, param1: any, param2: any, param3: any, callback: (err: any, row: T) => void): Promise<number>;
91 each<T = any>(sql: ISqlite.SqlType, ...params: any[]): Promise<number>;
92 /**
93 * Runs the SQL query with the specified parameters. The parameters are the same as the
94 * Database#run function, with the following differences:
95 *
96 * If the result set is empty, it will be an empty array, otherwise it will
97 * have an object for each result row which
98 * in turn contains the values of that row, like the Database#get function.
99 *
100 * Note that it first retrieves all result rows and stores them in memory.
101 * For queries that have potentially large result sets, use the Database#each
102 * function to retrieve all rows or Database#prepare followed by multiple
103 * Statement#get calls to retrieve a previously unknown amount of rows.
104 *
105 * @param {string} sql The SQL query to run.
106 *
107 * @param {any} [params, ...] When the SQL statement contains placeholders, you
108 * can pass them in here. They will be bound to the statement before it is
109 * executed. There are three ways of passing bind parameters: directly in
110 * the function's arguments, as an array, and as an object for named
111 * parameters. This automatically sanitizes inputs.
112 *
113 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaseallsql-param--callback
114 */
115 all<T = any[]>(sql: ISqlite.SqlType, ...params: any[]): Promise<T>;
116 /**
117 * Runs all SQL queries in the supplied string. No result rows are retrieved. If a query fails,
118 * no subsequent statements will be executed (wrap it in a transaction if you want all
119 * or none to be executed).
120 *
121 * Note: This function will only execute statements up to the first NULL byte.
122 * Comments are not allowed and will lead to runtime errors.
123 *
124 * @param {string} sql The SQL query to run.
125 * @param {any} [params, ...] Same as the `params` parameter of `all`
126 * @see https://github.com/mapbox/node-sqlite3/wiki/API#databaseexecsql-callback
127 */
128 exec(sql: ISqlite.SqlType, ...params: any[]): Promise<void>;
129 /**
130 * Prepares the SQL statement and optionally binds the specified parameters.
131 * When bind parameters are supplied, they are bound to the prepared statement.
132 *
133 * @param {string} sql The SQL query to run.
134 * @param {any} [params, ...] When the SQL statement contains placeholders, you
135 * can pass them in here. They will be bound to the statement before it is
136 * executed. There are three ways of passing bind parameters: directly in
137 * the function's arguments, as an array, and as an object for named
138 * parameters. This automatically sanitizes inputs.
139 * @returns Promise<Statement> Statement object
140 */
141 prepare(sql: ISqlite.SqlType, ...params: any[]): Promise<Statement<Stmt>>;
142 /**
143 * Loads a compiled SQLite extension into the database connection object.
144 *
145 * @param {string} path Filename of the extension to load
146 */
147 loadExtension(path: string): Promise<void>;
148 /**
149 * Performs a database migration.
150 */
151 migrate(config?: MigrationParams): Promise<void>;
152 /**
153 * The methods underneath requires creative work to implement. PRs / proposals accepted!
154 */
155 serialize(): void;
156 parallelize(): void;
157}
158
\No newline at end of file