@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.
Files changed (37) hide show
  1. package/CapgoCapacitorFastSql.podspec +18 -0
  2. package/Package.swift +30 -0
  3. package/README.md +340 -0
  4. package/android/build.gradle +60 -0
  5. package/android/src/main/AndroidManifest.xml +4 -0
  6. package/android/src/main/java/app/capgo/capacitor/fastsql/CapgoCapacitorFastSqlPlugin.java +220 -0
  7. package/android/src/main/java/app/capgo/capacitor/fastsql/SQLDatabase.java +231 -0
  8. package/android/src/main/java/app/capgo/capacitor/fastsql/SQLHTTPServer.java +229 -0
  9. package/dist/esm/definitions.d.ts +225 -0
  10. package/dist/esm/definitions.d.ts.map +1 -0
  11. package/dist/esm/definitions.js +10 -0
  12. package/dist/esm/helpers.d.ts +7 -0
  13. package/dist/esm/helpers.d.ts.map +1 -0
  14. package/dist/esm/helpers.js +6 -0
  15. package/dist/esm/index.d.ts +5 -0
  16. package/dist/esm/index.d.ts.map +1 -0
  17. package/dist/esm/index.js +5 -0
  18. package/dist/esm/native-sql.d.ts +42 -0
  19. package/dist/esm/native-sql.d.ts.map +1 -0
  20. package/dist/esm/native-sql.js +69 -0
  21. package/dist/esm/plugin.d.ts +3 -0
  22. package/dist/esm/plugin.d.ts.map +1 -0
  23. package/dist/esm/plugin.js +4 -0
  24. package/dist/esm/sql-connection.d.ts +94 -0
  25. package/dist/esm/sql-connection.d.ts.map +1 -0
  26. package/dist/esm/sql-connection.js +246 -0
  27. package/dist/esm/web.d.ts +67 -0
  28. package/dist/esm/web.d.ts.map +1 -0
  29. package/dist/esm/web.js +215 -0
  30. package/dist/plugin.cjs.js +557 -0
  31. package/dist/plugin.cjs.js.map +1 -0
  32. package/dist/plugin.js +560 -0
  33. package/dist/plugin.js.map +1 -0
  34. package/ios/Sources/CapgoCapacitorFastSqlPlugin/CapgoCapacitorFastSqlPlugin.swift +202 -0
  35. package/ios/Sources/CapgoCapacitorFastSqlPlugin/SQLDatabase.swift +215 -0
  36. package/ios/Sources/CapgoCapacitorFastSqlPlugin/SQLHTTPServer.swift +311 -0
  37. package/package.json +90 -0
