@capacitor-community/sqlite 4.2.0 → 4.2.2-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/README.md CHANGED
@@ -27,8 +27,21 @@
27
27
  | Quéau Jean Pierre | [jepiqueau](https://github.com/jepiqueau) | |
28
28
 
29
29
  To install:
30
+
31
+ ```
32
+ npm install --save @capacitor-community/sqlite
33
+ npx cap sync
34
+ ```
35
+
36
+ ```
37
+ yarn install --save @capacitor-community/sqlite
38
+ npx cap sync
39
+ ```
40
+
30
41
  ```
31
- npm install @capacitor-community/sqlite
42
+ pnpm install --save @capacitor-community/sqlite
43
+ pnpm install --save @jeep-sqlite
44
+ pnpm install --save sql.js
32
45
  npx cap sync
33
46
  ```
34
47
 
@@ -3,12 +3,13 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var require$$0 = require('sqlite3');
6
- var require$$0$1 = require('path');
7
- var require$$1 = require('fs');
8
- var require$$2 = require('node-fetch');
9
- var require$$3 = require('os');
10
- var require$$4 = require('jszip');
11
- var require$$5 = require('electron');
6
+ var require$$0$1 = require('node:fs/promises');
7
+ var require$$1 = require('path');
8
+ var require$$2 = require('fs');
9
+ var require$$3 = require('node-fetch');
10
+ var require$$4 = require('os');
11
+ var require$$5 = require('jszip');
12
+ var require$$6 = require('electron');
12
13
 
13
14
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
15
 
@@ -19,6 +20,7 @@ var require$$2__default = /*#__PURE__*/_interopDefaultLegacy(require$$2);
19
20
  var require$$3__default = /*#__PURE__*/_interopDefaultLegacy(require$$3);
20
21
  var require$$4__default = /*#__PURE__*/_interopDefaultLegacy(require$$4);
21
22
  var require$$5__default = /*#__PURE__*/_interopDefaultLegacy(require$$5);
23
+ var require$$6__default = /*#__PURE__*/_interopDefaultLegacy(require$$6);
22
24
 
23
25
  var src = {};
24
26
 
@@ -216,14 +218,19 @@ class UtilsSQLite {
216
218
  * BeginTransaction
217
219
  * @param db
218
220
  * @param isOpen
221
+ * @param mode
219
222
  */
220
- async beginTransaction(db, isOpen) {
223
+ async beginTransaction(db, isOpen, mode) {
224
+ // eslint-disable-next-line no-async-promise-executor
221
225
  return new Promise((resolve, reject) => {
222
226
  const msg = 'BeginTransaction: ';
223
227
  if (!isOpen) {
224
228
  return Promise.reject(`${msg}database not opened`);
225
229
  }
226
- const sql = 'BEGIN TRANSACTION;';
230
+ let sql = 'BEGIN TRANSACTION;';
231
+ if (mode.slice(0, 3) === "wal") {
232
+ sql = "BEGIN CONCURRENT";
233
+ }
227
234
  db.run(sql, (err) => {
228
235
  if (err) {
229
236
  reject(`${msg}${err.message}`);
@@ -821,6 +828,23 @@ class UtilsSQLite {
821
828
  return Promise.reject(`isSqlDeleted: ${err}`);
822
829
  }
823
830
  }
831
+ async getJournalMode(mDB) {
832
+ let resQuery = [];
833
+ let retMode = "delete";
834
+ const query = `PRAGMA journal_mode;`;
835
+ try {
836
+ resQuery = await this.queryAll(mDB, query, []);
837
+ if (resQuery.length === 1) {
838
+ for (const query of resQuery) {
839
+ retMode = query.journal_mode;
840
+ }
841
+ }
842
+ return retMode;
843
+ }
844
+ catch (err) {
845
+ return Promise.reject('GetJournalMode: ' + `${err}`);
846
+ }
847
+ }
824
848
  /**
825
849
  * GetTableColumnNamesTypes
826
850
  * @param mDB
@@ -925,7 +949,8 @@ class UtilsJson {
925
949
  let changes = 0;
926
950
  try {
927
951
  // start a transaction
928
- await this.sqliteUtil.beginTransaction(mDB, true);
952
+ const mode = await this.sqliteUtil.getJournalMode(mDB);
953
+ await this.sqliteUtil.beginTransaction(mDB, true, mode);
929
954
  }
930
955
  catch (err) {
931
956
  return Promise.reject(`CreateDatabaseSchema: ${err}`);
@@ -2561,7 +2586,8 @@ class ImportFromJson {
2561
2586
  try {
2562
2587
  initChanges = await this.sqliteUtil.dbChanges(mDB);
2563
2588
  // start a transaction
2564
- await this.sqliteUtil.beginTransaction(mDB, true);
2589
+ const mode = await this.sqliteUtil.getJournalMode(mDB);
2590
+ await this.sqliteUtil.beginTransaction(mDB, true, mode);
2565
2591
  }
2566
2592
  catch (err) {
2567
2593
  return Promise.reject(`createTablesData: ${err}`);
@@ -2621,7 +2647,8 @@ class ImportFromJson {
2621
2647
  try {
2622
2648
  initChanges = await this.sqliteUtil.dbChanges(mDB);
2623
2649
  // start a transaction
2624
- await this.sqliteUtil.beginTransaction(mDB, true);
2650
+ const mode = await this.sqliteUtil.getJournalMode(mDB);
2651
+ await this.sqliteUtil.beginTransaction(mDB, true, mode);
2625
2652
  }
2626
2653
  catch (err) {
2627
2654
  return Promise.reject(`createViews: ${err}`);
@@ -2673,6 +2700,7 @@ var utilsFile = {};
2673
2700
 
2674
2701
  Object.defineProperty(utilsFile, "__esModule", { value: true });
2675
2702
  utilsFile.UtilsFile = void 0;
2703
+ const promises_1 = require$$0__default$1["default"];
2676
2704
  class UtilsFile {
2677
2705
  constructor() {
2678
2706
  this.pathDB = 'Databases';
@@ -2685,12 +2713,12 @@ class UtilsFile {
2685
2713
  this.AppName = '';
2686
2714
  this.HomeDir = '';
2687
2715
  this.sep = '/';
2688
- this.Path = require$$0__default$1["default"];
2689
- this.NodeFs = require$$1__default["default"];
2690
- this.NodeFetch = require$$2__default["default"];
2691
- this.Os = require$$3__default["default"];
2692
- this.JSZip = require$$4__default["default"];
2693
- this.Electron = require$$5__default["default"];
2716
+ this.Path = require$$1__default["default"];
2717
+ this.NodeFs = require$$2__default["default"];
2718
+ this.NodeFetch = require$$3__default["default"];
2719
+ this.Os = require$$4__default["default"];
2720
+ this.JSZip = require$$5__default["default"];
2721
+ this.Electron = require$$6__default["default"];
2694
2722
  this.HomeDir = this.Os.homedir();
2695
2723
  const dir = __dirname;
2696
2724
  const idx = dir.indexOf('\\');
@@ -2996,6 +3024,7 @@ class UtilsFile {
2996
3024
  const isPath = this.isPathExists(filePath);
2997
3025
  if (isPath) {
2998
3026
  try {
3027
+ await this.waitForFilePathLock(filePath);
2999
3028
  this.NodeFs.unlinkSync(filePath);
3000
3029
  return Promise.resolve();
3001
3030
  }
@@ -3011,6 +3040,52 @@ class UtilsFile {
3011
3040
  return Promise.reject('DeleteFilePath: delete filePath' + 'failed');
3012
3041
  }
3013
3042
  }
3043
+ async waitForFilePathLock(filePath, timeoutMS = 4000) {
3044
+ let timeIsOver = false;
3045
+ setTimeout(() => {
3046
+ timeIsOver = true;
3047
+ }, timeoutMS);
3048
+ return new Promise((resolve, reject) => {
3049
+ const check = async () => {
3050
+ if (timeIsOver) {
3051
+ reject(new Error(`WaitForFilePathLock: The resource is still locked / busy after ${timeoutMS} milliseconds.`));
3052
+ return;
3053
+ }
3054
+ // check if path exists
3055
+ const isPath = this.isPathExists(filePath);
3056
+ // The file path does not exist. A non existant path cannot be locked.
3057
+ if (!isPath) {
3058
+ resolve();
3059
+ return;
3060
+ }
3061
+ try {
3062
+ const stream = await promises_1.open(filePath, 'r+');
3063
+ // We need to close the stream afterwards, because otherwise, we're locking the file
3064
+ await stream.close();
3065
+ resolve();
3066
+ }
3067
+ catch (err) {
3068
+ if (err.code === 'EBUSY') {
3069
+ // The resource is busy. Retry in 100ms
3070
+ setTimeout(() => {
3071
+ check();
3072
+ }, 100);
3073
+ return;
3074
+ }
3075
+ else if (err.code === 'ENOENT') {
3076
+ // The file does not exist (anymore). So it cannot be locked.
3077
+ resolve();
3078
+ return;
3079
+ }
3080
+ else {
3081
+ // Something else went wrong.
3082
+ reject(new Error(`WaitForFilePathLock: Error while checking the file: ${err}`));
3083
+ }
3084
+ }
3085
+ };
3086
+ check();
3087
+ });
3088
+ }
3014
3089
  /**
3015
3090
  * RenameFileName
3016
3091
  * @param fileName
@@ -3223,7 +3298,8 @@ class UtilsUpgrade {
3223
3298
  */
3224
3299
  async executeStatementsProcess(mDB, statements) {
3225
3300
  try {
3226
- await this.sqliteUtil.beginTransaction(mDB, true);
3301
+ const mode = await this.sqliteUtil.getJournalMode(mDB);
3302
+ await this.sqliteUtil.beginTransaction(mDB, true, mode);
3227
3303
  for (const statement of statements) {
3228
3304
  await this.sqliteUtil.execute(mDB, statement, false);
3229
3305
  }
@@ -3350,11 +3426,15 @@ class Database {
3350
3426
  */
3351
3427
  async close() {
3352
3428
  this.ensureDatabaseIsOpen();
3353
- this.database.close((err) => {
3354
- if (err) {
3355
- throw new Error('Close failed: ${this.dbName} ${err}');
3356
- }
3357
- this._isDbOpen = false;
3429
+ return new Promise((resolve, reject) => {
3430
+ this.database.close((err) => {
3431
+ if (err) {
3432
+ reject(new Error(`Close failed: ${this.dbName} ${err}`));
3433
+ return;
3434
+ }
3435
+ this._isDbOpen = false;
3436
+ resolve();
3437
+ });
3358
3438
  });
3359
3439
  }
3360
3440
  /**
@@ -3530,7 +3610,8 @@ class Database {
3530
3610
  this.ensureDatabaseIsOpen();
3531
3611
  try {
3532
3612
  if (transaction) {
3533
- await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
3613
+ const mode = await this.sqliteUtil.getJournalMode(this.database);
3614
+ await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen, mode);
3534
3615
  }
3535
3616
  const changes = await this.sqliteUtil.execute(this.database, sql, false);
3536
3617
  if (changes < 0) {
@@ -3586,7 +3667,8 @@ class Database {
3586
3667
  initChanges = await this.sqliteUtil.dbChanges(this.database);
3587
3668
  // start a transaction
3588
3669
  if (transaction) {
3589
- await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
3670
+ const mode = await this.sqliteUtil.getJournalMode(this.database);
3671
+ await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen, mode);
3590
3672
  }
3591
3673
  }
3592
3674
  catch (err) {
@@ -3629,7 +3711,8 @@ class Database {
3629
3711
  initChanges = await this.sqliteUtil.dbChanges(this.database);
3630
3712
  // start a transaction
3631
3713
  if (transaction) {
3632
- await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen);
3714
+ const mode = await this.sqliteUtil.getJournalMode(this.database);
3715
+ await this.sqliteUtil.beginTransaction(this.database, this._isDbOpen, mode);
3633
3716
  }
3634
3717
  }
3635
3718
  catch (err) {