From e37d0578bc71e4c2f275e58458e7c6474e8afe9a Mon Sep 17 00:00:00 2001 From: Christiaan Landman Date: Wed, 17 Apr 2024 11:54:14 +0200 Subject: [PATCH 1/4] Updated uuid dependency and updated uuid import to work in react native --- package.json | 4 +- src/setup-open.ts | 102 ++++++++++++++++++++++++++++++++------------- tests/package.json | 2 +- 3 files changed, 75 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index 0c83f9f..9ab589c 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "devDependencies": { "@changesets/cli": "^2.26.2", "@types/lodash": "^4.14.200", - "@types/uuid": "3.4.0", + "@types/uuid": "^9.0.1", "react": "18.2.0", "react-native": "0.71.0", "react-native-builder-bob": "^0.18.2", @@ -82,6 +82,6 @@ }, "dependencies": { "lodash": "^4.17.21", - "uuid": "3.4.0" + "uuid": "^9.0.1" } } diff --git a/src/setup-open.ts b/src/setup-open.ts index e02ef08..451da03 100644 --- a/src/setup-open.ts +++ b/src/setup-open.ts @@ -9,10 +9,10 @@ import { UpdateCallback, SQLBatchTuple, OpenOptions, - QueryResult + QueryResult, } from './types'; -import uuid from 'uuid'; +import { v4 } from 'uuid'; import _ from 'lodash'; import { enhanceQueryResult } from './utils'; import { DBListenerManagerInternal } from './DBListenerManager'; @@ -25,7 +25,7 @@ type LockCallbackRecord = { enum TransactionFinalizer { COMMIT = 'commit', - ROLLBACK = 'rollback' + ROLLBACK = 'rollback', } const DEFAULT_READ_CONNECTIONS = 4; @@ -49,7 +49,10 @@ function closeContextLock(dbName: string, id: ContextLockID) { * @param lockId * @returns */ -global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID) => { +global.onLockContextIsAvailable = async ( + dbName: string, + lockId: ContextLockID +) => { // Don't hold C++ bridge side up waiting to complete setImmediate(async () => { try { @@ -61,10 +64,15 @@ global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID) // @ts-expect-error This is not part of the public interface, but is used internally _contextId: lockId, execute: async (sql: string, args?: any[]) => { - const result = await proxy.executeInContext(dbName, lockId, sql, args); + const result = await proxy.executeInContext( + dbName, + lockId, + sql, + args + ); enhanceQueryResult(result); return result; - } + }, }); } catch (ex) { console.error(ex); @@ -87,11 +95,15 @@ export function setupOpen(QuickSQLite: ISQLite) { * By default opens DB in WAL mode with 4 Read connections and a single * write connection */ - open: (dbName: string, options: OpenOptions = {}): QuickSQLiteConnection => { + open: ( + dbName: string, + options: OpenOptions = {} + ): QuickSQLiteConnection => { // Opens the connection QuickSQLite.open(dbName, { ...options, - numReadConnections: options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS + numReadConnections: + options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS, }); const listenerManager = new DBListenerManagerInternal({ dbName }); @@ -106,7 +118,7 @@ export function setupOpen(QuickSQLite: ISQLite) { options?: LockOptions, hooks?: LockHooks ): Promise => { - const id = uuid.v4(); // TODO maybe do this in C++ + const id = v4(); // TODO maybe do this in C++ // Wrap the callback in a promise that will resolve to the callback result return new Promise((resolve, reject) => { // Add callback to the queue for timing @@ -117,14 +129,14 @@ export function setupOpen(QuickSQLite: ISQLite) { const res = await callback(context); closeContextLock(dbName, id); - resolve(res) + resolve(res); } catch (ex) { closeContextLock(dbName, id); - reject(ex) + reject(ex); } finally { - hooks?.lockReleased?.() + hooks?.lockReleased?.(); } - } + }, } as LockCallbackRecord); try { @@ -145,15 +157,20 @@ export function setupOpen(QuickSQLite: ISQLite) { }); }; - const readLock = (callback: (context: LockContext) => Promise, options?: LockOptions): Promise => - requestLock(ConcurrentLockType.READ, callback, options); + const readLock = ( + callback: (context: LockContext) => Promise, + options?: LockOptions + ): Promise => requestLock(ConcurrentLockType.READ, callback, options); - const writeLock = (callback: (context: LockContext) => Promise, options?: LockOptions): Promise => + const writeLock = ( + callback: (context: LockContext) => Promise, + options?: LockOptions + ): Promise => requestLock(ConcurrentLockType.WRITE, callback, options, { lockReleased: async () => { // flush updates once a write lock has been released listenerManager.flushUpdates(); - } + }, }); const wrapTransaction = async ( @@ -174,9 +191,13 @@ export function setupOpen(QuickSQLite: ISQLite) { return action(); }; - const commit = finalizedStatement(async () => context.execute('COMMIT')); + const commit = finalizedStatement(async () => + context.execute('COMMIT') + ); - const rollback = finalizedStatement(async () => context.execute('ROLLBACK')); + const rollback = finalizedStatement(async () => + context.execute('ROLLBACK') + ); const wrapExecute = ( @@ -184,7 +205,9 @@ export function setupOpen(QuickSQLite: ISQLite) { ): ((sql: string, params?: any[]) => Promise) => async (sql: string, params?: any[]) => { if (finalized) { - throw new Error(`Cannot execute in transaction after it has been finalized with commit/rollback.`); + throw new Error( + `Cannot execute in transaction after it has been finalized with commit/rollback.` + ); } return method(sql, params); }; @@ -194,7 +217,7 @@ export function setupOpen(QuickSQLite: ISQLite) { ...context, commit, rollback, - execute: wrapExecute(context.execute) + execute: wrapExecute(context.execute), }); switch (defaultFinalizer) { case TransactionFinalizer.COMMIT: @@ -214,26 +237,45 @@ export function setupOpen(QuickSQLite: ISQLite) { // Return the concurrent connection object return { close: () => QuickSQLite.close(dbName), - execute: (sql: string, args?: any[]) => writeLock((context) => context.execute(sql, args)), + execute: (sql: string, args?: any[]) => + writeLock((context) => context.execute(sql, args)), readLock, - readTransaction: async (callback: (context: TransactionContext) => Promise, options?: LockOptions) => - readLock((context) => wrapTransaction(context, callback)), + readTransaction: async ( + callback: (context: TransactionContext) => Promise, + options?: LockOptions + ) => readLock((context) => wrapTransaction(context, callback)), writeLock, - writeTransaction: async (callback: (context: TransactionContext) => Promise, options?: LockOptions) => - writeLock((context) => wrapTransaction(context, callback, TransactionFinalizer.COMMIT), options), + writeTransaction: async ( + callback: (context: TransactionContext) => Promise, + options?: LockOptions + ) => + writeLock( + (context) => + wrapTransaction(context, callback, TransactionFinalizer.COMMIT), + options + ), delete: () => QuickSQLite.delete(dbName, options?.location), executeBatch: (commands: SQLBatchTuple[]) => - writeLock((context) => QuickSQLite.executeBatch(dbName, commands, (context as any)._contextId)), + writeLock((context) => + QuickSQLite.executeBatch( + dbName, + commands, + (context as any)._contextId + ) + ), attach: (dbNameToAttach: string, alias: string, location?: string) => QuickSQLite.attach(dbName, dbNameToAttach, alias, location), detach: (alias: string) => QuickSQLite.detach(dbName, alias), loadFile: (location: string) => - writeLock((context) => QuickSQLite.loadFile(dbName, location, (context as any)._contextId)), + writeLock((context) => + QuickSQLite.loadFile(dbName, location, (context as any)._contextId) + ), listenerManager, registerUpdateHook: (callback: UpdateCallback) => listenerManager.registerListener({ rawTableChange: callback }), - registerTablesChangedHook: (callback) => listenerManager.registerListener({ tablesUpdated: callback }) + registerTablesChangedHook: (callback) => + listenerManager.registerListener({ tablesUpdated: callback }), }; - } + }, }; } diff --git a/tests/package.json b/tests/package.json index b5bfbf0..6355afa 100644 --- a/tests/package.json +++ b/tests/package.json @@ -33,7 +33,7 @@ "tailwindcss": "^3.2.4", "typeorm": "^0.3.11", "util": "^0.12.5", - "uuid": "3.4.0" + "uuid": "^9.0.1" }, "devDependencies": { "@babel/core": "^7.20.0", From daac2122154e6f5ccce1de6cf92d9245cf45b7ce Mon Sep 17 00:00:00 2001 From: Christiaan Landman Date: Wed, 17 Apr 2024 13:43:58 +0200 Subject: [PATCH 2/4] chore added updated lock files --- tests/ios/Podfile.lock | 4 ++-- tests/yarn.lock | 11 +++-------- yarn.lock | 18 ++++++++---------- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/tests/ios/Podfile.lock b/tests/ios/Podfile.lock index dc4841d..dd06310 100644 --- a/tests/ios/Podfile.lock +++ b/tests/ios/Podfile.lock @@ -930,7 +930,7 @@ PODS: - React-debug - react-native-get-random-values (1.9.0): - React-Core - - react-native-quick-sqlite (1.1.2): + - react-native-quick-sqlite (1.1.3): - powersync-sqlite-core (~> 0.1.6) - React - React-callinvoker @@ -1365,7 +1365,7 @@ SPEC CHECKSUMS: React-logger: 0a57b68dd2aec7ff738195f081f0520724b35dab React-Mapbuffer: 63913773ed7f96b814a2521e13e6d010282096ad react-native-get-random-values: dee677497c6a740b71e5612e8dbd83e7539ed5bb - react-native-quick-sqlite: a31ed88c8c78722f9eb6f959161ffb783bc6436e + react-native-quick-sqlite: 2b663b5762b255d2047545fc95f58b0c246c265c react-native-safe-area-context: 0ee144a6170530ccc37a0fd9388e28d06f516a89 React-nativeconfig: d7af5bae6da70fa15ce44f045621cf99ed24087c React-NativeModulesApple: 0123905d5699853ac68519607555a9a4f5c7b3ac diff --git a/tests/yarn.lock b/tests/yarn.lock index 59d8afd..5ec4e54 100644 --- a/tests/yarn.lock +++ b/tests/yarn.lock @@ -6267,10 +6267,10 @@ react-native-quick-base64@^2.0.5: base64-js "^1.5.1" react-native-quick-sqlite@./..: - version "1.1.2" + version "1.1.3" dependencies: lodash "^4.17.21" - uuid "3.4.0" + uuid "^9.0.1" react-native-safe-area-context@4.8.2: version "4.8.2" @@ -7411,11 +7411,6 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== -uuid@3.4.0: - version "3.4.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" - integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== - uuid@^7.0.3: version "7.0.3" resolved "https://registry.yarnpkg.com/uuid/-/uuid-7.0.3.tgz#c5c9f2c8cf25dc0a372c4df1441c41f5bd0c680b" @@ -7426,7 +7421,7 @@ uuid@^8.0.0, uuid@^8.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== -uuid@^9.0.0: +uuid@^9.0.0, uuid@^9.0.1: version "9.0.1" resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30" integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA== diff --git a/yarn.lock b/yarn.lock index c7b9d2d..4b2d0da 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1810,12 +1810,10 @@ resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.3.tgz#6209321eb2c1712a7e7466422b8cb1fc0d9dd5d8" integrity sha512-9aEbYZ3TbYMznPdcdr3SmIrLXwC/AKZXQeCf9Pgao5CKb8CyHuEX5jzWPTkvregvhRJHcpRO6BFoGW9ycaOkYw== -"@types/uuid@3.4.0": - version "3.4.0" - resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-3.4.0.tgz#91639516fbb892ed3737a2bdc1b030d02b4a09c1" - integrity sha512-Vd+WmnrQKrrfVJ+9LWyOWqlBQJFsfi8rhKRm3ag3ZrOjY5SmzZkGmxbkgRIk9jpZt4dpvE21cmbBSp1dCV7/fw== - dependencies: - "@types/node" "*" +"@types/uuid@^9.0.1": + version "9.0.8" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.8.tgz#7545ba4fc3c003d6c756f651f3bf163d8f0f29ba" + integrity sha512-jg+97EGIcY9AGHJJRaaPVgetKDsrTgbRjQ5Msgjh/DQKEFl0DtyRr/VCOyD1T2R1MNeWPK/u7JoGhlDZnKBAfA== "@types/yargs-parser@*": version "21.0.3" @@ -5872,10 +5870,10 @@ utils-merge@1.0.1: resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713" integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA== -uuid@3.4.0: - version "3.4.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" - integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A== +uuid@^9.0.1: + version "9.0.1" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.1.tgz#e188d4c8853cc722220392c424cd637f32293f30" + integrity sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA== validate-npm-package-license@^3.0.1: version "3.0.4" From 3c6cb8dbe433090eafaf937893cefc7763baaf7f Mon Sep 17 00:00:00 2001 From: Christiaan Landman Date: Wed, 17 Apr 2024 13:52:37 +0200 Subject: [PATCH 3/4] chore revert formatting noise --- src/setup-open.ts | 100 ++++++++++++++-------------------------------- 1 file changed, 29 insertions(+), 71 deletions(-) diff --git a/src/setup-open.ts b/src/setup-open.ts index 451da03..7d1ae6c 100644 --- a/src/setup-open.ts +++ b/src/setup-open.ts @@ -9,7 +9,7 @@ import { UpdateCallback, SQLBatchTuple, OpenOptions, - QueryResult, + QueryResult } from './types'; import { v4 } from 'uuid'; @@ -25,7 +25,7 @@ type LockCallbackRecord = { enum TransactionFinalizer { COMMIT = 'commit', - ROLLBACK = 'rollback', + ROLLBACK = 'rollback' } const DEFAULT_READ_CONNECTIONS = 4; @@ -49,10 +49,7 @@ function closeContextLock(dbName: string, id: ContextLockID) { * @param lockId * @returns */ -global.onLockContextIsAvailable = async ( - dbName: string, - lockId: ContextLockID -) => { +global.onLockContextIsAvailable = async (dbName: string, lockId: ContextLockID) => { // Don't hold C++ bridge side up waiting to complete setImmediate(async () => { try { @@ -64,15 +61,10 @@ global.onLockContextIsAvailable = async ( // @ts-expect-error This is not part of the public interface, but is used internally _contextId: lockId, execute: async (sql: string, args?: any[]) => { - const result = await proxy.executeInContext( - dbName, - lockId, - sql, - args - ); + const result = await proxy.executeInContext(dbName, lockId, sql, args); enhanceQueryResult(result); return result; - }, + } }); } catch (ex) { console.error(ex); @@ -95,15 +87,11 @@ export function setupOpen(QuickSQLite: ISQLite) { * By default opens DB in WAL mode with 4 Read connections and a single * write connection */ - open: ( - dbName: string, - options: OpenOptions = {} - ): QuickSQLiteConnection => { + open: (dbName: string, options: OpenOptions = {}): QuickSQLiteConnection => { // Opens the connection QuickSQLite.open(dbName, { ...options, - numReadConnections: - options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS, + numReadConnections: options?.numReadConnections ?? DEFAULT_READ_CONNECTIONS }); const listenerManager = new DBListenerManagerInternal({ dbName }); @@ -129,14 +117,14 @@ export function setupOpen(QuickSQLite: ISQLite) { const res = await callback(context); closeContextLock(dbName, id); - resolve(res); + resolve(res) } catch (ex) { closeContextLock(dbName, id); - reject(ex); + reject(ex) } finally { - hooks?.lockReleased?.(); + hooks?.lockReleased?.() } - }, + } } as LockCallbackRecord); try { @@ -157,20 +145,15 @@ export function setupOpen(QuickSQLite: ISQLite) { }); }; - const readLock = ( - callback: (context: LockContext) => Promise, - options?: LockOptions - ): Promise => requestLock(ConcurrentLockType.READ, callback, options); + const readLock = (callback: (context: LockContext) => Promise, options?: LockOptions): Promise => + requestLock(ConcurrentLockType.READ, callback, options); - const writeLock = ( - callback: (context: LockContext) => Promise, - options?: LockOptions - ): Promise => + const writeLock = (callback: (context: LockContext) => Promise, options?: LockOptions): Promise => requestLock(ConcurrentLockType.WRITE, callback, options, { lockReleased: async () => { // flush updates once a write lock has been released listenerManager.flushUpdates(); - }, + } }); const wrapTransaction = async ( @@ -191,13 +174,9 @@ export function setupOpen(QuickSQLite: ISQLite) { return action(); }; - const commit = finalizedStatement(async () => - context.execute('COMMIT') - ); + const commit = finalizedStatement(async () => context.execute('COMMIT')); - const rollback = finalizedStatement(async () => - context.execute('ROLLBACK') - ); + const rollback = finalizedStatement(async () => context.execute('ROLLBACK')); const wrapExecute = ( @@ -205,9 +184,7 @@ export function setupOpen(QuickSQLite: ISQLite) { ): ((sql: string, params?: any[]) => Promise) => async (sql: string, params?: any[]) => { if (finalized) { - throw new Error( - `Cannot execute in transaction after it has been finalized with commit/rollback.` - ); + throw new Error(`Cannot execute in transaction after it has been finalized with commit/rollback.`); } return method(sql, params); }; @@ -217,7 +194,7 @@ export function setupOpen(QuickSQLite: ISQLite) { ...context, commit, rollback, - execute: wrapExecute(context.execute), + execute: wrapExecute(context.execute) }); switch (defaultFinalizer) { case TransactionFinalizer.COMMIT: @@ -237,45 +214,26 @@ export function setupOpen(QuickSQLite: ISQLite) { // Return the concurrent connection object return { close: () => QuickSQLite.close(dbName), - execute: (sql: string, args?: any[]) => - writeLock((context) => context.execute(sql, args)), + execute: (sql: string, args?: any[]) => writeLock((context) => context.execute(sql, args)), readLock, - readTransaction: async ( - callback: (context: TransactionContext) => Promise, - options?: LockOptions - ) => readLock((context) => wrapTransaction(context, callback)), + readTransaction: async (callback: (context: TransactionContext) => Promise, options?: LockOptions) => + readLock((context) => wrapTransaction(context, callback)), writeLock, - writeTransaction: async ( - callback: (context: TransactionContext) => Promise, - options?: LockOptions - ) => - writeLock( - (context) => - wrapTransaction(context, callback, TransactionFinalizer.COMMIT), - options - ), + writeTransaction: async (callback: (context: TransactionContext) => Promise, options?: LockOptions) => + writeLock((context) => wrapTransaction(context, callback, TransactionFinalizer.COMMIT), options), delete: () => QuickSQLite.delete(dbName, options?.location), executeBatch: (commands: SQLBatchTuple[]) => - writeLock((context) => - QuickSQLite.executeBatch( - dbName, - commands, - (context as any)._contextId - ) - ), + writeLock((context) => QuickSQLite.executeBatch(dbName, commands, (context as any)._contextId)), attach: (dbNameToAttach: string, alias: string, location?: string) => QuickSQLite.attach(dbName, dbNameToAttach, alias, location), detach: (alias: string) => QuickSQLite.detach(dbName, alias), loadFile: (location: string) => - writeLock((context) => - QuickSQLite.loadFile(dbName, location, (context as any)._contextId) - ), + writeLock((context) => QuickSQLite.loadFile(dbName, location, (context as any)._contextId)), listenerManager, registerUpdateHook: (callback: UpdateCallback) => listenerManager.registerListener({ rawTableChange: callback }), - registerTablesChangedHook: (callback) => - listenerManager.registerListener({ tablesUpdated: callback }), + registerTablesChangedHook: (callback) => listenerManager.registerListener({ tablesUpdated: callback }) }; - }, + } }; -} +} \ No newline at end of file From 710ace44a58760521d6ea80ec7fd08ccb4d23c96 Mon Sep 17 00:00:00 2001 From: Christiaan Landman Date: Wed, 17 Apr 2024 14:33:12 +0200 Subject: [PATCH 4/4] chore changeset --- .changeset/rotten-spoons-beg.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rotten-spoons-beg.md diff --git a/.changeset/rotten-spoons-beg.md b/.changeset/rotten-spoons-beg.md new file mode 100644 index 0000000..02cbb13 --- /dev/null +++ b/.changeset/rotten-spoons-beg.md @@ -0,0 +1,5 @@ +--- +'@journeyapps/react-native-quick-sqlite': patch +--- + +Updated UUID dependency.