@capgo/capacitor-fast-sql 8.0.24 → 8.0.25
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.
- package/README.md +54 -0
- package/android/build.gradle +1 -1
- package/android/src/main/java/app/capgo/capacitor/fastsql/CapgoCapacitorFastSqlPlugin.java +7 -1
- package/android/src/main/java/app/capgo/capacitor/fastsql/EncryptedSQLDatabase.java +7 -8
- package/android/src/main/java/app/capgo/capacitor/fastsql/SQLDatabase.java +7 -8
- package/dist/esm/definitions.d.ts +40 -0
- package/dist/esm/definitions.d.ts.map +1 -1
- package/dist/esm/fast-sql.d.ts +1 -1
- package/dist/esm/fast-sql.d.ts.map +1 -1
- package/dist/esm/fast-sql.js +12 -3
- package/dist/esm/helpers.js +0 -1
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/native-sql-connection.d.ts +95 -0
- package/dist/esm/native-sql-connection.d.ts.map +1 -0
- package/dist/esm/native-sql-connection.js +244 -0
- package/dist/esm/sql-connection.d.ts +4 -78
- package/dist/esm/sql-connection.d.ts.map +1 -1
- package/dist/esm/sql-connection.js +1 -244
- package/dist/esm/web-sql-connection.d.ts +72 -0
- package/dist/esm/web-sql-connection.d.ts.map +1 -0
- package/dist/esm/web-sql-connection.js +122 -0
- package/dist/esm/web.d.ts +9 -2
- package/dist/esm/web.d.ts.map +1 -1
- package/dist/esm/web.js +41 -25
- package/dist/plugin.cjs.js +176 -29
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +176 -29
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapgoCapacitorFastSqlPlugin/CapgoCapacitorFastSqlPlugin.swift +8 -2
- package/package.json +1 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web implementation of SQLConnection that calls the Capacitor plugin directly,
|
|
3
|
+
* avoiding HTTP network requests which are not available on the web platform.
|
|
4
|
+
*/
|
|
5
|
+
export class WebSQLConnection {
|
|
6
|
+
constructor(database, plugin) {
|
|
7
|
+
this.inTransaction = false;
|
|
8
|
+
this.database = database;
|
|
9
|
+
this.plugin = plugin;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get the database name
|
|
13
|
+
*/
|
|
14
|
+
getDatabaseName() {
|
|
15
|
+
return this.database;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Execute a SQL query via the Capacitor plugin
|
|
19
|
+
*
|
|
20
|
+
* @param statement - SQL statement to execute
|
|
21
|
+
* @param params - Parameters to bind to the statement
|
|
22
|
+
* @returns Query results
|
|
23
|
+
*/
|
|
24
|
+
async execute(statement, params) {
|
|
25
|
+
return this.plugin.execute({ database: this.database, statement, params });
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Execute multiple SQL statements in a batch, sequentially
|
|
29
|
+
*
|
|
30
|
+
* @param operations - Array of SQL operations to execute
|
|
31
|
+
* @returns Array of results for each operation
|
|
32
|
+
*/
|
|
33
|
+
async executeBatch(operations) {
|
|
34
|
+
const results = [];
|
|
35
|
+
for (const op of operations) {
|
|
36
|
+
results.push(await this.execute(op.statement, op.params));
|
|
37
|
+
}
|
|
38
|
+
return results;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Begin a transaction
|
|
42
|
+
*
|
|
43
|
+
* @param isolationLevel - Optional isolation level
|
|
44
|
+
*/
|
|
45
|
+
async beginTransaction(isolationLevel) {
|
|
46
|
+
if (this.inTransaction) {
|
|
47
|
+
throw new Error('Transaction already in progress');
|
|
48
|
+
}
|
|
49
|
+
await this.plugin.beginTransaction({ database: this.database, isolationLevel });
|
|
50
|
+
this.inTransaction = true;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Commit the current transaction
|
|
54
|
+
*/
|
|
55
|
+
async commit() {
|
|
56
|
+
if (!this.inTransaction) {
|
|
57
|
+
throw new Error('No transaction in progress');
|
|
58
|
+
}
|
|
59
|
+
await this.plugin.commitTransaction({ database: this.database });
|
|
60
|
+
this.inTransaction = false;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Rollback the current transaction
|
|
64
|
+
*/
|
|
65
|
+
async rollback() {
|
|
66
|
+
if (!this.inTransaction) {
|
|
67
|
+
throw new Error('No transaction in progress');
|
|
68
|
+
}
|
|
69
|
+
await this.plugin.rollbackTransaction({ database: this.database });
|
|
70
|
+
this.inTransaction = false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Execute operations within a transaction automatically
|
|
74
|
+
*
|
|
75
|
+
* @param callback - Function containing operations to execute
|
|
76
|
+
* @param isolationLevel - Optional isolation level
|
|
77
|
+
*/
|
|
78
|
+
async transaction(callback, isolationLevel) {
|
|
79
|
+
await this.beginTransaction(isolationLevel);
|
|
80
|
+
try {
|
|
81
|
+
const result = await callback(this);
|
|
82
|
+
await this.commit();
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (this.inTransaction) {
|
|
87
|
+
try {
|
|
88
|
+
await this.rollback();
|
|
89
|
+
}
|
|
90
|
+
catch (rollbackError) {
|
|
91
|
+
throw new Error(`Transaction failed and rollback failed: ${String(rollbackError)}; original error: ${String(error)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Query helper for SELECT statements
|
|
99
|
+
*
|
|
100
|
+
* @param statement - SELECT statement
|
|
101
|
+
* @param params - Query parameters
|
|
102
|
+
* @returns Array of rows
|
|
103
|
+
*/
|
|
104
|
+
async query(statement, params) {
|
|
105
|
+
const result = await this.execute(statement, params);
|
|
106
|
+
return result.rows;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
110
|
+
*
|
|
111
|
+
* @param statement - SQL statement
|
|
112
|
+
* @param params - Statement parameters
|
|
113
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
114
|
+
*/
|
|
115
|
+
async run(statement, params) {
|
|
116
|
+
const result = await this.execute(statement, params);
|
|
117
|
+
return {
|
|
118
|
+
rowsAffected: result.rowsAffected,
|
|
119
|
+
insertId: result.insertId,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
package/dist/esm/web.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { WebPlugin } from '@capacitor/core';
|
|
2
|
-
import type { CapgoCapacitorFastSqlPlugin, SQLConnectionOptions, SQLResult, SQLValue, IsolationLevel } from './definitions';
|
|
2
|
+
import type { CapgoCapacitorFastSqlPlugin, SQLConnectionOptions, SQLResult, SQLValue, IsolationLevel, WebConfig } from './definitions';
|
|
3
3
|
/**
|
|
4
4
|
* Web implementation using sql.js (SQLite compiled to WebAssembly)
|
|
5
5
|
*
|
|
@@ -10,9 +10,16 @@ export declare class CapgoCapacitorFastSqlWeb extends WebPlugin implements Capgo
|
|
|
10
10
|
private databases;
|
|
11
11
|
private sqlPromise;
|
|
12
12
|
private nextPort;
|
|
13
|
+
private webConfig;
|
|
13
14
|
constructor();
|
|
14
15
|
/**
|
|
15
|
-
*
|
|
16
|
+
* Configure web-specific options (no-op on native platforms).
|
|
17
|
+
* Must be called before connect() when using locally bundled sql.js files.
|
|
18
|
+
*/
|
|
19
|
+
configureWeb(config: WebConfig): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Load sql.js library (lazy – called on first connect()).
|
|
22
|
+
* Returns the cached promise so callers can await it directly.
|
|
16
23
|
*/
|
|
17
24
|
private loadSqlJs;
|
|
18
25
|
connect(options: SQLConnectionOptions): Promise<{
|
package/dist/esm/web.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EACV,2BAA2B,EAC3B,oBAAoB,EACpB,SAAS,EACT,QAAQ,EACR,cAAc,
|
|
1
|
+
{"version":3,"file":"web.d.ts","sourceRoot":"","sources":["../../src/web.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EACV,2BAA2B,EAC3B,oBAAoB,EACpB,SAAS,EACT,QAAQ,EACR,cAAc,EACd,SAAS,EACV,MAAM,eAAe,CAAC;AAKvB;;;;;GAKG;AACH,qBAAa,wBAAyB,SAAQ,SAAU,YAAW,2BAA2B;IAC5F,OAAO,CAAC,SAAS,CAAoE;IACrF,OAAO,CAAC,UAAU,CAA6B;IAC/C,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,SAAS,CAAiB;;IAMlC;;;OAGG;IACG,YAAY,CAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAC,IAAI,CAAC;IAMpD;;;OAGG;IACH,OAAO,CAAC,SAAS;IA4BX,OAAO,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IAyBlG,UAAU,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAcxD,aAAa,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAYtF,OAAO,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAC;QAAC,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,SAAS,CAAC;IAiClG,gBAAgB,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,cAAc,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAO/F,iBAAiB,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAO/D,mBAAmB,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAOvE;;OAEG;IACH,OAAO,CAAC,aAAa;IAMrB;;OAEG;IACH,OAAO,CAAC,eAAe;IAYvB;;OAEG;YACW,eAAe;IAgC7B;;OAEG;YACW,iBAAiB;IAgCzB,gBAAgB,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAGvD"}
|
package/dist/esm/web.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
const DEFAULT_SQLJS_URL = 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.js';
|
|
3
|
+
const DEFAULT_WASM_URL = 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.wasm';
|
|
2
4
|
/**
|
|
3
5
|
* Web implementation using sql.js (SQLite compiled to WebAssembly)
|
|
4
6
|
*
|
|
@@ -11,36 +13,50 @@ export class CapgoCapacitorFastSqlWeb extends WebPlugin {
|
|
|
11
13
|
this.databases = new Map();
|
|
12
14
|
this.sqlPromise = null;
|
|
13
15
|
this.nextPort = 9000;
|
|
14
|
-
this.
|
|
16
|
+
this.webConfig = {};
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
|
-
*
|
|
19
|
+
* Configure web-specific options (no-op on native platforms).
|
|
20
|
+
* Must be called before connect() when using locally bundled sql.js files.
|
|
18
21
|
*/
|
|
19
|
-
async
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
this.sqlPromise =
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
22
|
+
async configureWeb(config) {
|
|
23
|
+
this.webConfig = config;
|
|
24
|
+
// Reset any in-progress load so the next connect() uses the new config
|
|
25
|
+
this.sqlPromise = null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Load sql.js library (lazy – called on first connect()).
|
|
29
|
+
* Returns the cached promise so callers can await it directly.
|
|
30
|
+
*/
|
|
31
|
+
loadSqlJs() {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
if (!this.sqlPromise) {
|
|
34
|
+
const jsUrl = (_a = this.webConfig.sqlJsUrl) !== null && _a !== void 0 ? _a : DEFAULT_SQLJS_URL;
|
|
35
|
+
const wasmUrl = (_b = this.webConfig.wasmUrl) !== null && _b !== void 0 ? _b : DEFAULT_WASM_URL;
|
|
36
|
+
this.sqlPromise = new Promise((resolve, reject) => {
|
|
37
|
+
try {
|
|
38
|
+
// Load sql.js from the configured URL (CDN or local bundle)
|
|
39
|
+
const script = document.createElement('script');
|
|
40
|
+
script.src = jsUrl;
|
|
41
|
+
script.onload = async () => {
|
|
42
|
+
const initSqlJs = window.initSqlJs;
|
|
43
|
+
const SQL = await initSqlJs({
|
|
44
|
+
locateFile: (file) => (file.endsWith('.wasm') ? wasmUrl : file),
|
|
45
|
+
});
|
|
46
|
+
resolve(SQL);
|
|
47
|
+
};
|
|
48
|
+
script.onerror = reject;
|
|
49
|
+
document.head.appendChild(script);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
reject(error);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
return this.sqlPromise;
|
|
41
57
|
}
|
|
42
58
|
async connect(options) {
|
|
43
|
-
const SQL = await this.
|
|
59
|
+
const SQL = await this.loadSqlJs();
|
|
44
60
|
// Check if database already exists in IndexedDB
|
|
45
61
|
const savedData = await this.loadFromIndexedDB(options.database);
|
|
46
62
|
let db;
|
package/dist/plugin.cjs.js
CHANGED
|
@@ -23,7 +23,7 @@ const CapgoCapacitorFastSql = core.registerPlugin('CapgoCapacitorFastSql', {
|
|
|
23
23
|
*
|
|
24
24
|
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
25
25
|
*/
|
|
26
|
-
class
|
|
26
|
+
class NativeSQLConnection {
|
|
27
27
|
constructor(database, port, token) {
|
|
28
28
|
this.inTransaction = false;
|
|
29
29
|
this.database = database;
|
|
@@ -261,6 +261,129 @@ class SQLConnection {
|
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
+
/**
|
|
265
|
+
* Web implementation of SQLConnection that calls the Capacitor plugin directly,
|
|
266
|
+
* avoiding HTTP network requests which are not available on the web platform.
|
|
267
|
+
*/
|
|
268
|
+
class WebSQLConnection {
|
|
269
|
+
constructor(database, plugin) {
|
|
270
|
+
this.inTransaction = false;
|
|
271
|
+
this.database = database;
|
|
272
|
+
this.plugin = plugin;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Get the database name
|
|
276
|
+
*/
|
|
277
|
+
getDatabaseName() {
|
|
278
|
+
return this.database;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Execute a SQL query via the Capacitor plugin
|
|
282
|
+
*
|
|
283
|
+
* @param statement - SQL statement to execute
|
|
284
|
+
* @param params - Parameters to bind to the statement
|
|
285
|
+
* @returns Query results
|
|
286
|
+
*/
|
|
287
|
+
async execute(statement, params) {
|
|
288
|
+
return this.plugin.execute({ database: this.database, statement, params });
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Execute multiple SQL statements in a batch, sequentially
|
|
292
|
+
*
|
|
293
|
+
* @param operations - Array of SQL operations to execute
|
|
294
|
+
* @returns Array of results for each operation
|
|
295
|
+
*/
|
|
296
|
+
async executeBatch(operations) {
|
|
297
|
+
const results = [];
|
|
298
|
+
for (const op of operations) {
|
|
299
|
+
results.push(await this.execute(op.statement, op.params));
|
|
300
|
+
}
|
|
301
|
+
return results;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Begin a transaction
|
|
305
|
+
*
|
|
306
|
+
* @param isolationLevel - Optional isolation level
|
|
307
|
+
*/
|
|
308
|
+
async beginTransaction(isolationLevel) {
|
|
309
|
+
if (this.inTransaction) {
|
|
310
|
+
throw new Error('Transaction already in progress');
|
|
311
|
+
}
|
|
312
|
+
await this.plugin.beginTransaction({ database: this.database, isolationLevel });
|
|
313
|
+
this.inTransaction = true;
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Commit the current transaction
|
|
317
|
+
*/
|
|
318
|
+
async commit() {
|
|
319
|
+
if (!this.inTransaction) {
|
|
320
|
+
throw new Error('No transaction in progress');
|
|
321
|
+
}
|
|
322
|
+
await this.plugin.commitTransaction({ database: this.database });
|
|
323
|
+
this.inTransaction = false;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Rollback the current transaction
|
|
327
|
+
*/
|
|
328
|
+
async rollback() {
|
|
329
|
+
if (!this.inTransaction) {
|
|
330
|
+
throw new Error('No transaction in progress');
|
|
331
|
+
}
|
|
332
|
+
await this.plugin.rollbackTransaction({ database: this.database });
|
|
333
|
+
this.inTransaction = false;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Execute operations within a transaction automatically
|
|
337
|
+
*
|
|
338
|
+
* @param callback - Function containing operations to execute
|
|
339
|
+
* @param isolationLevel - Optional isolation level
|
|
340
|
+
*/
|
|
341
|
+
async transaction(callback, isolationLevel) {
|
|
342
|
+
await this.beginTransaction(isolationLevel);
|
|
343
|
+
try {
|
|
344
|
+
const result = await callback(this);
|
|
345
|
+
await this.commit();
|
|
346
|
+
return result;
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
if (this.inTransaction) {
|
|
350
|
+
try {
|
|
351
|
+
await this.rollback();
|
|
352
|
+
}
|
|
353
|
+
catch (rollbackError) {
|
|
354
|
+
throw new Error(`Transaction failed and rollback failed: ${String(rollbackError)}; original error: ${String(error)}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
throw error;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Query helper for SELECT statements
|
|
362
|
+
*
|
|
363
|
+
* @param statement - SELECT statement
|
|
364
|
+
* @param params - Query parameters
|
|
365
|
+
* @returns Array of rows
|
|
366
|
+
*/
|
|
367
|
+
async query(statement, params) {
|
|
368
|
+
const result = await this.execute(statement, params);
|
|
369
|
+
return result.rows;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
373
|
+
*
|
|
374
|
+
* @param statement - SQL statement
|
|
375
|
+
* @param params - Statement parameters
|
|
376
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
377
|
+
*/
|
|
378
|
+
async run(statement, params) {
|
|
379
|
+
const result = await this.execute(statement, params);
|
|
380
|
+
return {
|
|
381
|
+
rowsAffected: result.rowsAffected,
|
|
382
|
+
insertId: result.insertId,
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
264
387
|
/**
|
|
265
388
|
* FastSQL - High-level API for managing SQL connections
|
|
266
389
|
*
|
|
@@ -282,8 +405,15 @@ class FastSQL {
|
|
|
282
405
|
}
|
|
283
406
|
// Connect via native plugin
|
|
284
407
|
const info = await CapgoCapacitorFastSql.connect(options);
|
|
285
|
-
// Create connection instance
|
|
286
|
-
|
|
408
|
+
// Create connection instance appropriate for the current platform
|
|
409
|
+
let connection;
|
|
410
|
+
const platform = core.Capacitor.getPlatform();
|
|
411
|
+
if (platform === 'android' || platform === 'ios') {
|
|
412
|
+
connection = new NativeSQLConnection(info.database, info.port, info.token);
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
connection = new WebSQLConnection(info.database, CapgoCapacitorFastSql);
|
|
416
|
+
}
|
|
287
417
|
// Store connection
|
|
288
418
|
this.connections.set(options.database, connection);
|
|
289
419
|
return connection;
|
|
@@ -513,6 +643,8 @@ class KeyValueStore {
|
|
|
513
643
|
}
|
|
514
644
|
}
|
|
515
645
|
|
|
646
|
+
const DEFAULT_SQLJS_URL = 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.js';
|
|
647
|
+
const DEFAULT_WASM_URL = 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.wasm';
|
|
516
648
|
/**
|
|
517
649
|
* Web implementation using sql.js (SQLite compiled to WebAssembly)
|
|
518
650
|
*
|
|
@@ -525,36 +657,50 @@ class CapgoCapacitorFastSqlWeb extends core.WebPlugin {
|
|
|
525
657
|
this.databases = new Map();
|
|
526
658
|
this.sqlPromise = null;
|
|
527
659
|
this.nextPort = 9000;
|
|
528
|
-
this.
|
|
660
|
+
this.webConfig = {};
|
|
529
661
|
}
|
|
530
662
|
/**
|
|
531
|
-
*
|
|
663
|
+
* Configure web-specific options (no-op on native platforms).
|
|
664
|
+
* Must be called before connect() when using locally bundled sql.js files.
|
|
532
665
|
*/
|
|
533
|
-
async
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
this.sqlPromise =
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
666
|
+
async configureWeb(config) {
|
|
667
|
+
this.webConfig = config;
|
|
668
|
+
// Reset any in-progress load so the next connect() uses the new config
|
|
669
|
+
this.sqlPromise = null;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Load sql.js library (lazy – called on first connect()).
|
|
673
|
+
* Returns the cached promise so callers can await it directly.
|
|
674
|
+
*/
|
|
675
|
+
loadSqlJs() {
|
|
676
|
+
var _a, _b;
|
|
677
|
+
if (!this.sqlPromise) {
|
|
678
|
+
const jsUrl = (_a = this.webConfig.sqlJsUrl) !== null && _a !== void 0 ? _a : DEFAULT_SQLJS_URL;
|
|
679
|
+
const wasmUrl = (_b = this.webConfig.wasmUrl) !== null && _b !== void 0 ? _b : DEFAULT_WASM_URL;
|
|
680
|
+
this.sqlPromise = new Promise((resolve, reject) => {
|
|
681
|
+
try {
|
|
682
|
+
// Load sql.js from the configured URL (CDN or local bundle)
|
|
683
|
+
const script = document.createElement('script');
|
|
684
|
+
script.src = jsUrl;
|
|
685
|
+
script.onload = async () => {
|
|
686
|
+
const initSqlJs = window.initSqlJs;
|
|
687
|
+
const SQL = await initSqlJs({
|
|
688
|
+
locateFile: (file) => (file.endsWith('.wasm') ? wasmUrl : file),
|
|
689
|
+
});
|
|
690
|
+
resolve(SQL);
|
|
691
|
+
};
|
|
692
|
+
script.onerror = reject;
|
|
693
|
+
document.head.appendChild(script);
|
|
694
|
+
}
|
|
695
|
+
catch (error) {
|
|
696
|
+
reject(error);
|
|
697
|
+
}
|
|
698
|
+
});
|
|
699
|
+
}
|
|
700
|
+
return this.sqlPromise;
|
|
555
701
|
}
|
|
556
702
|
async connect(options) {
|
|
557
|
-
const SQL = await this.
|
|
703
|
+
const SQL = await this.loadSqlJs();
|
|
558
704
|
// Check if database already exists in IndexedDB
|
|
559
705
|
const savedData = await this.loadFromIndexedDB(options.database);
|
|
560
706
|
let db;
|
|
@@ -735,5 +881,6 @@ var web = /*#__PURE__*/Object.freeze({
|
|
|
735
881
|
exports.CapgoCapacitorFastSql = CapgoCapacitorFastSql;
|
|
736
882
|
exports.FastSQL = FastSQL;
|
|
737
883
|
exports.KeyValueStore = KeyValueStore;
|
|
738
|
-
exports.
|
|
884
|
+
exports.NativeSQLConnection = NativeSQLConnection;
|
|
885
|
+
exports.WebSQLConnection = WebSQLConnection;
|
|
739
886
|
//# sourceMappingURL=plugin.cjs.js.map
|