Skip to content

Commit e37d057

Browse files
Christiaan LandmanChristiaan Landman
Christiaan Landman
authored and
Christiaan Landman
committed
Updated uuid dependency and updated uuid import to work in react native
1 parent 7b56453 commit e37d057

File tree

3 files changed

+75
-33
lines changed

3 files changed

+75
-33
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"devDependencies": {
5050
"@changesets/cli": "^2.26.2",
5151
"@types/lodash": "^4.14.200",
52-
"@types/uuid": "3.4.0",
52+
"@types/uuid": "^9.0.1",
5353
"react": "18.2.0",
5454
"react-native": "0.71.0",
5555
"react-native-builder-bob": "^0.18.2",
@@ -82,6 +82,6 @@
8282
},
8383
"dependencies": {
8484
"lodash": "^4.17.21",
85-
"uuid": "3.4.0"
85+
"uuid": "^9.0.1"
8686
}
8787
}

src/setup-open.ts

+72-30
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
UpdateCallback,
1010
SQLBatchTuple,
1111
OpenOptions,
12-
QueryResult
12+
QueryResult,
1313
} from './types';
1414

15-
import uuid from 'uuid';
15+
import { v4 } from 'uuid';
1616
import _ from 'lodash';
1717
import { enhanceQueryResult } from './utils';
1818
import { DBListenerManagerInternal } from './DBListenerManager';
@@ -25,7 +25,7 @@ type LockCallbackRecord = {
2525

2626
enum TransactionFinalizer {
2727
COMMIT = 'commit',
28-
ROLLBACK = 'rollback'
28+
ROLLBACK = 'rollback',
2929
}
3030

3131
const DEFAULT_READ_CONNECTIONS = 4;
@@ -49,7 +49,10 @@ function closeContextLock(dbName: string, id: ContextLockID) {
4949
* @param lockId
5050
* @returns
5151
*/
52-
global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID) => {
52+
global.onLockContextIsAvailable = async (
53+
dbName: string,
54+
lockId: ContextLockID
55+
) => {
5356
// Don't hold C++ bridge side up waiting to complete
5457
setImmediate(async () => {
5558
try {
@@ -61,10 +64,15 @@ global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID)
6164
// @ts-expect-error This is not part of the public interface, but is used internally
6265
_contextId: lockId,
6366
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+
);
6573
enhanceQueryResult(result);
6674
return result;
67-
}
75+
},
6876
});
6977
} catch (ex) {
7078
console.error(ex);
@@ -87,11 +95,15 @@ export function setupOpen(QuickSQLite: ISQLite) {
8795
* By default opens DB in WAL mode with 4 Read connections and a single
8896
* write connection
8997
*/
90-
open: (dbName: string, options: OpenOptions = {}): QuickSQLiteConnection => {
98+
open: (
99+
dbName: string,
100+
options: OpenOptions = {}
101+
): QuickSQLiteConnection => {
91102
// Opens the connection
92103
QuickSQLite.open(dbName, {
93104
...options,
94-
numReadConnections: options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS
105+
numReadConnections:
106+
options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS,
95107
});
96108

97109
const listenerManager = new DBListenerManagerInternal({ dbName });
@@ -106,7 +118,7 @@ export function setupOpen(QuickSQLite: ISQLite) {
106118
options?: LockOptions,
107119
hooks?: LockHooks
108120
): Promise<T> => {
109-
const id = uuid.v4(); // TODO maybe do this in C++
121+
const id = v4(); // TODO maybe do this in C++
110122
// Wrap the callback in a promise that will resolve to the callback result
111123
return new Promise<T>((resolve, reject) => {
112124
// Add callback to the queue for timing
@@ -117,14 +129,14 @@ export function setupOpen(QuickSQLite: ISQLite) {
117129
const res = await callback(context);
118130

119131
closeContextLock(dbName, id);
120-
resolve(res)
132+
resolve(res);
121133
} catch (ex) {
122134
closeContextLock(dbName, id);
123-
reject(ex)
135+
reject(ex);
124136
} finally {
125-
hooks?.lockReleased?.()
137+
hooks?.lockReleased?.();
126138
}
127-
}
139+
},
128140
} as LockCallbackRecord);
129141

130142
try {
@@ -145,15 +157,20 @@ export function setupOpen(QuickSQLite: ISQLite) {
145157
});
146158
};
147159

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);
150164

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> =>
152169
requestLock(ConcurrentLockType.WRITE, callback, options, {
153170
lockReleased: async () => {
154171
// flush updates once a write lock has been released
155172
listenerManager.flushUpdates();
156-
}
173+
},
157174
});
158175

159176
const wrapTransaction = async <T>(
@@ -174,17 +191,23 @@ export function setupOpen(QuickSQLite: ISQLite) {
174191
return action();
175192
};
176193

177-
const commit = finalizedStatement(async () => context.execute('COMMIT'));
194+
const commit = finalizedStatement(async () =>
195+
context.execute('COMMIT')
196+
);
178197

179-
const rollback = finalizedStatement(async () => context.execute('ROLLBACK'));
198+
const rollback = finalizedStatement(async () =>
199+
context.execute('ROLLBACK')
200+
);
180201

181202
const wrapExecute =
182203
<T>(
183204
method: (sql: string, params?: any[]) => Promise<QueryResult>
184205
): ((sql: string, params?: any[]) => Promise<QueryResult>) =>
185206
async (sql: string, params?: any[]) => {
186207
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+
);
188211
}
189212
return method(sql, params);
190213
};
@@ -194,7 +217,7 @@ export function setupOpen(QuickSQLite: ISQLite) {
194217
...context,
195218
commit,
196219
rollback,
197-
execute: wrapExecute(context.execute)
220+
execute: wrapExecute(context.execute),
198221
});
199222
switch (defaultFinalizer) {
200223
case TransactionFinalizer.COMMIT:
@@ -214,26 +237,45 @@ export function setupOpen(QuickSQLite: ISQLite) {
214237
// Return the concurrent connection object
215238
return {
216239
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)),
218242
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)),
221247
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+
),
224257
delete: () => QuickSQLite.delete(dbName, options?.location),
225258
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+
),
227266
attach: (dbNameToAttach: string, alias: string, location?: string) =>
228267
QuickSQLite.attach(dbName, dbNameToAttach, alias, location),
229268
detach: (alias: string) => QuickSQLite.detach(dbName, alias),
230269
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+
),
232273
listenerManager,
233274
registerUpdateHook: (callback: UpdateCallback) =>
234275
listenerManager.registerListener({ rawTableChange: callback }),
235-
registerTablesChangedHook: (callback) => listenerManager.registerListener({ tablesUpdated: callback })
276+
registerTablesChangedHook: (callback) =>
277+
listenerManager.registerListener({ tablesUpdated: callback }),
236278
};
237-
}
279+
},
238280
};
239281
}

tests/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"tailwindcss": "^3.2.4",
3434
"typeorm": "^0.3.11",
3535
"util": "^0.12.5",
36-
"uuid": "3.4.0"
36+
"uuid": "^9.0.1"
3737
},
3838
"devDependencies": {
3939
"@babel/core": "^7.20.0",

0 commit comments

Comments
 (0)