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