@@ -9,10 +9,10 @@ import {
9
9
UpdateCallback ,
10
10
SQLBatchTuple ,
11
11
OpenOptions ,
12
- QueryResult
12
+ QueryResult ,
13
13
} from './types' ;
14
14
15
- import uuid from 'uuid' ;
15
+ import { v4 } from 'uuid' ;
16
16
import _ from 'lodash' ;
17
17
import { enhanceQueryResult } from './utils' ;
18
18
import { DBListenerManagerInternal } from './DBListenerManager' ;
@@ -25,7 +25,7 @@ type LockCallbackRecord = {
25
25
26
26
enum TransactionFinalizer {
27
27
COMMIT = 'commit' ,
28
- ROLLBACK = 'rollback'
28
+ ROLLBACK = 'rollback' ,
29
29
}
30
30
31
31
const DEFAULT_READ_CONNECTIONS = 4 ;
@@ -49,7 +49,10 @@ function closeContextLock(dbName: string, id: ContextLockID) {
49
49
* @param lockId
50
50
* @returns
51
51
*/
52
- global . onLockContextIsAvailable = async ( dbName : string , lockId : ContextLockID ) => {
52
+ global . onLockContextIsAvailable = async (
53
+ dbName : string ,
54
+ lockId : ContextLockID
55
+ ) => {
53
56
// Don't hold C++ bridge side up waiting to complete
54
57
setImmediate ( async ( ) => {
55
58
try {
@@ -61,10 +64,15 @@ global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID)
61
64
// @ts -expect-error This is not part of the public interface, but is used internally
62
65
_contextId : lockId ,
63
66
execute : async ( sql : string , args ?: any [ ] ) => {
64
- const result = await proxy . executeInContext ( dbName , lockId , sql , args ) ;
67
+ const result = await proxy . executeInContext (
68
+ dbName ,
69
+ lockId ,
70
+ sql ,
71
+ args
72
+ ) ;
65
73
enhanceQueryResult ( result ) ;
66
74
return result ;
67
- }
75
+ } ,
68
76
} ) ;
69
77
} catch ( ex ) {
70
78
console . error ( ex ) ;
@@ -87,11 +95,15 @@ export function setupOpen(QuickSQLite: ISQLite) {
87
95
* By default opens DB in WAL mode with 4 Read connections and a single
88
96
* write connection
89
97
*/
90
- open : ( dbName : string , options : OpenOptions = { } ) : QuickSQLiteConnection => {
98
+ open : (
99
+ dbName : string ,
100
+ options : OpenOptions = { }
101
+ ) : QuickSQLiteConnection => {
91
102
// Opens the connection
92
103
QuickSQLite . open ( dbName , {
93
104
...options ,
94
- numReadConnections : options ?. numReadConnections ?? DEFAULT_READ_CONNECTIONS
105
+ numReadConnections :
106
+ options ?. numReadConnections ?? DEFAULT_READ_CONNECTIONS ,
95
107
} ) ;
96
108
97
109
const listenerManager = new DBListenerManagerInternal ( { dbName } ) ;
@@ -106,7 +118,7 @@ export function setupOpen(QuickSQLite: ISQLite) {
106
118
options ?: LockOptions ,
107
119
hooks ?: LockHooks
108
120
) : Promise < T > => {
109
- const id = uuid . v4 ( ) ; // TODO maybe do this in C++
121
+ const id = v4 ( ) ; // TODO maybe do this in C++
110
122
// Wrap the callback in a promise that will resolve to the callback result
111
123
return new Promise < T > ( ( resolve , reject ) => {
112
124
// Add callback to the queue for timing
@@ -117,14 +129,14 @@ export function setupOpen(QuickSQLite: ISQLite) {
117
129
const res = await callback ( context ) ;
118
130
119
131
closeContextLock ( dbName , id ) ;
120
- resolve ( res )
132
+ resolve ( res ) ;
121
133
} catch ( ex ) {
122
134
closeContextLock ( dbName , id ) ;
123
- reject ( ex )
135
+ reject ( ex ) ;
124
136
} finally {
125
- hooks ?. lockReleased ?.( )
137
+ hooks ?. lockReleased ?.( ) ;
126
138
}
127
- }
139
+ } ,
128
140
} as LockCallbackRecord ) ;
129
141
130
142
try {
@@ -145,15 +157,20 @@ export function setupOpen(QuickSQLite: ISQLite) {
145
157
} ) ;
146
158
} ;
147
159
148
- const readLock = < T > ( callback : ( context : LockContext ) => Promise < T > , options ?: LockOptions ) : Promise < T > =>
149
- requestLock ( ConcurrentLockType . READ , callback , options ) ;
160
+ const readLock = < T > (
161
+ callback : ( context : LockContext ) => Promise < T > ,
162
+ options ?: LockOptions
163
+ ) : Promise < T > => requestLock ( ConcurrentLockType . READ , callback , options ) ;
150
164
151
- const writeLock = < T > ( callback : ( context : LockContext ) => Promise < T > , options ?: LockOptions ) : Promise < T > =>
165
+ const writeLock = < T > (
166
+ callback : ( context : LockContext ) => Promise < T > ,
167
+ options ?: LockOptions
168
+ ) : Promise < T > =>
152
169
requestLock ( ConcurrentLockType . WRITE , callback , options , {
153
170
lockReleased : async ( ) => {
154
171
// flush updates once a write lock has been released
155
172
listenerManager . flushUpdates ( ) ;
156
- }
173
+ } ,
157
174
} ) ;
158
175
159
176
const wrapTransaction = async < T > (
@@ -174,17 +191,23 @@ export function setupOpen(QuickSQLite: ISQLite) {
174
191
return action ( ) ;
175
192
} ;
176
193
177
- const commit = finalizedStatement ( async ( ) => context . execute ( 'COMMIT' ) ) ;
194
+ const commit = finalizedStatement ( async ( ) =>
195
+ context . execute ( 'COMMIT' )
196
+ ) ;
178
197
179
- const rollback = finalizedStatement ( async ( ) => context . execute ( 'ROLLBACK' ) ) ;
198
+ const rollback = finalizedStatement ( async ( ) =>
199
+ context . execute ( 'ROLLBACK' )
200
+ ) ;
180
201
181
202
const wrapExecute =
182
203
< T > (
183
204
method : ( sql : string , params ?: any [ ] ) => Promise < QueryResult >
184
205
) : ( ( sql : string , params ?: any [ ] ) => Promise < QueryResult > ) =>
185
206
async ( sql : string , params ?: any [ ] ) => {
186
207
if ( finalized ) {
187
- throw new Error ( `Cannot execute in transaction after it has been finalized with commit/rollback.` ) ;
208
+ throw new Error (
209
+ `Cannot execute in transaction after it has been finalized with commit/rollback.`
210
+ ) ;
188
211
}
189
212
return method ( sql , params ) ;
190
213
} ;
@@ -194,7 +217,7 @@ export function setupOpen(QuickSQLite: ISQLite) {
194
217
...context ,
195
218
commit,
196
219
rollback,
197
- execute : wrapExecute ( context . execute )
220
+ execute : wrapExecute ( context . execute ) ,
198
221
} ) ;
199
222
switch ( defaultFinalizer ) {
200
223
case TransactionFinalizer . COMMIT :
@@ -214,26 +237,45 @@ export function setupOpen(QuickSQLite: ISQLite) {
214
237
// Return the concurrent connection object
215
238
return {
216
239
close : ( ) => QuickSQLite . close ( dbName ) ,
217
- execute : ( sql : string , args ?: any [ ] ) => writeLock ( ( context ) => context . execute ( sql , args ) ) ,
240
+ execute : ( sql : string , args ?: any [ ] ) =>
241
+ writeLock ( ( context ) => context . execute ( sql , args ) ) ,
218
242
readLock,
219
- readTransaction : async < T > ( callback : ( context : TransactionContext ) => Promise < T > , options ?: LockOptions ) =>
220
- readLock ( ( context ) => wrapTransaction ( context , callback ) ) ,
243
+ readTransaction : async < T > (
244
+ callback : ( context : TransactionContext ) => Promise < T > ,
245
+ options ?: LockOptions
246
+ ) => readLock ( ( context ) => wrapTransaction ( context , callback ) ) ,
221
247
writeLock,
222
- writeTransaction : async < T > ( callback : ( context : TransactionContext ) => Promise < T > , options ?: LockOptions ) =>
223
- writeLock ( ( context ) => wrapTransaction ( context , callback , TransactionFinalizer . COMMIT ) , options ) ,
248
+ writeTransaction : async < T > (
249
+ callback : ( context : TransactionContext ) => Promise < T > ,
250
+ options ?: LockOptions
251
+ ) =>
252
+ writeLock (
253
+ ( context ) =>
254
+ wrapTransaction ( context , callback , TransactionFinalizer . COMMIT ) ,
255
+ options
256
+ ) ,
224
257
delete : ( ) => QuickSQLite . delete ( dbName , options ?. location ) ,
225
258
executeBatch : ( commands : SQLBatchTuple [ ] ) =>
226
- writeLock ( ( context ) => QuickSQLite . executeBatch ( dbName , commands , ( context as any ) . _contextId ) ) ,
259
+ writeLock ( ( context ) =>
260
+ QuickSQLite . executeBatch (
261
+ dbName ,
262
+ commands ,
263
+ ( context as any ) . _contextId
264
+ )
265
+ ) ,
227
266
attach : ( dbNameToAttach : string , alias : string , location ?: string ) =>
228
267
QuickSQLite . attach ( dbName , dbNameToAttach , alias , location ) ,
229
268
detach : ( alias : string ) => QuickSQLite . detach ( dbName , alias ) ,
230
269
loadFile : ( location : string ) =>
231
- writeLock ( ( context ) => QuickSQLite . loadFile ( dbName , location , ( context as any ) . _contextId ) ) ,
270
+ writeLock ( ( context ) =>
271
+ QuickSQLite . loadFile ( dbName , location , ( context as any ) . _contextId )
272
+ ) ,
232
273
listenerManager,
233
274
registerUpdateHook : ( callback : UpdateCallback ) =>
234
275
listenerManager . registerListener ( { rawTableChange : callback } ) ,
235
- registerTablesChangedHook : ( callback ) => listenerManager . registerListener ( { tablesUpdated : callback } )
276
+ registerTablesChangedHook : ( callback ) =>
277
+ listenerManager . registerListener ( { tablesUpdated : callback } ) ,
236
278
} ;
237
- }
279
+ } ,
238
280
} ;
239
281
}
0 commit comments