UNPKG

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