@@ -0,0 +1,215 @@
1
+ import { WebPlugin } from '@capacitor/core';
2
+ /**
3
+ * Web implementation using sql.js (SQLite compiled to WebAssembly)
4
+ *
5
+ * This provides a compatible API on the web platform, storing databases
6
+ * in IndexedDB for persistence.
7
+ */
8
+ export class CapgoCapacitorNativeSqlWeb extends WebPlugin {
9
+ constructor() {
10
+ super();
11
+ this.databases = new Map();
12
+ this.sqlPromise = null;
13
+ this.nextPort = 9000;
14
+ this.loadSqlJs();
15
+ }
16
+ /**
17
+ * Load sql.js library
18
+ */
19
+ async loadSqlJs() {
20
+ if (this.sqlPromise)
21
+ return;
22
+ this.sqlPromise = new Promise(async (resolve, reject) => {
23
+ try {
24
+ // Load sql.js from CDN
25
+ const script = document.createElement('script');
26
+ script.src =
27
+ 'https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/sql-wasm.js';
28
+ script.onload = async () => {
29
+ const initSqlJs = window.initSqlJs;
30
+ const SQL = await initSqlJs({
31
+ locateFile: (file) => `https://cdnjs.cloudflare.com/ajax/libs/sql.js/1.8.0/${file}`,
32
+ });
33
+ resolve(SQL);
34
+ };
35
+ script.onerror = reject;
36
+ document.head.appendChild(script);
37
+ }
38
+ catch (error) {
39
+ reject(error);
40
+ }
41
+ });
42
+ }
43
+ async connect(options) {
44
+ const SQL = await this.sqlPromise;
45
+ // Check if database already exists in IndexedDB
46
+ const savedData = await this.loadFromIndexedDB(options.database);
47
+ let db;
48
+ if (savedData) {
49
+ db = new SQL.Database(savedData);
50
+ }
51
+ else {
52
+ db = new SQL.Database();
53
+ }
54
+ const token = this.generateToken();
55
+ const port = this.nextPort++;
56
+ this.databases.set(options.database, { db, token, port });
57
+ return {
58
+ port,
59
+ token,
60
+ database: options.database,
61
+ };
62
+ }
63
+ async disconnect(options) {
64
+ const dbInfo = this.databases.get(options.database);
65
+ if (!dbInfo) {
66
+ throw new Error(`Database '${options.database}' is not connected`);
67
+ }
68
+ // Save to IndexedDB before closing
69
+ const data = dbInfo.db.export();
70
+ await this.saveToIndexedDB(options.database, data);
71
+ dbInfo.db.close();
72
+ this.databases.delete(options.database);
73
+ }
74
+ async getServerInfo(options) {
75
+ const dbInfo = this.databases.get(options.database);
76
+ if (!dbInfo) {
77
+ throw new Error(`Database '${options.database}' is not connected`);
78
+ }
79
+ return {
80
+ port: dbInfo.port,
81
+ token: dbInfo.token,
82
+ };
83
+ }
84
+ async execute(options) {
85
+ const dbInfo = this.databases.get(options.database);
86
+ if (!dbInfo) {
87
+ throw new Error(`Database '${options.database}' is not connected`);
88
+ }
89
+ try {
90
+ const stmt = dbInfo.db.prepare(options.statement);
91
+ if (options.params) {
92
+ stmt.bind(options.params);
93
+ }
94
+ const rows = [];
95
+ while (stmt.step()) {
96
+ const row = stmt.getAsObject();
97
+ rows.push(row);
98
+ }
99
+ stmt.free();
100
+ // Get changes and last insert ID
101
+ const changes = dbInfo.db.getRowsModified();
102
+ const insertId = this.getLastInsertId(dbInfo.db);
103
+ return {
104
+ rows,
105
+ rowsAffected: changes,
106
+ insertId: insertId > 0 ? insertId : undefined,
107
+ };
108
+ }
109
+ catch (error) {
110
+ throw new Error(`SQL execution failed: ${error.message}`);
111
+ }
112
+ }
113
+ async beginTransaction(options) {
114
+ await this.execute({
115
+ database: options.database,
116
+ statement: 'BEGIN TRANSACTION',
117
+ });
118
+ }
119
+ async commitTransaction(options) {
120
+ await this.execute({
121
+ database: options.database,
122
+ statement: 'COMMIT',
123
+ });
124
+ }
125
+ async rollbackTransaction(options) {
126
+ await this.execute({
127
+ database: options.database,
128
+ statement: 'ROLLBACK',
129
+ });
130
+ }
131
+ /**
132
+ * Generate a random authentication token
133
+ */
134
+ generateToken() {
135
+ return Array.from(crypto.getRandomValues(new Uint8Array(32)))
136
+ .map((b) => b.toString(16).padStart(2, '0'))
137
+ .join('');
138
+ }
139
+ /**
140
+ * Get last insert row ID
141
+ */
142
+ getLastInsertId(db) {
143
+ try {
144
+ const result = db.exec('SELECT last_insert_rowid()');
145
+ if (result.length > 0 && result[0].values.length > 0) {
146
+ return result[0].values[0][0];
147
+ }
148
+ }
149
+ catch (_a) {
150
+ // Ignore error
151
+ }
152
+ return -1;
153
+ }
154
+ /**
155
+ * Save database to IndexedDB
156
+ */
157
+ async saveToIndexedDB(dbName, data) {
158
+ return new Promise((resolve, reject) => {
159
+ const request = indexedDB.open('CapacitorNativeSQL', 1);
160
+ request.onerror = () => reject(request.error);
161
+ request.onupgradeneeded = (event) => {
162
+ const db = event.target.result;
163
+ if (!db.objectStoreNames.contains('databases')) {
164
+ db.createObjectStore('databases');
165
+ }
166
+ };
167
+ request.onsuccess = () => {
168
+ const db = request.result;
169
+ const transaction = db.transaction(['databases'], 'readwrite');
170
+ const store = transaction.objectStore('databases');
171
+ const putRequest = store.put(data, dbName);
172
+ putRequest.onsuccess = () => {
173
+ db.close();
174
+ resolve();
175
+ };
176
+ putRequest.onerror = () => {
177
+ db.close();
178
+ reject(putRequest.error);
179
+ };
180
+ };
181
+ });
182
+ }
183
+ /**
184
+ * Load database from IndexedDB
185
+ */
186
+ async loadFromIndexedDB(dbName) {
187
+ return new Promise((resolve, reject) => {
188
+ const request = indexedDB.open('CapacitorNativeSQL', 1);
189
+ request.onerror = () => reject(request.error);
190
+ request.onupgradeneeded = (event) => {
191
+ const db = event.target.result;
192
+ if (!db.objectStoreNames.contains('databases')) {
193
+ db.createObjectStore('databases');
194
+ }
195
+ };
196
+ request.onsuccess = () => {
197
+ const db = request.result;
198
+ const transaction = db.transaction(['databases'], 'readonly');
199
+ const store = transaction.objectStore('databases');
200
+ const getRequest = store.get(dbName);
201
+ getRequest.onsuccess = () => {
202
+ db.close();
203
+ resolve(getRequest.result || null);
204
+ };
205
+ getRequest.onerror = () => {
206
+ db.close();
207
+ reject(getRequest.error);
208
+ };
209
+ };
210
+ });
211
+ }
212
+ async getPluginVersion() {
213
+ return { version: "web" };
214
+ }
215
+ }