@op-engineering/op-sqlite 15.0.1 → 15.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,314 @@
1
+ export type Scalar =
2
+ | string
3
+ | number
4
+ | boolean
5
+ | null
6
+ | ArrayBuffer
7
+ | ArrayBufferView;
8
+
9
+ /**
10
+ * Object returned by SQL Query executions {
11
+ * insertId: Represent the auto-generated row id if applicable
12
+ * rowsAffected: Number of affected rows if result of a update query
13
+ * message: if status === 1, here you will find error description
14
+ * rows: if status is undefined or 0 this object will contain the query results
15
+ * }
16
+ *
17
+ * @interface QueryResult
18
+ */
19
+ export type QueryResult = {
20
+ insertId?: number;
21
+ rowsAffected: number;
22
+ res?: any[];
23
+ rows: Array<Record<string, Scalar>>;
24
+ // An array of intermediate results, just values without column names
25
+ rawRows?: Scalar[][];
26
+ columnNames?: string[];
27
+ /**
28
+ * Query metadata, available only for select query results
29
+ */
30
+ metadata?: ColumnMetadata[];
31
+ };
32
+
33
+ /**
34
+ * Column metadata
35
+ * Describes some information about columns fetched by the query
36
+ */
37
+ export type ColumnMetadata = {
38
+ /** The name used for this column for this result set */
39
+ name: string;
40
+ /** The declared column type for this column, when fetched directly from a table or a View resulting from a table column. "UNKNOWN" for dynamic values, like function returned ones. */
41
+ type: string;
42
+ /**
43
+ * The index for this column for this result set*/
44
+ index: number;
45
+ };
46
+
47
+ /**
48
+ * Allows the execution of bulk of sql commands
49
+ * inside a transaction
50
+ * If a single query must be executed many times with different arguments, its preferred
51
+ * to declare it a single time, and use an array of array parameters.
52
+ */
53
+ export type SQLBatchTuple =
54
+ | [string]
55
+ | [string, Scalar[]]
56
+ | [string, Scalar[][]];
57
+
58
+ export type UpdateHookOperation = 'INSERT' | 'DELETE' | 'UPDATE';
59
+
60
+ /**
61
+ * status: 0 or undefined for correct execution, 1 for error
62
+ * message: if status === 1, here you will find error description
63
+ * rowsAffected: Number of affected rows if status == 0
64
+ */
65
+ export type BatchQueryResult = {
66
+ rowsAffected?: number;
67
+ };
68
+
69
+ /**
70
+ * Result of loading a file and executing every line as a SQL command
71
+ * Similar to BatchQueryResult
72
+ */
73
+ export type FileLoadResult = BatchQueryResult & {
74
+ commands?: number;
75
+ };
76
+
77
+ export type Transaction = {
78
+ commit: () => Promise<QueryResult>;
79
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
80
+ rollback: () => QueryResult;
81
+ };
82
+
83
+ export type _PendingTransaction = {
84
+ /*
85
+ * The start function should not throw or return a promise because the
86
+ * queue just calls it and does not monitor for failures or completions.
87
+ *
88
+ * It should catch any errors and call the resolve or reject of the wrapping
89
+ * promise when complete.
90
+ *
91
+ * It should also automatically commit or rollback the transaction if needed
92
+ */
93
+ start: () => void;
94
+ };
95
+
96
+ export type PreparedStatement = {
97
+ bind: (params: any[]) => Promise<void>;
98
+ bindSync: (params: any[]) => void;
99
+ execute: () => Promise<QueryResult>;
100
+ };
101
+
102
+ export type _InternalDB = {
103
+ close: () => void;
104
+ delete: (location?: string) => void;
105
+ attach: (params: {
106
+ secondaryDbFileName: string;
107
+ alias: string;
108
+ location?: string;
109
+ }) => void;
110
+ detach: (alias: string) => void;
111
+ transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
112
+ executeSync: (query: string, params?: Scalar[]) => QueryResult;
113
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
114
+ executeWithHostObjects: (
115
+ query: string,
116
+ params?: Scalar[]
117
+ ) => Promise<QueryResult>;
118
+ executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
119
+ loadFile: (location: string) => Promise<FileLoadResult>;
120
+ updateHook: (
121
+ callback?:
122
+ | ((params: {
123
+ table: string;
124
+ operation: UpdateHookOperation;
125
+ row?: any;
126
+ rowId: number;
127
+ }) => void)
128
+ | null
129
+ ) => void;
130
+ commitHook: (callback?: (() => void) | null) => void;
131
+ rollbackHook: (callback?: (() => void) | null) => void;
132
+ prepareStatement: (query: string) => PreparedStatement;
133
+ loadExtension: (path: string, entryPoint?: string) => void;
134
+ executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
135
+ executeRawSync: (query: string, params?: Scalar[]) => any[];
136
+ getDbPath: (location?: string) => string;
137
+ reactiveExecute: (params: {
138
+ query: string;
139
+ arguments: any[];
140
+ fireOn: {
141
+ table: string;
142
+ ids?: number[];
143
+ }[];
144
+ callback: (response: any) => void;
145
+ }) => () => void;
146
+ sync: () => void;
147
+ flushPendingReactiveQueries: () => Promise<void>;
148
+ };
149
+
150
+ export type DB = {
151
+ close: () => void;
152
+ delete: (location?: string) => void;
153
+ attach: (params: {
154
+ secondaryDbFileName: string;
155
+ alias: string;
156
+ location?: string;
157
+ }) => void;
158
+ detach: (alias: string) => void;
159
+ /**
160
+ * Wraps all the executions into a transaction. If an error is thrown it will rollback all of the changes
161
+ *
162
+ * You need to use this if you are using reactive queries for the queries to fire after the transaction is done
163
+ */
164
+ transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>;
165
+ /**
166
+ * Sync version of the execute function
167
+ * It will block the JS thread and therefore your UI and should be used with caution
168
+ *
169
+ * When writing your queries, you can use the ? character as a placeholder for parameters
170
+ * The parameters will be automatically escaped and sanitized
171
+ *
172
+ * Example:
173
+ * db.executeSync('SELECT * FROM table WHERE id = ?', [1]);
174
+ *
175
+ * If you are writing a query that doesn't require parameters, you can omit the second argument
176
+ *
177
+ * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
178
+ * Transactions protect you from partial writes and ensure that your data is always in a consistent state
179
+ *
180
+ * @param query
181
+ * @param params
182
+ * @returns QueryResult
183
+ */
184
+ executeSync: (query: string, params?: Scalar[]) => QueryResult;
185
+ /**
186
+ * Basic query execution function, it is async don't forget to await it
187
+ *
188
+ * When writing your queries, you can use the ? character as a placeholder for parameters
189
+ * The parameters will be automatically escaped and sanitized
190
+ *
191
+ * Example:
192
+ * await db.execute('SELECT * FROM table WHERE id = ?', [1]);
193
+ *
194
+ * If you are writing a query that doesn't require parameters, you can omit the second argument
195
+ *
196
+ * If you are writing to the database YOU SHOULD BE USING TRANSACTIONS!
197
+ * Transactions protect you from partial writes and ensure that your data is always in a consistent state
198
+ *
199
+ * If you need a large amount of queries ran as fast as possible you should be using `executeBatch`, `executeRaw`, `loadFile` or `executeWithHostObjects`
200
+ *
201
+ * @param query string of your SQL query
202
+ * @param params a list of parameters to bind to the query, if any
203
+ * @returns Promise<QueryResult> with the result of the query
204
+ */
205
+ execute: (query: string, params?: Scalar[]) => Promise<QueryResult>;
206
+ /**
207
+ * Similar to the execute function but returns the response in HostObjects
208
+ * Read more about HostObjects in the documentation and their pitfalls
209
+ *
210
+ * Will be a lot faster than the normal execute functions when returning data but you will pay when accessing the fields
211
+ * as the conversion is done the moment you access any field
212
+ * @param query
213
+ * @param params
214
+ * @returns
215
+ */
216
+ executeWithHostObjects: (
217
+ query: string,
218
+ params?: Scalar[]
219
+ ) => Promise<QueryResult>;
220
+ /**
221
+ * Executes all the queries in the params inside a single transaction
222
+ *
223
+ * It's faster than executing single queries as data is sent to the native side only once
224
+ * @param commands
225
+ * @returns Promise<BatchQueryResult>
226
+ */
227
+ executeBatch: (commands: SQLBatchTuple[]) => Promise<BatchQueryResult>;
228
+ /**
229
+ * Loads a SQLite Dump from disk. It will be the fastest way to execute a large set of queries as no JS is involved
230
+ */
231
+ loadFile: (location: string) => Promise<FileLoadResult>;
232
+ updateHook: (
233
+ callback?:
234
+ | ((params: {
235
+ table: string;
236
+ operation: UpdateHookOperation;
237
+ row?: any;
238
+ rowId: number;
239
+ }) => void)
240
+ | null
241
+ ) => void;
242
+ commitHook: (callback?: (() => void) | null) => void;
243
+ rollbackHook: (callback?: (() => void) | null) => void;
244
+ /**
245
+ * Constructs a prepared statement from the query string
246
+ * The statement can be re-bound with parameters and executed
247
+ * The performance gain is significant when the same query is executed multiple times, NOT when the query is executed (once)
248
+ * The cost lies in the preparation of the statement as it is compiled and optimized by the sqlite engine, the params can then rebound
249
+ * but the query itself is already optimized
250
+ *
251
+ * @param query string of your SQL query
252
+ * @returns Prepared statement object
253
+ */
254
+ prepareStatement: (query: string) => PreparedStatement;
255
+ /**
256
+ * Loads a runtime loadable sqlite extension. Libsql and iOS embedded version do not support loading extensions
257
+ */
258
+ loadExtension: (path: string, entryPoint?: string) => void;
259
+ /**
260
+ * Same as `execute` except the results are not returned in objects but rather in arrays with just the values and not the keys
261
+ * It will be faster since a lot of repeated work is skipped and only the values you care about are returned
262
+ */
263
+ executeRaw: (query: string, params?: Scalar[]) => Promise<any[]>;
264
+ /**
265
+ * Same as `executeRaw` but it will block the JS thread and therefore your UI and should be used with caution
266
+ * It will return an array of arrays with just the values and not the keys
267
+ */
268
+ executeRawSync: (query: string, params?: Scalar[]) => any[];
269
+ /**
270
+ * Get's the absolute path to the db file. Useful for debugging on local builds and for attaching the DB from users devices
271
+ */
272
+ getDbPath: (location?: string) => string;
273
+ /**
274
+ * Reactive execution of queries when data is written to the database. Check the docs for how to use them.
275
+ */
276
+ reactiveExecute: (params: {
277
+ query: string;
278
+ arguments: any[];
279
+ fireOn: {
280
+ table: string;
281
+ ids?: number[];
282
+ }[];
283
+ callback: (response: any) => void;
284
+ }) => () => void;
285
+ /** This function is only available for libsql.
286
+ * Allows to trigger a sync the database with it's remote replica
287
+ * In order for this function to work you need to use openSync or openRemote functions
288
+ * with libsql: true in the package.json
289
+ *
290
+ * The database is hosted in turso
291
+ **/
292
+ sync: () => void;
293
+ };
294
+
295
+ export type DBParams = {
296
+ url?: string;
297
+ authToken?: string;
298
+ name?: string;
299
+ location?: string;
300
+ syncInterval?: number;
301
+ };
302
+
303
+ export type OPSQLiteProxy = {
304
+ open: (options: {
305
+ name: string;
306
+ location?: string;
307
+ encryptionKey?: string;
308
+ }) => _InternalDB;
309
+ openRemote: (options: { url: string; authToken: string }) => _InternalDB;
310
+ openSync: (options: DBParams) => _InternalDB;
311
+ isSQLCipher: () => boolean;
312
+ isLibsql: () => boolean;
313
+ isIOSEmbedded: () => boolean;
314
+ };