react-native-nitro-sqlite 9.1.11 → 9.2.0

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.
Files changed (111) hide show
  1. package/android/.gradle/8.9/checksums/checksums.lock +0 -0
  2. package/android/.gradle/8.9/checksums/md5-checksums.bin +0 -0
  3. package/android/.gradle/8.9/checksums/sha1-checksums.bin +0 -0
  4. package/android/.gradle/8.9/dependencies-accessors/gc.properties +0 -0
  5. package/android/.gradle/8.9/fileChanges/last-build.bin +0 -0
  6. package/android/.gradle/8.9/fileHashes/fileHashes.lock +0 -0
  7. package/android/.gradle/8.9/gc.properties +0 -0
  8. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  9. package/android/.gradle/buildOutputCleanup/cache.properties +2 -0
  10. package/android/.gradle/vcs-1/gc.properties +0 -0
  11. package/android/CMakeLists.txt +0 -1
  12. package/cpp/NitroSQLiteException.hpp +16 -13
  13. package/cpp/importSqlFile.cpp +2 -2
  14. package/cpp/logs.hpp +14 -12
  15. package/cpp/operations.cpp +29 -28
  16. package/cpp/operations.hpp +1 -1
  17. package/cpp/specs/HybridNativeQueryResult.hpp +1 -1
  18. package/cpp/specs/HybridNitroSQLite.cpp +5 -5
  19. package/cpp/specs/HybridNitroSQLite.hpp +16 -13
  20. package/cpp/sqliteExecuteBatch.cpp +1 -1
  21. package/cpp/types.hpp +3 -3
  22. package/cpp/utils.hpp +2 -3
  23. package/lib/commonjs/DatabaseQueue.js +104 -0
  24. package/lib/commonjs/DatabaseQueue.js.map +1 -0
  25. package/lib/commonjs/NitroSQLiteError.js +50 -0
  26. package/lib/commonjs/NitroSQLiteError.js.map +1 -0
  27. package/lib/commonjs/index.js +9 -1
  28. package/lib/commonjs/index.js.map +1 -1
  29. package/lib/commonjs/nitro.js +1 -2
  30. package/lib/commonjs/nitro.js.map +1 -1
  31. package/lib/commonjs/nullHandling.js +1 -1
  32. package/lib/commonjs/nullHandling.js.map +1 -1
  33. package/lib/commonjs/operations/execute.js +14 -6
  34. package/lib/commonjs/operations/execute.js.map +1 -1
  35. package/lib/commonjs/operations/executeBatch.js +17 -4
  36. package/lib/commonjs/operations/executeBatch.js.map +1 -1
  37. package/lib/commonjs/operations/session.js +17 -15
  38. package/lib/commonjs/operations/session.js.map +1 -1
  39. package/lib/commonjs/operations/transaction.js +26 -54
  40. package/lib/commonjs/operations/transaction.js.map +1 -1
  41. package/lib/commonjs/typeORM.js +1 -2
  42. package/lib/commonjs/typeORM.js.map +1 -1
  43. package/lib/commonjs/types.js.map +1 -1
  44. package/lib/module/DatabaseQueue.js +91 -0
  45. package/lib/module/DatabaseQueue.js.map +1 -0
  46. package/lib/module/NitroSQLiteError.js +45 -0
  47. package/lib/module/NitroSQLiteError.js.map +1 -0
  48. package/lib/module/index.js +2 -1
  49. package/lib/module/index.js.map +1 -1
  50. package/lib/module/nitro.js +0 -1
  51. package/lib/module/nitro.js.map +1 -1
  52. package/lib/module/nullHandling.js +1 -1
  53. package/lib/module/nullHandling.js.map +1 -1
  54. package/lib/module/operations/execute.js +13 -6
  55. package/lib/module/operations/execute.js.map +1 -1
  56. package/lib/module/operations/executeBatch.js +16 -4
  57. package/lib/module/operations/executeBatch.js.map +1 -1
  58. package/lib/module/operations/session.js +17 -14
  59. package/lib/module/operations/session.js.map +1 -1
  60. package/lib/module/operations/transaction.js +25 -54
  61. package/lib/module/operations/transaction.js.map +1 -1
  62. package/lib/module/types.js.map +1 -1
  63. package/lib/typescript/commonjs/DatabaseQueue.d.ts +20 -0
  64. package/lib/typescript/commonjs/DatabaseQueue.d.ts.map +1 -0
  65. package/lib/typescript/commonjs/NitroSQLiteError.d.ts +13 -0
  66. package/lib/typescript/commonjs/NitroSQLiteError.d.ts.map +1 -0
  67. package/lib/typescript/commonjs/index.d.ts +4 -3
  68. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  69. package/lib/typescript/commonjs/nitro.d.ts +0 -5
  70. package/lib/typescript/commonjs/nitro.d.ts.map +1 -1
  71. package/lib/typescript/commonjs/nullHandling.d.ts +1 -1
  72. package/lib/typescript/commonjs/nullHandling.d.ts.map +1 -1
  73. package/lib/typescript/commonjs/operations/execute.d.ts.map +1 -1
  74. package/lib/typescript/commonjs/operations/executeBatch.d.ts.map +1 -1
  75. package/lib/typescript/commonjs/operations/session.d.ts +0 -2
  76. package/lib/typescript/commonjs/operations/session.d.ts.map +1 -1
  77. package/lib/typescript/commonjs/operations/transaction.d.ts +1 -4
  78. package/lib/typescript/commonjs/operations/transaction.d.ts.map +1 -1
  79. package/lib/typescript/commonjs/types.d.ts +1 -1
  80. package/lib/typescript/commonjs/types.d.ts.map +1 -1
  81. package/lib/typescript/module/DatabaseQueue.d.ts +20 -0
  82. package/lib/typescript/module/DatabaseQueue.d.ts.map +1 -0
  83. package/lib/typescript/module/NitroSQLiteError.d.ts +13 -0
  84. package/lib/typescript/module/NitroSQLiteError.d.ts.map +1 -0
  85. package/lib/typescript/module/index.d.ts +4 -3
  86. package/lib/typescript/module/index.d.ts.map +1 -1
  87. package/lib/typescript/module/nitro.d.ts +0 -5
  88. package/lib/typescript/module/nitro.d.ts.map +1 -1
  89. package/lib/typescript/module/nullHandling.d.ts +1 -1
  90. package/lib/typescript/module/nullHandling.d.ts.map +1 -1
  91. package/lib/typescript/module/operations/execute.d.ts.map +1 -1
  92. package/lib/typescript/module/operations/executeBatch.d.ts.map +1 -1
  93. package/lib/typescript/module/operations/session.d.ts +0 -2
  94. package/lib/typescript/module/operations/session.d.ts.map +1 -1
  95. package/lib/typescript/module/operations/transaction.d.ts +1 -4
  96. package/lib/typescript/module/operations/transaction.d.ts.map +1 -1
  97. package/lib/typescript/module/types.d.ts +1 -1
  98. package/lib/typescript/module/types.d.ts.map +1 -1
  99. package/package.json +15 -15
  100. package/src/DatabaseQueue.ts +129 -0
  101. package/src/NitroSQLiteError.ts +45 -0
  102. package/src/index.ts +3 -2
  103. package/src/nitro.ts +0 -6
  104. package/src/nullHandling.ts +8 -4
  105. package/src/operations/execute.ts +27 -19
  106. package/src/operations/executeBatch.ts +30 -10
  107. package/src/operations/session.ts +21 -21
  108. package/src/operations/transaction.ts +42 -85
  109. package/src/specs/NitroSQLite.nitro.ts +5 -5
  110. package/src/typeORM.ts +6 -6
  111. package/src/types.ts +5 -3
