@capgo/capacitor-fast-sql 8.0.46 → 8.0.48

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.
@@ -19,7 +19,7 @@ import java.util.Map;
19
19
  @CapacitorPlugin(name = "CapgoCapacitorFastSql")
20
20
  public class CapgoCapacitorFastSqlPlugin extends Plugin {
21
21
 
22
- private final String pluginVersion = "8.0.46";
22
+ private final String pluginVersion = "8.0.48";
23
23
 
24
24
  private Map<String, DatabaseConnection> databases = new HashMap<>();
25
25
  private SQLHTTPServer server;
@@ -10,7 +10,7 @@ import SQLite3
10
10
  */
11
11
  @objc(CapgoCapacitorFastSqlPlugin)
12
12
  public class CapgoCapacitorFastSqlPlugin: CAPPlugin, CAPBridgedPlugin {
13
- private let pluginVersion: String = "8.0.46"
13
+ private let pluginVersion: String = "8.0.48"
14
14
  public let identifier = "CapgoCapacitorFastSqlPlugin"
15
15
  public let jsName = "CapgoCapacitorFastSql"
16
16
  public let pluginMethods: [CAPPluginMethod] = [
@@ -59,12 +59,26 @@ public class CapgoCapacitorFastSqlPlugin: CAPPlugin, CAPBridgedPlugin {
59
59
 
60
60
  // Open database
61
61
  let db = try SQLDatabase(path: dbPath, encrypted: encrypted, encryptionKey: encryptionKey)
62
- databases[database] = db
63
62
 
64
- // Start HTTP server if not already running
65
- if server == nil {
66
- server = try SQLHTTPServer(databases: databases)
67
- try server?.start()
63
+ // Start HTTP server if not already running, otherwise register the
64
+ // new database with it. SQLHTTPServer stores a VALUE COPY of the
65
+ // databases dict (Swift Dict is a value type), so every connect()
66
+ // after server creation must push the new entry in explicitly.
67
+ if let server {
68
+ databases[database] = db
69
+ server.addDatabase(db, forKey: database)
70
+ } else {
71
+ var initialDatabases = databases
72
+ initialDatabases[database] = db
73
+ do {
74
+ let newServer = try SQLHTTPServer(databases: initialDatabases)
75
+ try newServer.start()
76
+ databases[database] = db
77
+ self.server = newServer
78
+ } catch {
79
+ db.close()
80
+ throw error
81
+ }
68
82
  }
69
83
 
70
84
  guard let server = server else {
@@ -93,8 +107,9 @@ public class CapgoCapacitorFastSqlPlugin: CAPPlugin, CAPBridgedPlugin {
93
107
  return
94
108
  }
95
109
 
96
- db.close()
110
+ server?.removeDatabase(forKey: database)
97
111
  databases.removeValue(forKey: database)
112
+ db.close()
98
113
 
99
114
  // Stop server if no more databases
100
115
  if databases.isEmpty {
@@ -12,6 +12,7 @@ class SQLHTTPServer {
12
12
  let port: Int
13
13
  let token: String
14
14
  private var databases: [String: SQLDatabase]
15
+ private let databasesLock = NSLock()
15
16
  private var server: Server?
16
17
  private var isRunning = false
17
18
 
@@ -24,6 +25,34 @@ class SQLHTTPServer {
24
25
  self.server = Server()
25
26
  }
26
27
 
28
+ /// Registers a newly-connected database with the HTTP server.
29
+ ///
30
+ /// Must be called after every `connect()` that finds the server already running,
31
+ /// because `SQLHTTPServer` holds a **value-type copy** of the databases dictionary
32
+ /// and cannot observe subsequent mutations made to the plugin's own copy.
33
+ func addDatabase(_ db: SQLDatabase, forKey key: String) {
34
+ databasesLock.lock()
35
+ defer { databasesLock.unlock() }
36
+ databases[key] = db
37
+ }
38
+
39
+ /// Removes a disconnected database from the HTTP server's registry.
40
+ ///
41
+ /// Call whenever a database is disconnected so in-flight HTTP requests for it
42
+ /// receive a timely "not found" response rather than accessing a closed handle.
43
+ func removeDatabase(forKey key: String) {
44
+ databasesLock.lock()
45
+ defer { databasesLock.unlock() }
46
+ databases.removeValue(forKey: key)
47
+ }
48
+
49
+ /// Returns the open database for `key`, or `nil` if not registered.
50
+ private func registeredDatabase(forKey key: String) -> SQLDatabase? {
51
+ databasesLock.lock()
52
+ defer { databasesLock.unlock() }
53
+ return databases[key]
54
+ }
55
+
27
56
  func start() throws {
28
57
  guard !isRunning else { return }
29
58
  guard let server = server else {
@@ -180,7 +209,7 @@ class SQLHTTPServer {
180
209
  }
181
210
 
182
211
  // Check if database exists
183
- guard let db = databases[database] else {
212
+ guard let db = registeredDatabase(forKey: database) else {
184
213
  let bodyData = "Database not found".data(using: .utf8)!
185
214
  return HTTPResponse(.notFound, body: bodyData)
186
215
  }
@@ -219,7 +248,7 @@ class SQLHTTPServer {
219
248
  }
220
249
 
221
250
  // Check if database exists
222
- guard let db = databases[database] else {
251
+ guard let db = registeredDatabase(forKey: database) else {
223
252
  let bodyData = "Database not found".data(using: .utf8)!
224
253
  return HTTPResponse(.notFound, body: bodyData)
225
254
  }
@@ -267,7 +296,7 @@ class SQLHTTPServer {
267
296
  }
268
297
 
269
298
  // Check if database exists
270
- guard let db = databases[database] else {
299
+ guard let db = registeredDatabase(forKey: database) else {
271
300
  let bodyData = "Database not found".data(using: .utf8)!
272
301
  return HTTPResponse(.notFound, body: bodyData)
273
302
  }
@@ -296,7 +325,7 @@ class SQLHTTPServer {
296
325
  }
297
326
 
298
327
  // Check if database exists
299
- guard let db = databases[database] else {
328
+ guard let db = registeredDatabase(forKey: database) else {
300
329
  let bodyData = "Database not found".data(using: .utf8)!
301
330
  return HTTPResponse(.notFound, body: bodyData)
302
331
  }
@@ -325,7 +354,7 @@ class SQLHTTPServer {
325
354
  }
326
355
 
327
356
  // Check if database exists
328
- guard let db = databases[database] else {
357
+ guard let db = registeredDatabase(forKey: database) else {
329
358
  let bodyData = "Database not found".data(using: .utf8)!
330
359
  return HTTPResponse(.notFound, body: bodyData)
331
360
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-fast-sql",
3
- "version": "8.0.46",
3
+ "version": "8.0.48",
4
4
  "description": "High-performance native SQLite plugin with custom protocol for efficient sync operations and IndexedDB replacement",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",
@@ -53,8 +53,7 @@
53
53
  "prepublishOnly": "bun run build",
54
54
  "check:wiring": "node scripts/check-capacitor-plugin-wiring.mjs",
55
55
  "example:install": "cd example-app && bun install --frozen-lockfile",
56
- "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build",
57
- "example:capgo:deploy": "bun run example:build && bun scripts/deploy-example-capgo.mjs"
56
+ "example:build": "bun run build && cd example-app && bun install --frozen-lockfile && bun run build"
58
57
  },
59
58
  "devDependencies": {
60
59
  "@capacitor/android": "^8.0.0",