@capacitor-community/sqlite 5.5.1 → 5.6.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 (34) hide show
  1. package/README.md +2 -0
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +1 -1
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +217 -218
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +1 -1
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +103 -102
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsFile.java +1 -1
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsMigrate.java +1 -1
  8. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSQLStatement.java +1 -1
  9. package/dist/esm/definitions.js.map +1 -1
  10. package/dist/esm/web-typeorm-utils/database.d.ts +21 -0
  11. package/dist/esm/web-typeorm-utils/database.js +91 -0
  12. package/dist/esm/web-typeorm-utils/database.js.map +1 -0
  13. package/dist/esm/web-typeorm-utils/utilsSQLite.d.ts +28 -0
  14. package/dist/esm/web-typeorm-utils/utilsSQLite.js +233 -0
  15. package/dist/esm/web-typeorm-utils/utilsSQLite.js.map +1 -0
  16. package/dist/esm/web.d.ts +4 -2
  17. package/dist/esm/web.js +210 -56
  18. package/dist/esm/web.js.map +1 -1
  19. package/dist/plugin.cjs.js +519 -49
  20. package/dist/plugin.cjs.js.map +1 -1
  21. package/dist/plugin.js +519 -49
  22. package/dist/plugin.js.map +1 -1
  23. package/electron/dist/plugin.js +14 -6
  24. package/electron/dist/plugin.js.map +1 -1
  25. package/ios/Plugin/CapacitorSQLite.swift +3 -4
  26. package/ios/Plugin/Database.swift +3 -1
  27. package/ios/Plugin/Utils/UtilsDelete.swift +1 -1
  28. package/ios/Plugin/Utils/UtilsSQLCipher.swift +1 -2
  29. package/ios/Plugin/Utils/UtilsSQLStatement.swift +35 -37
  30. package/package.json +6 -6
  31. package/src/definitions.ts +0 -1
  32. package/src/web-typeorm-utils/database.ts +111 -0
  33. package/src/web-typeorm-utils/utilsSQLite.ts +249 -0
  34. package/src/web.ts +212 -62
package/src/web.ts CHANGED
@@ -2,6 +2,7 @@ import { WebPlugin } from '@capacitor/core';
2
2
 
