@capgo/capacitor-fast-sql 7.0.1
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/CapgoCapacitorFastSql.podspec +18 -0
- package/Package.swift +30 -0
- package/README.md +340 -0
- package/android/build.gradle +60 -0
- package/android/src/main/AndroidManifest.xml +4 -0
- package/android/src/main/java/app/capgo/capacitor/fastsql/CapgoCapacitorFastSqlPlugin.java +220 -0
- package/android/src/main/java/app/capgo/capacitor/fastsql/SQLDatabase.java +231 -0
- package/android/src/main/java/app/capgo/capacitor/fastsql/SQLHTTPServer.java +229 -0
- package/dist/esm/definitions.d.ts +225 -0
- package/dist/esm/definitions.d.ts.map +1 -0
- package/dist/esm/definitions.js +10 -0
- package/dist/esm/helpers.d.ts +7 -0
- package/dist/esm/helpers.d.ts.map +1 -0
- package/dist/esm/helpers.js +6 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/native-sql.d.ts +42 -0
- package/dist/esm/native-sql.d.ts.map +1 -0
- package/dist/esm/native-sql.js +69 -0
- package/dist/esm/plugin.d.ts +3 -0
- package/dist/esm/plugin.d.ts.map +1 -0
- package/dist/esm/plugin.js +4 -0
- package/dist/esm/sql-connection.d.ts +94 -0
- package/dist/esm/sql-connection.d.ts.map +1 -0
- package/dist/esm/sql-connection.js +246 -0
- package/dist/esm/web.d.ts +67 -0
- package/dist/esm/web.d.ts.map +1 -0
- package/dist/esm/web.js +215 -0
- package/dist/plugin.cjs.js +557 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +560 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/CapgoCapacitorFastSqlPlugin/CapgoCapacitorFastSqlPlugin.swift +202 -0
- package/ios/Sources/CapgoCapacitorFastSqlPlugin/SQLDatabase.swift +215 -0
- package/ios/Sources/CapgoCapacitorFastSqlPlugin/SQLHTTPServer.swift +311 -0
- package/package.json +90 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helper classes for easier SQL operations
|
|
3
|
+
* Import these separately: import { NativeSQL, SQLConnection } from '@capgo/capacitor-native-sql/helpers';
|
|
4
|
+
*/
|
|
5
|
+
export { NativeSQL } from './native-sql';
|
|
6
|
+
export { SQLConnection } from './sql-connection';
|
|
7
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,OAAO,EAAE,uBAAuB,EAAE,MAAM,UAAU,CAAC;AAGnD,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { SQLConnectionOptions } from './definitions';
|
|
2
|
+
import { SQLConnection } from './sql-connection';
|
|
3
|
+
/**
|
|
4
|
+
* NativeSQL - High-level API for managing SQL connections
|
|
5
|
+
*
|
|
6
|
+
* This class provides a convenient interface for opening/closing database connections
|
|
7
|
+
* and managing multiple databases simultaneously.
|
|
8
|
+
*/
|
|
9
|
+
export declare class NativeSQL {
|
|
10
|
+
private static connections;
|
|
11
|
+
/**
|
|
12
|
+
* Open a database connection
|
|
13
|
+
*
|
|
14
|
+
* @param options - Connection options
|
|
15
|
+
* @returns SQLConnection instance for executing queries
|
|
16
|
+
*/
|
|
17
|
+
static connect(options: SQLConnectionOptions): Promise<SQLConnection>;
|
|
18
|
+
/**
|
|
19
|
+
* Close a database connection
|
|
20
|
+
*
|
|
21
|
+
* @param database - Database name to close
|
|
22
|
+
*/
|
|
23
|
+
static disconnect(database: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Get an existing connection
|
|
26
|
+
*
|
|
27
|
+
* @param database - Database name
|
|
28
|
+
* @returns SQLConnection instance or null if not connected
|
|
29
|
+
*/
|
|
30
|
+
static getConnection(database: string): SQLConnection | null;
|
|
31
|
+
/**
|
|
32
|
+
* Close all open connections
|
|
33
|
+
*/
|
|
34
|
+
static disconnectAll(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Get list of all open database connections
|
|
37
|
+
*
|
|
38
|
+
* @returns Array of database names
|
|
39
|
+
*/
|
|
40
|
+
static getOpenDatabases(): string[];
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=native-sql.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native-sql.d.ts","sourceRoot":"","sources":["../../src/native-sql.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;;;GAKG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAC,WAAW,CAAyC;IAEnE;;;;;OAKG;WACU,OAAO,CAClB,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC,aAAa,CAAC;IAsBzB;;;;OAIG;WACU,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAaxD;;;;;OAKG;IACH,MAAM,CAAC,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI;IAI5D;;OAEG;WACU,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC;IAK3C;;;;OAIG;IACH,MAAM,CAAC,gBAAgB,IAAI,MAAM,EAAE;CAGpC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CapgoCapacitorNativeSql } from './plugin';
|
|
2
|
+
import { SQLConnection } from './sql-connection';
|
|
3
|
+
/**
|
|
4
|
+
* NativeSQL - High-level API for managing SQL connections
|
|
5
|
+
*
|
|
6
|
+
* This class provides a convenient interface for opening/closing database connections
|
|
7
|
+
* and managing multiple databases simultaneously.
|
|
8
|
+
*/
|
|
9
|
+
export class NativeSQL {
|
|
10
|
+
/**
|
|
11
|
+
* Open a database connection
|
|
12
|
+
*
|
|
13
|
+
* @param options - Connection options
|
|
14
|
+
* @returns SQLConnection instance for executing queries
|
|
15
|
+
*/
|
|
16
|
+
static async connect(options) {
|
|
17
|
+
// Check if already connected
|
|
18
|
+
if (this.connections.has(options.database)) {
|
|
19
|
+
return this.connections.get(options.database);
|
|
20
|
+
}
|
|
21
|
+
// Connect via native plugin
|
|
22
|
+
const info = await CapgoCapacitorNativeSql.connect(options);
|
|
23
|
+
// Create connection instance
|
|
24
|
+
const connection = new SQLConnection(info.database, info.port, info.token);
|
|
25
|
+
// Store connection
|
|
26
|
+
this.connections.set(options.database, connection);
|
|
27
|
+
return connection;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Close a database connection
|
|
31
|
+
*
|
|
32
|
+
* @param database - Database name to close
|
|
33
|
+
*/
|
|
34
|
+
static async disconnect(database) {
|
|
35
|
+
const connection = this.connections.get(database);
|
|
36
|
+
if (!connection) {
|
|
37
|
+
throw new Error(`Database '${database}' is not connected`);
|
|
38
|
+
}
|
|
39
|
+
// Disconnect via native plugin
|
|
40
|
+
await CapgoCapacitorNativeSql.disconnect({ database });
|
|
41
|
+
// Remove connection
|
|
42
|
+
this.connections.delete(database);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get an existing connection
|
|
46
|
+
*
|
|
47
|
+
* @param database - Database name
|
|
48
|
+
* @returns SQLConnection instance or null if not connected
|
|
49
|
+
*/
|
|
50
|
+
static getConnection(database) {
|
|
51
|
+
return this.connections.get(database) || null;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Close all open connections
|
|
55
|
+
*/
|
|
56
|
+
static async disconnectAll() {
|
|
57
|
+
const databases = Array.from(this.connections.keys());
|
|
58
|
+
await Promise.all(databases.map((db) => this.disconnect(db)));
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Get list of all open database connections
|
|
62
|
+
*
|
|
63
|
+
* @returns Array of database names
|
|
64
|
+
*/
|
|
65
|
+
static getOpenDatabases() {
|
|
66
|
+
return Array.from(this.connections.keys());
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
NativeSQL.connections = new Map();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/plugin.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,eAAe,CAAC;AAEnE,eAAO,MAAM,uBAAuB,+BAGhC,CAAC"}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { SQLValue, SQLRow, SQLResult, SQLBatchOperation } from './definitions';
|
|
2
|
+
import { IsolationLevel } from './definitions';
|
|
3
|
+
/**
|
|
4
|
+
* SQL Connection class that uses HTTP protocol for efficient communication
|
|
5
|
+
* with native SQLite databases, bypassing Capacitor's standard bridge.
|
|
6
|
+
*
|
|
7
|
+
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SQLConnection {
|
|
10
|
+
private port;
|
|
11
|
+
private token;
|
|
12
|
+
private database;
|
|
13
|
+
private baseUrl;
|
|
14
|
+
private inTransaction;
|
|
15
|
+
constructor(database: string, port: number, token: string);
|
|
16
|
+
/**
|
|
17
|
+
* Get the database name
|
|
18
|
+
*/
|
|
19
|
+
getDatabaseName(): string;
|
|
20
|
+
/**
|
|
21
|
+
* Execute a SQL query via HTTP protocol for optimal performance
|
|
22
|
+
*
|
|
23
|
+
* @param statement - SQL statement to execute
|
|
24
|
+
* @param params - Parameters to bind to the statement
|
|
25
|
+
* @returns Query results
|
|
26
|
+
*/
|
|
27
|
+
execute(statement: string, params?: SQLValue[]): Promise<SQLResult>;
|
|
28
|
+
/**
|
|
29
|
+
* Execute multiple SQL statements in a batch for better performance
|
|
30
|
+
*
|
|
31
|
+
* @param operations - Array of SQL operations to execute
|
|
32
|
+
* @returns Array of results for each operation
|
|
33
|
+
*/
|
|
34
|
+
executeBatch(operations: SQLBatchOperation[]): Promise<SQLResult[]>;
|
|
35
|
+
/**
|
|
36
|
+
* Begin a transaction
|
|
37
|
+
*
|
|
38
|
+
* @param isolationLevel - Optional isolation level
|
|
39
|
+
*/
|
|
40
|
+
beginTransaction(isolationLevel?: IsolationLevel): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Commit the current transaction
|
|
43
|
+
*/
|
|
44
|
+
commit(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Rollback the current transaction
|
|
47
|
+
*/
|
|
48
|
+
rollback(): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Execute operations within a transaction automatically
|
|
51
|
+
*
|
|
52
|
+
* @param callback - Function containing operations to execute
|
|
53
|
+
* @param isolationLevel - Optional isolation level
|
|
54
|
+
*/
|
|
55
|
+
transaction<T>(callback: (conn: SQLConnection) => Promise<T>, isolationLevel?: IsolationLevel): Promise<T>;
|
|
56
|
+
/**
|
|
57
|
+
* Query helper for SELECT statements
|
|
58
|
+
*
|
|
59
|
+
* @param statement - SELECT statement
|
|
60
|
+
* @param params - Query parameters
|
|
61
|
+
* @returns Array of rows
|
|
62
|
+
*/
|
|
63
|
+
query(statement: string, params?: SQLValue[]): Promise<SQLRow[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
66
|
+
*
|
|
67
|
+
* @param statement - SQL statement
|
|
68
|
+
* @param params - Statement parameters
|
|
69
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
70
|
+
*/
|
|
71
|
+
run(statement: string, params?: SQLValue[]): Promise<{
|
|
72
|
+
rowsAffected: number;
|
|
73
|
+
insertId?: number;
|
|
74
|
+
}>;
|
|
75
|
+
/**
|
|
76
|
+
* Serialize parameters for transmission
|
|
77
|
+
* Binary data (Uint8Array) is converted to base64 for JSON transport
|
|
78
|
+
*/
|
|
79
|
+
private serializeParams;
|
|
80
|
+
/**
|
|
81
|
+
* Deserialize result from server
|
|
82
|
+
* Base64-encoded binary data is converted back to Uint8Array
|
|
83
|
+
*/
|
|
84
|
+
private deserializeResult;
|
|
85
|
+
/**
|
|
86
|
+
* Convert Uint8Array to base64 string
|
|
87
|
+
*/
|
|
88
|
+
private uint8ArrayToBase64;
|
|
89
|
+
/**
|
|
90
|
+
* Convert base64 string to Uint8Array
|
|
91
|
+
*/
|
|
92
|
+
private base64ToUint8Array;
|
|
93
|
+
}
|
|
94
|
+
//# sourceMappingURL=sql-connection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-connection.d.ts","sourceRoot":"","sources":["../../src/sql-connection.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,QAAQ,EACR,MAAM,EACN,SAAS,EACT,iBAAiB,EAClB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE/C;;;;;GAKG;AACH,qBAAa,aAAa;IACxB,OAAO,CAAC,IAAI,CAAS;IACrB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,QAAQ,CAAS;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,aAAa,CAAS;gBAElB,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM;IAOzD;;OAEG;IACH,eAAe,IAAI,MAAM;IAIzB;;;;;;OAMG;IACG,OAAO,CACX,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,QAAQ,EAAE,GAClB,OAAO,CAAC,SAAS,CAAC;IAuBrB;;;;;OAKG;IACG,YAAY,CAChB,UAAU,EAAE,iBAAiB,EAAE,GAC9B,OAAO,CAAC,SAAS,EAAE,CAAC;IAyBvB;;;;OAIG;IACG,gBAAgB,CACpB,cAAc,CAAC,EAAE,cAAc,GAC9B,OAAO,CAAC,IAAI,CAAC;IAyBhB;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB7B;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAqB/B;;;;;OAKG;IACG,WAAW,CAAC,CAAC,EACjB,QAAQ,EAAE,CAAC,IAAI,EAAE,aAAa,KAAK,OAAO,CAAC,CAAC,CAAC,EAC7C,cAAc,CAAC,EAAE,cAAc,GAC9B,OAAO,CAAC,CAAC,CAAC;IAYb;;;;;;OAMG;IACG,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKtE;;;;;;OAMG;IACG,GAAG,CACP,SAAS,EAAE,MAAM,EACjB,MAAM,CAAC,EAAE,QAAQ,EAAE,GAClB,OAAO,CAAC;QAAE,YAAY,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAQvD;;;OAGG;IACH,OAAO,CAAC,eAAe;IAYvB;;;OAGG;IACH,OAAO,CAAC,iBAAiB;IAwBzB;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAQ1B;;OAEG;IACH,OAAO,CAAC,kBAAkB;CAQ3B"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { IsolationLevel } from './definitions';
|
|
2
|
+
/**
|
|
3
|
+
* SQL Connection class that uses HTTP protocol for efficient communication
|
|
4
|
+
* with native SQLite databases, bypassing Capacitor's standard bridge.
|
|
5
|
+
*
|
|
6
|
+
* Inspired by capacitor-blob-writer's approach to avoid serialization overhead.
|
|
7
|
+
*/
|
|
8
|
+
export class SQLConnection {
|
|
9
|
+
constructor(database, port, token) {
|
|
10
|
+
this.inTransaction = false;
|
|
11
|
+
this.database = database;
|
|
12
|
+
this.port = port;
|
|
13
|
+
this.token = token;
|
|
14
|
+
this.baseUrl = `http://localhost:${port}`;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Get the database name
|
|
18
|
+
*/
|
|
19
|
+
getDatabaseName() {
|
|
20
|
+
return this.database;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Execute a SQL query via HTTP protocol for optimal performance
|
|
24
|
+
*
|
|
25
|
+
* @param statement - SQL statement to execute
|
|
26
|
+
* @param params - Parameters to bind to the statement
|
|
27
|
+
* @returns Query results
|
|
28
|
+
*/
|
|
29
|
+
async execute(statement, params) {
|
|
30
|
+
const response = await fetch(`${this.baseUrl}/execute`, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/json',
|
|
34
|
+
Authorization: `Bearer ${this.token}`,
|
|
35
|
+
'X-Database': this.database,
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
statement,
|
|
39
|
+
params: params ? this.serializeParams(params) : [],
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const error = await response.text();
|
|
44
|
+
throw new Error(`SQL execution failed: ${error}`);
|
|
45
|
+
}
|
|
46
|
+
const result = await response.json();
|
|
47
|
+
return this.deserializeResult(result);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Execute multiple SQL statements in a batch for better performance
|
|
51
|
+
*
|
|
52
|
+
* @param operations - Array of SQL operations to execute
|
|
53
|
+
* @returns Array of results for each operation
|
|
54
|
+
*/
|
|
55
|
+
async executeBatch(operations) {
|
|
56
|
+
const response = await fetch(`${this.baseUrl}/batch`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: {
|
|
59
|
+
'Content-Type': 'application/json',
|
|
60
|
+
Authorization: `Bearer ${this.token}`,
|
|
61
|
+
'X-Database': this.database,
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
operations: operations.map((op) => ({
|
|
65
|
+
statement: op.statement,
|
|
66
|
+
params: op.params ? this.serializeParams(op.params) : [],
|
|
67
|
+
})),
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const error = await response.text();
|
|
72
|
+
throw new Error(`SQL batch execution failed: ${error}`);
|
|
73
|
+
}
|
|
74
|
+
const results = await response.json();
|
|
75
|
+
return results.map((r) => this.deserializeResult(r));
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Begin a transaction
|
|
79
|
+
*
|
|
80
|
+
* @param isolationLevel - Optional isolation level
|
|
81
|
+
*/
|
|
82
|
+
async beginTransaction(isolationLevel) {
|
|
83
|
+
if (this.inTransaction) {
|
|
84
|
+
throw new Error('Transaction already in progress');
|
|
85
|
+
}
|
|
86
|
+
const response = await fetch(`${this.baseUrl}/transaction/begin`, {
|
|
87
|
+
method: 'POST',
|
|
88
|
+
headers: {
|
|
89
|
+
'Content-Type': 'application/json',
|
|
90
|
+
Authorization: `Bearer ${this.token}`,
|
|
91
|
+
'X-Database': this.database,
|
|
92
|
+
},
|
|
93
|
+
body: JSON.stringify({
|
|
94
|
+
isolationLevel: isolationLevel || IsolationLevel.Serializable,
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
if (!response.ok) {
|
|
98
|
+
const error = await response.text();
|
|
99
|
+
throw new Error(`Failed to begin transaction: ${error}`);
|
|
100
|
+
}
|
|
101
|
+
this.inTransaction = true;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Commit the current transaction
|
|
105
|
+
*/
|
|
106
|
+
async commit() {
|
|
107
|
+
if (!this.inTransaction) {
|
|
108
|
+
throw new Error('No transaction in progress');
|
|
109
|
+
}
|
|
110
|
+
const response = await fetch(`${this.baseUrl}/transaction/commit`, {
|
|
111
|
+
method: 'POST',
|
|
112
|
+
headers: {
|
|
113
|
+
Authorization: `Bearer ${this.token}`,
|
|
114
|
+
'X-Database': this.database,
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
if (!response.ok) {
|
|
118
|
+
const error = await response.text();
|
|
119
|
+
throw new Error(`Failed to commit transaction: ${error}`);
|
|
120
|
+
}
|
|
121
|
+
this.inTransaction = false;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Rollback the current transaction
|
|
125
|
+
*/
|
|
126
|
+
async rollback() {
|
|
127
|
+
if (!this.inTransaction) {
|
|
128
|
+
throw new Error('No transaction in progress');
|
|
129
|
+
}
|
|
130
|
+
const response = await fetch(`${this.baseUrl}/transaction/rollback`, {
|
|
131
|
+
method: 'POST',
|
|
132
|
+
headers: {
|
|
133
|
+
Authorization: `Bearer ${this.token}`,
|
|
134
|
+
'X-Database': this.database,
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
if (!response.ok) {
|
|
138
|
+
const error = await response.text();
|
|
139
|
+
throw new Error(`Failed to rollback transaction: ${error}`);
|
|
140
|
+
}
|
|
141
|
+
this.inTransaction = false;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Execute operations within a transaction automatically
|
|
145
|
+
*
|
|
146
|
+
* @param callback - Function containing operations to execute
|
|
147
|
+
* @param isolationLevel - Optional isolation level
|
|
148
|
+
*/
|
|
149
|
+
async transaction(callback, isolationLevel) {
|
|
150
|
+
await this.beginTransaction(isolationLevel);
|
|
151
|
+
try {
|
|
152
|
+
const result = await callback(this);
|
|
153
|
+
await this.commit();
|
|
154
|
+
return result;
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
await this.rollback();
|
|
158
|
+
throw error;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Query helper for SELECT statements
|
|
163
|
+
*
|
|
164
|
+
* @param statement - SELECT statement
|
|
165
|
+
* @param params - Query parameters
|
|
166
|
+
* @returns Array of rows
|
|
167
|
+
*/
|
|
168
|
+
async query(statement, params) {
|
|
169
|
+
const result = await this.execute(statement, params);
|
|
170
|
+
return result.rows;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Execute helper for INSERT/UPDATE/DELETE statements
|
|
174
|
+
*
|
|
175
|
+
* @param statement - SQL statement
|
|
176
|
+
* @param params - Statement parameters
|
|
177
|
+
* @returns Number of affected rows and insert ID if applicable
|
|
178
|
+
*/
|
|
179
|
+
async run(statement, params) {
|
|
180
|
+
const result = await this.execute(statement, params);
|
|
181
|
+
return {
|
|
182
|
+
rowsAffected: result.rowsAffected,
|
|
183
|
+
insertId: result.insertId,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Serialize parameters for transmission
|
|
188
|
+
* Binary data (Uint8Array) is converted to base64 for JSON transport
|
|
189
|
+
*/
|
|
190
|
+
serializeParams(params) {
|
|
191
|
+
return params.map((param) => {
|
|
192
|
+
if (param instanceof Uint8Array) {
|
|
193
|
+
return {
|
|
194
|
+
_type: 'binary',
|
|
195
|
+
_data: this.uint8ArrayToBase64(param),
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return param;
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Deserialize result from server
|
|
203
|
+
* Base64-encoded binary data is converted back to Uint8Array
|
|
204
|
+
*/
|
|
205
|
+
deserializeResult(result) {
|
|
206
|
+
return {
|
|
207
|
+
rows: result.rows.map((row) => {
|
|
208
|
+
const deserializedRow = {};
|
|
209
|
+
for (const [key, value] of Object.entries(row)) {
|
|
210
|
+
if (value &&
|
|
211
|
+
typeof value === 'object' &&
|
|
212
|
+
value._type === 'binary') {
|
|
213
|
+
deserializedRow[key] = this.base64ToUint8Array(value._data);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
deserializedRow[key] = value;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return deserializedRow;
|
|
220
|
+
}),
|
|
221
|
+
rowsAffected: result.rowsAffected || 0,
|
|
222
|
+
insertId: result.insertId,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Convert Uint8Array to base64 string
|
|
227
|
+
*/
|
|
228
|
+
uint8ArrayToBase64(bytes) {
|
|
229
|
+
let binary = '';
|
|
230
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
231
|
+
binary += String.fromCharCode(bytes[i]);
|
|
232
|
+
}
|
|
233
|
+
return btoa(binary);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Convert base64 string to Uint8Array
|
|
237
|
+
*/
|
|
238
|
+
base64ToUint8Array(base64) {
|
|
239
|
+
const binary = atob(base64);
|
|
240
|
+
const bytes = new Uint8Array(binary.length);
|
|
241
|
+
for (let i = 0; i < binary.length; i++) {
|
|
242
|
+
bytes[i] = binary.charCodeAt(i);
|
|
243
|
+
}
|
|
244
|
+
return bytes;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { WebPlugin } from '@capacitor/core';
|
|
2
|
+
import type { CapgoCapacitorNativeSqlPlugin, SQLConnectionOptions, SQLResult, SQLValue, IsolationLevel } from './definitions';
|
|
3
|
+
/**
|
|
4
|
+
* Web implementation using sql.js (SQLite compiled to WebAssembly)
|
|
5
|
+
*
|
|
6
|
+
* This provides a compatible API on the web platform, storing databases
|
|
7
|
+
* in IndexedDB for persistence.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CapgoCapacitorNativeSqlWeb extends WebPlugin implements CapgoCapacitorNativeSqlPlugin {
|
|
10
|
+
private databases;
|
|
11
|
+
private sqlPromise;
|
|
12
|
+
private nextPort;
|
|
13
|
+
constructor();
|
|
14
|
+
/**
|
|
15
|
+
* Load sql.js library
|
|
16
|
+
*/
|
|
17
|
+
private loadSqlJs;
|
|
18
|
+
connect(options: SQLConnectionOptions): Promise<{
|
|
19
|
+
port: number;
|
|
20
|
+
token: string;
|
|
21
|
+
database: string;
|
|
22
|
+
}>;
|
|
23
|
+
disconnect(options: {
|
|
24
|
+
database: string;
|
|
25
|
+
}): Promise<void>;
|
|
26
|
+
getServerInfo(options: {
|
|
27
|
+
database: string;
|
|
28
|
+
}): Promise<{
|
|
29
|
+
port: number;
|
|
30
|
+
token: string;
|
|
31
|
+
}>;
|
|
32
|
+
execute(options: {
|
|
33
|
+
database: string;
|
|
34
|
+
statement: string;
|
|
35
|
+
params?: SQLValue[];
|
|
36
|
+
}): Promise<SQLResult>;
|
|
37
|
+
beginTransaction(options: {
|
|
38
|
+
database: string;
|
|
39
|
+
isolationLevel?: IsolationLevel;
|
|
40
|
+
}): Promise<void>;
|
|
41
|
+
commitTransaction(options: {
|
|
42
|
+
database: string;
|
|
43
|
+
}): Promise<void>;
|
|
44
|
+
rollbackTransaction(options: {
|
|
45
|
+
database: string;
|
|
46
|
+
}): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Generate a random authentication token
|
|
49
|
+
*/
|
|
50
|
+
private generateToken;
|
|
51
|
+
/**
|
|
52
|
+
* Get last insert row ID
|
|
53
|
+
*/
|
|
54
|
+
private getLastInsertId;
|
|
55
|
+
/**
|
|
56
|
+
* Save database to IndexedDB
|
|
57
|
+
*/
|
|
58
|
+
private saveToIndexedDB;
|
|
59
|
+
/**
|
|
60
|
+
* Load database from IndexedDB
|
|
61
|
+
*/
|
|
62
|
+
private loadFromIndexedDB;
|
|
63
|
+
getPluginVersion(): Promise<{
|
|
64
|
+
version: string;
|
|
65
|
+
}>;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=web.d.ts.map
|
|
@@ -0,0 +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,6BAA6B,EAC7B,oBAAoB,EACpB,SAAS,EACT,QAAQ,EACR,cAAc,EACf,MAAM,eAAe,CAAC;AAEvB;;;;;GAKG;AACH,qBAAa,0BACX,SAAQ,SACR,YAAW,6BAA6B;IAExC,OAAO,CAAC,SAAS,CAGH;IACd,OAAO,CAAC,UAAU,CAA6B;IAC/C,OAAO,CAAC,QAAQ,CAAQ;;IAOxB;;OAEG;YACW,SAAS;IAyBjB,OAAO,CACX,OAAO,EAAE,oBAAoB,GAC5B,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IAyBvD,UAAU,CAAC,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAcxD,aAAa,CAAC,OAAO,EAAE;QAC3B,QAAQ,EAAE,MAAM,CAAC;KAClB,GAAG,OAAO,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC;IAYtC,OAAO,CAAC,OAAO,EAAE;QACrB,QAAQ,EAAE,MAAM,CAAC;QACjB,SAAS,EAAE,MAAM,CAAC;QAClB,MAAM,CAAC,EAAE,QAAQ,EAAE,CAAC;KACrB,GAAG,OAAO,CAAC,SAAS,CAAC;IAiChB,gBAAgB,CAAC,OAAO,EAAE;QAC9B,QAAQ,EAAE,MAAM,CAAC;QACjB,cAAc,CAAC,EAAE,cAAc,CAAC;KACjC,GAAG,OAAO,CAAC,IAAI,CAAC;IAOX,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;IAmC7B;;OAEG;YACW,iBAAiB;IAkCzB,gBAAgB,IAAI,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;CAGvD"}
|