-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.d.ts
302 lines (275 loc) · 12.8 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import BetterSqlite3 = require("better-sqlite3");
declare namespace BetterSqlite3Helper {
type MigrationOptions = {
/** Whether to set to 'last' to automatically reapply the last migration-file. Default: false */
force?: "last" | false;
/** The name of the database table that is used to keep track. Default: 'migration' */
table?: string;
/** The path of the migration files. Default: './migrations' */
migrationsPath?: string;
/** Or an array of migration strings */
migrations?: string[];
};
type DBOptions = {
/** Path to sqlite database file. Default: './data/sqlite3.db' */
path?: string;
/** Whether to create a db only in memory. Default: false */
memory?: boolean;
/** Whether to open database readonly. Default: false */
readonly?: boolean;
/** Whether to throw error if database not exists. Default: false */
fileMustExist?: boolean;
/** Whether to automatically enable 'PRAGMA journal_mode = WAL'. Default: true */
WAL?: boolean;
/** Migration options. Disable completely by setting `migrate: false` */
migrate?: MigrationOptions | false;
};
type DataObject = { [key: string]: any };
/**
* Specifies a where clause.
*
* - Either a string containing the value to use as ID that will be translated to ['id = ?', id]
* - Or an array with a string and the replacements for ? after that. F.e. ['id > ? && name = ?', id, name].
* - Or an object with key values. F.e. {id: params.id}. Or simply an ID that will be translated to ['id = ?', id]
*/
type WhereClause = string | any[] | DataObject;
type VariableArgFunction = (...params: any[]) => any;
type ArgumentTypes<F extends VariableArgFunction> = F extends (
...args: infer A
) => any
? A
: never;
interface Transaction<F extends VariableArgFunction> {
(...params: ArgumentTypes<F>): any;
default(...params: ArgumentTypes<F>): any;
deferred(...params: ArgumentTypes<F>): any;
immediate(...params: ArgumentTypes<F>): any;
exclusive(...params: ArgumentTypes<F>): any;
}
interface DBInstance {
memory: boolean;
readonly: boolean;
name: string;
open: boolean;
inTransaction: boolean;
connection(): BetterSqlite3.Database;
prepare(sql: string): BetterSqlite3.Statement;
transaction<F extends VariableArgFunction>(fn: F): Transaction<F>;
exec(sqls: string): this;
pragma(string: string, options?: BetterSqlite3.PragmaOptions): any;
checkpoint(databaseName?: string): this;
function(name: string, cb: (...params: any[]) => any): this;
function(
name: string,
options: BetterSqlite3.RegistrationOptions,
cb: (...params: any[]) => any
): this;
aggregate(name: string, options: BetterSqlite3.AggregateOptions): this;
loadExtension(path: string): this;
close(): this;
defaultSafeIntegers(toggleState?: boolean): this;
/**
* Executes the prepared statement. When execution completes it returns an info object describing any changes made. The info object has two properties:
*
* info.changes: The total number of rows that were inserted, updated, or deleted by this operation. Changes made by foreign key actions or trigger programs do not count.
* info.lastInsertRowid: The rowid of the last row inserted into the database (ignoring those caused by trigger programs). If the current statement did not insert any rows into the database, this number should be completely ignored.
*
* If execution of the statement fails, an Error is thrown.
* @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#runbindparameters---object
*
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#binding-parameters
* @returns {Object}
*/
run(query: string, ...bindParameters: any[]): BetterSqlite3.RunResult;
/**
* Returns all values of a query
* @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#allbindparameters---array-of-rows
*
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {any} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#binding-parameters
* @returns {array}
*/
query<RowData = DataObject>(
query: string,
...bindParameters: any[]
): RowData[];
/**
* Similar to .query(), but instead of returning every row together, an iterator is returned so you can retrieve the rows one by one.
* @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#iteratebindparameters---iterator
*
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/wiki/API#binding-parameters
* @returns {Iterator}
*/
queryIterate<RowData = DataObject>(
query: string,
...bindParameters: any[]
): Iterable<RowData>;
/**
* Returns the values of the first row of the query-result
* @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#getbindparameters---row
*
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
* @returns {Object|undefined}
*/
queryFirstRow<RowData = DataObject>(
query: string,
...bindParameters: any[]
): RowData | undefined;
/**
* Returns the values of the first row of the query-result
* @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#getbindparameters---row
* It returns always an object and thus can be used with destructuring assignment
*
* @example const {id, name} = DB().queryFirstRowObject(sql)
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
* @returns {Object}
*/
queryFirstRowObject<RowData = DataObject>(
query: string,
...bindParameters: any[]
): RowData | Object;
/**
* Returns the value of the first column in the first row of the query-result
*
* @param {Object} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
* @returns {*}
*/
queryFirstCell<CellType = any>(
query: string,
...bindParameters: any[]
): CellType | undefined;
/**
* Returns an Array that only contains the values of the specified column
*
* @param {String} column Name of the column
* @param {String} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
* @returns {Array}
*/
queryColumn<ColumnType = any>(
column: string,
query: string,
...bindParameters: any[]
): ColumnType[];
/**
* Returns a Object that get it key-value-combination from the result of the query
*
* @param {String} key Name of the column that values should be the key
* @param {String} column Name of the column that values should be the value for the object
* @param {String} query the SQL-Query that should be run. Can contain placeholders for bind parameters.
* @param {*} bindParameters You can specify bind parameters @see https://github.com/JoshuaWise/better-sqlite3/blob/master/docs/api.md#binding-parameters
* @returns {object}
*/
queryKeyAndColumn<ValueColumnType = any>(
key: string,
column: string,
query: string,
...bindParameters: any[]
): { [key: string]: ValueColumnType };
/**
* Create an update statement; create more complex one with exec yourself.
*
* @param {String} table required. Name of the table
* @param {Object} data A Object of data to set. Key is the name of the column. Value 'undefined' is filtered
* @param {String|Array|Object} where required. array with a string and the replacements for ? after that. F.e. ['id > ? && name = ?', id, name]. Or an object with key values. F.e. {id: params.id}.
* @param {undefined|Array} whiteList optional List of columns that can only be updated with "data"
* @returns {Integer} Number of changed rows
*/
update<RowData = DataObject>(
table: string,
data: Partial<RowData>,
where: WhereClause,
whiteList?: string[]
): number;
/**
* Create an update statement; create more complex one with exec yourself.
*
* @param {String} table Name of the table
* @param {Object} data a Object of data to set. Key is the name of the column. Value 'undefined' is filtered
* @param {String|Array|Object} where required. array with a string and the replacements for ? after that. F.e. ['id > ? && name = ?', id, name]. Or an object with key values. F.e. {id: params.id}.
* @param {undefined|Array} whiteBlackList optional List of columns that can not be updated with "data" (blacklist)
* @returns {Integer} Number of changed rows
*/
updateWithBlackList<RowData = DataObject>(
table: string,
data: Partial<RowData>,
where: WhereClause,
blackList?: string[]
): number;
/**
* Create an insert statement; create more complex one with exec yourself.
*
* @param {String} table Name of the table
* @param {Object|Array} data a Object of data to set. Key is the name of the column. Can be an array of objects.
* @param {undefined|Array} whiteList optional List of columns that only can be updated with "data"
* @returns {Integer} Last inserted row id
*/
insert<RowData = DataObject>(
table: string,
data: Partial<RowData> | Partial<RowData>[],
whiteList?: string[]
): number;
/**
* Create an insert statement; create more complex one with exec yourself.
*
* @param {String} table Name of the table
* @param {Object|Array} data a Object of data to set. Key is the name of the column. Can be an array of objects.
* @param {undefined|Array} whiteBlackList optional List of columns that can not be updated with "data" (blacklist)
* @returns {Integer} Last inserted row id
*/
insertWithBlackList<RowData = DataObject>(
table: string,
data: Partial<RowData> | Partial<RowData>[],
blackList?: string[]
): number;
/**
* Create an replace statement; create more complex one with exec yourself.
*
* @param {String} table Name of the table
* @param {Object|Array} data a Object of data to set. Key is the name of the column. Can be an array of objects.
* @param {undefined|Array} whiteList optional List of columns that only can be updated with "data"
* @returns {Integer} Last inserted row id
*/
replace<RowData = DataObject>(
table: string,
data: Partial<RowData> | Partial<RowData>[],
whiteList?: string[]
): number;
/**
* Create an replace statement; create more complex one with exec yourself.
*
* @param {String} table Name of the table
* @param {Object|Array} data a Object of data to set. Key is the name of the column. Can be an array of objects.
* @param {undefined|Array} whiteBlackList optional List of columns that can not be updated with "data" (blacklist)
* @returns {Integer} Last inserted row id
*/
replaceWithBlackList<RowData = DataObject>(
table: string,
data: Partial<RowData> | Partial<RowData>[],
blackList?: string[]
): number;
/**
* Create a delete statement; create more complex one with exec yourself.
*
* @param {String} table required. Name of the table
* @param {String|Array|Object} where required. array with a string and the replacements for ? after that. F.e. ['id > ? && name = ?', id, name]. Or an object with key values. F.e. {id: params.id}.
* @returns {Integer} Number of changed rows
*/
delete(
table: string,
where: WhereClause,
): number;
/**
* Migrates database schema to the latest version
*/
migrate(options?: MigrationOptions): this;
}
}
export default function DB(
options?: BetterSqlite3Helper.DBOptions
): BetterSqlite3Helper.DBInstance;