3
3
  import type {
4
4
  CapacitorSQLitePlugin,
5
+ capConnectionOptions,
5
6
  capAllConnectionsOptions,
6
7
  capChangeSecretOptions,
7
8
  capEchoOptions,
@@ -35,6 +36,7 @@ import type {
35
36
  capSQLiteExtensionPath,
36
37
  capSQLiteExtensionEnable,
37
38
  } from './definitions';
39
+ import { Database } from './web-typeorm-utils/database';
38
40
 
39
41
  export class CapacitorSQLiteWeb
40
42
  extends WebPlugin
@@ -42,6 +44,7 @@ export class CapacitorSQLiteWeb
42
44
  {
43
45
  private jeepSqliteElement: any = null;
44
46
  private isWebStoreOpen = false;
47
+ private databases: { [databasename: string]: Database } = {};
45
48
 
46
49
  async initWebStore(): Promise<void> {
47
50
  await customElements.whenDefined('jeep-sqlite');
@@ -131,39 +134,88 @@ export class CapacitorSQLiteWeb
131
134
  return echoResult;
132
135
  }
133
136
 
134
- async createConnection(options: capSQLiteOptions): Promise<void> {
135
- this.ensureJeepSqliteIsAvailable();
136
- this.ensureWebstoreIsOpen();
137
-
138
- try {
139
- await this.jeepSqliteElement.createConnection(options);
140
- return;
141
- } catch (err) {
142
- throw new Error(`${err}`);
137
+ async createConnection(options: capConnectionOptions): Promise<void> {
138
+ if (typeof window !== 'undefined' && window.document) {
139
+ this.ensureJeepSqliteIsAvailable();
140
+ this.ensureWebstoreIsOpen();
141
+
142
+ try {
143
+ await this.jeepSqliteElement.createConnection(options);
144
+ return;
145
+ } catch (err) {
146
+ throw new Error(`${err}`);
147
+ }
148
+ } else {
149
+ // Only for typeOrm to run migration:generate
150
+ const optionKeys = Object.keys(options);
151
+ let dbName: string | undefined;
152
+ if (!optionKeys.includes('database')) {
153
+ throw new Error('Must provide a database name');
154
+ } else {
155
+ dbName = options.database;
156
+ }
157
+ const connName: string = 'RW_' + dbName;
158
+ const databaseConnection: Database = new Database();
159
+ this.databases[connName] = databaseConnection;
143
160
  }
144
161
  }
145
162
 
146
163
  async open(options: capSQLiteOptions): Promise<void> {
147
- this.ensureJeepSqliteIsAvailable();
148
- this.ensureWebstoreIsOpen();
149
-
150
- try {
151
- await this.jeepSqliteElement.open(options);
152
- return;
153
- } catch (err) {
154
- throw new Error(`${err}`);
164
+ if (typeof window !== 'undefined' && window.document) {
165
+ this.ensureJeepSqliteIsAvailable();
166
+ this.ensureWebstoreIsOpen();
167
+
168
+ try {
169
+ await this.jeepSqliteElement.open(options);
170
+ return;
171
+ } catch (err) {
172
+ throw new Error(`${err}`);
173
+ }
174
+ } else {
175
+ // Only for typeOrm to run migration:generate
176
+
177
+ const dbName = options.database ? options.database : '';
178
+ if (dbName.length === 0) {
179
+ throw new Error('Must provide a database name');
180
+ }
181
+ const readonly = false;
182
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
183
+ const database = this.getDatabaseConnectionOrThrowError(connName);
184
+ try {
185
+ await database.open(dbName);
186
+ } catch (err) {
187
+ throw new Error(`### open ${err}`);
188
+ }
155
189
  }
156
190
  }
157
191
 
158
192
  async closeConnection(options: capSQLiteOptions): Promise<void> {
159
- this.ensureJeepSqliteIsAvailable();
160
- this.ensureWebstoreIsOpen();
161
-
162
- try {
163
- await this.jeepSqliteElement.closeConnection(options);
164
- return;
165
- } catch (err) {
166
- throw new Error(`${err}`);
193
+ if (typeof window !== 'undefined' && window.document) {
194
+ this.ensureJeepSqliteIsAvailable();
195
+ this.ensureWebstoreIsOpen();
196
+
197
+ try {
198
+ await this.jeepSqliteElement.closeConnection(options);
199
+ return;
200
+ } catch (err) {
201
+ throw new Error(`${err}`);
202
+ }
203
+ } else {
204
+ // Only for typeOrm to run migration:generate
205
+ const dbName = options.database ? options.database : '';
206
+ if (dbName.length === 0) {
207
+ throw new Error('Must provide a database name');
208
+ }
209
+ const readonly = false;
210
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
211
+ const database = this.getDatabaseConnectionOrThrowError(connName);
212
+ try {
213
+ await database.close();
214
+ // remove the connection from dictionary
215
+ delete this.databases[connName];
216
+ } catch (err) {
217
+ throw new Error(`### closeConnection ${err}`);
218
+ }
167
219
  }
168
220
  }
169
221
 
@@ -195,14 +247,31 @@ export class CapacitorSQLiteWeb
195
247
  }
196
248
 
197
249
  async close(options: capSQLiteOptions): Promise<void> {
198
- this.ensureJeepSqliteIsAvailable();
199
- this.ensureWebstoreIsOpen();
200
-
201
- try {
202
- await this.jeepSqliteElement.close(options);
203
- return;
204
- } catch (err) {
205
- throw new Error(`${err}`);
250
+ if (typeof window !== 'undefined' && window.document) {
251
+ this.ensureJeepSqliteIsAvailable();
252
+ this.ensureWebstoreIsOpen();
253
+
254
+ try {
255
+ await this.jeepSqliteElement.close(options);
256
+ return;
257
+ } catch (err) {
258
+ throw new Error(`${err}`);
259
+ }
260
+ } else {
261
+ // Only for typeOrm to run migration:generate
262
+
263
+ const dbName = options.database ? options.database : '';
264
+ if (dbName.length === 0) {
265
+ throw new Error('Must provide a database name');
266
+ }
267
+ const readonly = false;
268
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
269
+ const database = this.getDatabaseConnectionOrThrowError(connName);
270
+ try {
271
+ await database.close();
272
+ } catch (err) {
273
+ throw new Error(`### close ${err}`);
274
+ }
206
275
  }
207
276
  }
208
277
  async beginTransaction(options: capSQLiteOptions): Promise<capSQLiteChanges> {
@@ -274,15 +343,38 @@ export class CapacitorSQLiteWeb
274
343
  }
275
344
 
276
345
  async execute(options: capSQLiteExecuteOptions): Promise<capSQLiteChanges> {
277
- this.ensureJeepSqliteIsAvailable();
278
- this.ensureWebstoreIsOpen();
279
-
280
- try {
281
- const executeResult: capSQLiteChanges =
282
- await this.jeepSqliteElement.execute(options);
283
- return executeResult;
284
- } catch (err) {
285
- throw new Error(`${err}`);
346
+ if (typeof window !== 'undefined' && window.document) {
347
+ this.ensureJeepSqliteIsAvailable();
348
+ this.ensureWebstoreIsOpen();
349
+
350
+ try {
351
+ const executeResult: capSQLiteChanges =
352
+ await this.jeepSqliteElement.execute(options);
353
+ return executeResult;
354
+ } catch (err) {
355
+ throw new Error(`${err}`);
356
+ }
357
+ } else {
358
+ // Only for typeOrm to run migration:generate
359
+
360
+ const dbName = options.database ? options.database : '';
361
+ if (dbName.length === 0) {
362
+ throw new Error('Must provide a database name');
363
+ }
364
+ const statements = options.statements ? options.statements : '';
365
+ if (statements.length === 0) {
366
+ return Promise.reject('Must provide raw SQL statements');
367
+ }
368
+ const readonly = false;
369
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
370
+ const database = this.getDatabaseConnectionOrThrowError(connName);
371
+ try {
372
+ const ret = await database.executeSQL(statements);
373
+ const executeResult: capSQLiteChanges = { changes: { changes: ret } };
374
+ return executeResult;
375
+ } catch (err) {
376
+ throw new Error(`${err}`);
377
+ }
286
378
  }
287
379
  }
288
380
 
@@ -300,29 +392,79 @@ export class CapacitorSQLiteWeb
300
392
  }
301
393
 
302
394
  async run(options: capSQLiteRunOptions): Promise<capSQLiteChanges> {
303
- this.ensureJeepSqliteIsAvailable();
304
- this.ensureWebstoreIsOpen();
305
-
306
- try {
307
- const runResult: capSQLiteChanges = await this.jeepSqliteElement.run(
308
- options,
309
- );
310
- return runResult;
311
- } catch (err) {
312
- throw new Error(`${err}`);
395
+ if (typeof window !== 'undefined' && window.document) {
396
+ this.ensureJeepSqliteIsAvailable();
397
+ this.ensureWebstoreIsOpen();
398
+
399
+ try {
400
+ const runResult: capSQLiteChanges = await this.jeepSqliteElement.run(
401
+ options,
402
+ );
403
+ return runResult;
404
+ } catch (err) {
405
+ throw new Error(`${err}`);
406
+ }
407
+ } else {
408
+ // Only for typeOrm to run migration:generate
409
+
410
+ const dbName = options.database ? options.database : '';
411
+ if (dbName.length === 0) {
412
+ throw new Error('Must provide a database name');
413
+ }
414
+ const statement = options.statement ? options.statement : '';
415
+ if (statement.length === 0) {
416
+ return Promise.reject('Must provide raw SQL statement');
417
+ }
418
+ const values = options.values ? options.values : [];
419
+ const readonly = false;
420
+ const returnMode = options.returnMode ? options.returnMode : 'no';
421
+
422
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
423
+ const database = this.getDatabaseConnectionOrThrowError(connName);
424
+ try {
425
+ const runResult = await database.run(statement, values, returnMode);
426
+ return runResult;
427
+ } catch (err) {
428
+ throw new Error(`${err}`);
429
+ }
313
430
  }
314
431
  }
315
432
  async query(options: capSQLiteQueryOptions): Promise<capSQLiteValues> {
316
- this.ensureJeepSqliteIsAvailable();
317
- this.ensureWebstoreIsOpen();
318
-
319
- try {
320
- const queryResult: capSQLiteValues = await this.jeepSqliteElement.query(
321
- options,
322
- );
323
- return queryResult;
324
- } catch (err) {
325
- throw new Error(`${err}`);
433
+ if (typeof window !== 'undefined' && window.document) {
434
+ this.ensureJeepSqliteIsAvailable();
435
+ this.ensureWebstoreIsOpen();
436
+
437
+ try {
438
+ const queryResult: capSQLiteValues = await this.jeepSqliteElement.query(
439
+ options,
440
+ );
441
+ return queryResult;
442
+ } catch (err) {
443
+ throw new Error(`${err}`);
444
+ }
445
+ } else {
446
+ // Only for typeOrm to run migration:generate
447
+ const dbName = options.database ? options.database : '';
448
+ if (dbName.length === 0) {
449
+ throw new Error('Must provide a database name');
450
+ }
451
+ const statement = options.statement ? options.statement : '';
452
+ if (statement.length === 0) {
453
+ return Promise.reject('Must provide raw SQL statement');
454
+ }
455
+ const values = options.values ? options.values : [];
456
+ const readonly = false;
457
+ const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
458
+ const database = this.getDatabaseConnectionOrThrowError(connName);
459
+ try {
460
+ const queryResult: capSQLiteValues = await database.selectSQL(
461
+ statement,
462
+ values,
463
+ );
464
+ return queryResult;
465
+ } catch (err) {
466
+ throw new Error(`${err}`);
467
+ }
326
468
  }
327
469
  }
328
470
  async isDBExists(options: capSQLiteOptions): Promise<capSQLiteResult> {
@@ -552,6 +694,14 @@ export class CapacitorSQLiteWeb
552
694
  );
553
695
  }
554
696
  }
697
+ private getDatabaseConnectionOrThrowError(dbName: string): Database {
698
+ const databaseNames = Object.keys(this.databases);
699
+ if (!databaseNames.includes(dbName)) {
700
+ throw new Error(`No connection available for database "${dbName}"`);
701
+ }
702
+
703
+ return this.databases[dbName];
704
+ }
555
705
 
556
706
  ////////////////////////////////////
557
707
  ////// UNIMPLEMENTED METHODS