@@ -1,4 +1,4 @@
1
- import { locks, HybridNitroSQLite } from '../nitro'
1
+ import { queueOperationAsync, throwIfDatabaseIsNotOpen } from '../DatabaseQueue'
2
2
  import type {
3
3
  QueryResult,
4
4
  Transaction,
@@ -6,131 +6,88 @@ import type {
6
6
  QueryResultRow,
7
7
  } from '../types'
8
8
  import { execute, executeAsync } from './execute'
9
+ import NitroSQLiteError from '../NitroSQLiteError'
9
10
 
10
- export interface PendingTransaction {
11
- /*
12
- * The start function should not throw or return a promise because the
13
- * queue just calls it and does not monitor for failures or completions.
14
- *
15
- * It should catch any errors and call the resolve or reject of the wrapping
16
- * promise when complete.
17
- *
18
- * It should also automatically commit or rollback the transaction if needed
19
- */
20
- start: () => void
21
- }
22
-
23
- export const transaction = (
11
+ export const transaction = async <Result = void>(
24
12
  dbName: string,
25
- fn: (tx: Transaction) => Promise<void> | void
26
- ): Promise<void> => {
27
- if (locks[dbName] == null)
28
- throw Error(`Nitro SQLite Error: No lock found on db: ${dbName}`)
13
+ transactionCallback: (tx: Transaction) => Promise<Result>,
14
+ isExclusive = false,
15
+ ) => {
16
+ throwIfDatabaseIsNotOpen(dbName)
29
17
 
30
- let isFinalized = false
18
+ let isFinished = false
31
19
 
32
- // Local transaction context object implementation
33
- const executeOnTransaction = <Data extends QueryResultRow = never>(
20
+ const executeOnTransaction = <Row extends QueryResultRow = never>(
34
21
  query: string,
35
- params?: SQLiteQueryParams
36
- ): QueryResult<Data> => {
37
- if (isFinalized) {
38
- throw Error(
39
- `Nitro SQLite Error: Cannot execute query on finalized transaction: ${dbName}`
22
+ params?: SQLiteQueryParams,
23
+ ): QueryResult<Row> => {
24
+ if (isFinished) {
25
+ throw new NitroSQLiteError(
26
+ `Cannot execute query on finalized transaction: ${dbName}`,
40
27
  )
41
28
  }
42
29
  return execute(dbName, query, params)
43
30
  }
44
31
 
45
- const executeAsyncOnTransaction = <Data extends QueryResultRow = never>(
32
+ const executeAsyncOnTransaction = <Row extends QueryResultRow = never>(
46
33
  query: string,
47
- params?: SQLiteQueryParams
48
- ): Promise<QueryResult<Data>> => {
49
- if (isFinalized) {
50
- throw Error(
51
- `Nitro SQLite Error: Cannot execute query on finalized transaction: ${dbName}`
34
+ params?: SQLiteQueryParams,
35
+ ): Promise<QueryResult<Row>> => {
36
+ if (isFinished) {
37
+ throw new NitroSQLiteError(
38
+ `Cannot execute query on finalized transaction: ${dbName}`,
52
39
  )
53
40
  }
54
41
  return executeAsync(dbName, query, params)
55
42
  }
56
43
 
57
44
  const commit = () => {
58
- if (isFinalized) {
59
- throw Error(
60
- `Nitro SQLite Error: Cannot execute commit on finalized transaction: ${dbName}`
45
+ if (isFinished) {
46
+ throw new NitroSQLiteError(
47
+ `Cannot execute commit on finalized transaction: ${dbName}`,
61
48
  )
62
49
  }
63
- const result = HybridNitroSQLite.execute(dbName, 'COMMIT')
64
- isFinalized = true
65
- return result
50
+ isFinished = true
51
+ return execute(dbName, 'COMMIT')
66
52
  }
67
53
 
68
54
  const rollback = () => {
69
- if (isFinalized) {
70
- throw Error(
71
- `Nitro SQLite Error: Cannot execute rollback on finalized transaction: ${dbName}`
55
+ if (isFinished) {
56
+ throw new NitroSQLiteError(
57
+ `Cannot execute rollback on finalized transaction: ${dbName}`,
72
58
  )
73
59
  }
74
- const result = HybridNitroSQLite.execute(dbName, 'ROLLBACK')
75
- isFinalized = true
76
- return result
60
+ isFinished = true
61
+ return execute(dbName, 'ROLLBACK')
77
62
  }
78
63
 
79
- async function run() {
64
+ return await queueOperationAsync(dbName, async () => {
80
65
  try {
81
- await HybridNitroSQLite.executeAsync(dbName, 'BEGIN TRANSACTION')
66
+ await executeAsync(
67
+ dbName,
68
+ isExclusive ? 'BEGIN EXCLUSIVE TRANSACTION' : 'BEGIN TRANSACTION',
69
+ )
82
70
 
83
- await fn({
71
+ const result = await transactionCallback({
84
72
  commit,
85
73
  execute: executeOnTransaction,
86
74
  executeAsync: executeAsyncOnTransaction,
87
75
  rollback,
88
76
  })
89
77
 
90
- if (!isFinalized) commit()
78
+ if (!isFinished) commit()
79
+
80
+ return result
91
81
  } catch (executionError) {
92
- if (!isFinalized) {
82
+ if (!isFinished) {
93
83
  try {
94
84
  rollback()
95
85
  } catch (rollbackError) {
96
- throw rollbackError
86
+ throw NitroSQLiteError.fromError(rollbackError)
97
87
  }
98
88
  }
99
89
 
100
- throw executionError
101
- } finally {
102
- locks[dbName]!.inProgress = false
103
- isFinalized = false
104
- startNextTransaction(dbName)
90
+ throw NitroSQLiteError.fromError(executionError)
105
91
  }
106
- }
107
-
108
- return new Promise((resolve, reject) => {
109
- const tx: PendingTransaction = {
110
- start: () => {
111
- run().then(resolve).catch(reject)
112
- },
113
- }
114
-
115
- locks[dbName]?.queue.push(tx)
116
- startNextTransaction(dbName)
117
92
  })
118
93
  }
119
-
120
- function startNextTransaction(dbName: string) {
121
- if (locks[dbName] == null) throw Error(`Lock not found for db: ${dbName}`)
122
-
123
- if (locks[dbName].inProgress) {
124
- // Transaction is already in process bail out
125
- return
126
- }
127
-
128
- if (locks[dbName].queue.length > 0) {
129
- locks[dbName].inProgress = true
130
-
131
- const tx = locks[dbName].queue.shift()!
132
- setImmediate(() => {
133
- tx.start()
134
- })
135
- }
136
- }
@@ -16,26 +16,26 @@ export interface NitroSQLite
16
16
  mainDbName: string,
17
17
  dbNameToAttach: string,
18
18
  alias: string,
19
- location?: string
19
+ location?: string,
20
20
  ): void
21
21
  detach(mainDbName: string, alias: string): void
22
22
  execute(
23
23
  dbName: string,
24
24
  query: string,
25
- params?: NativeSQLiteQueryParams
25
+ params?: NativeSQLiteQueryParams,
26
26
  ): NativeQueryResult
27
27
  executeAsync(
28
28
  dbName: string,
29
29
  query: string,
30
- params?: NativeSQLiteQueryParams
30
+ params?: NativeSQLiteQueryParams,
31
31
  ): Promise<NativeQueryResult>
32
32
  executeBatch(
33
33
  dbName: string,
34
- commands: NativeBatchQueryCommand[]
34
+ commands: NativeBatchQueryCommand[],
35
35
  ): BatchQueryResult
36
36
  executeBatchAsync(
37
37
  dbName: string,
38
- commands: NativeBatchQueryCommand[]
38
+ commands: NativeBatchQueryCommand[],
39
39
  ): Promise<BatchQueryResult>
40
40
  loadFile(dbName: string, location: string): FileLoadResult
41
41
  loadFileAsync(dbName: string, location: string): Promise<FileLoadResult>
package/src/typeORM.ts CHANGED
@@ -18,7 +18,7 @@ interface TypeOrmNitroSQLiteConnection {
18
18
  sql: string,
19
19
  params: SQLiteQueryParams | undefined,
20
20
  okExecute: (res: QueryResult<RowData>) => void,
21
- failExecute: (msg: string) => void
21
+ failExecute: (msg: string) => void,
22
22
  ) => Promise<void>
23
23
  transaction: (fn: (tx: Transaction) => Promise<void>) => Promise<void>
24
24
  close: (okClose: () => void, failClose: (e: unknown) => void) => void
@@ -26,7 +26,7 @@ interface TypeOrmNitroSQLiteConnection {
26
26
  dbNameToAttach: string,
27
27
  alias: string,
28
28
  location: string | undefined,
29
- callback: () => void
29
+ callback: () => void,
30
30
  ) => void
31
31
  detach: (alias: string, callback: () => void) => void
32
32
  }
@@ -42,7 +42,7 @@ export const typeORMDriver = {
42
42
  location?: string
43
43
  },
44
44
  ok: (db: TypeOrmNitroSQLiteConnection) => void,
45
- fail: (msg: string) => void
45
+ fail: (msg: string) => void,
46
46
  ): TypeOrmNitroSQLiteConnection | null => {
47
47
  try {
48
48
  const db = Operations.open(options)
@@ -52,7 +52,7 @@ export const typeORMDriver = {
52
52
  sql: string,
53
53
  params: SQLiteQueryParams | undefined,
54
54
  okExecute: (res: QueryResult<RowData>) => void,
55
- failExecute: (msg: string) => void
55
+ failExecute: (msg: string) => void,
56
56
  ) => {
57
57
  try {
58
58
  const result = await db.executeAsync<RowData>(sql, params)
@@ -62,7 +62,7 @@ export const typeORMDriver = {
62
62
  }
63
63
  },
64
64
  transaction: (
65
- fn: (tx: Transaction) => Promise<void>
65
+ fn: (tx: Transaction) => Promise<void>,
66
66
  ): Promise<void> => {
67
67
  return db.transaction(fn)
68
68
  },
@@ -78,7 +78,7 @@ export const typeORMDriver = {
78
78
  dbNameToAttach: string,
79
79
  alias: string,
80
80
  location: string | undefined,
81
- callback: () => void
81
+ callback: () => void,
82
82
  ) => {
83
83
  db.attach(dbNameToAttach, alias, location)
84
84
  callback()
package/src/types.ts CHANGED
@@ -8,7 +8,9 @@ export interface NitroSQLiteConnection {
8
8
  delete(): void
9
9
  attach(dbNameToAttach: string, alias: string, location?: string): void
10
10
  detach(alias: string): void
11
- transaction(fn: (tx: Transaction) => Promise<void> | void): Promise<void>
11
+ transaction: <Result = void>(
12
+ transactionCallback: (tx: Transaction) => Promise<Result>,
13
+ ) => Promise<Result>
12
14
  execute: ExecuteQuery
13
15
  executeAsync: ExecuteAsyncQuery
14
16
  executeBatch(commands: BatchQueryCommand[]): BatchQueryResult
@@ -67,12 +69,12 @@ export interface QueryResult<Row extends QueryResultRow = QueryResultRow> {
67
69
 
68
70
  export type ExecuteQuery = <Row extends QueryResultRow = QueryResultRow>(
69
71
  query: string,
70
- params?: SQLiteQueryParams
72
+ params?: SQLiteQueryParams,
71
73
  ) => QueryResult<Row>
72
74
 
73
75
  export type ExecuteAsyncQuery = <Row extends QueryResultRow = QueryResultRow>(
74
76
  query: string,
75
- params?: SQLiteQueryParams
77
+ params?: SQLiteQueryParams,
76
78
  ) => Promise<QueryResult<Row>>
77
79
 
78
80
  export interface Transaction {