@capacitor-community/sqlite 5.0.5 → 5.0.7-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 (30) hide show
  1. package/README.md +3 -0
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +9 -0
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +9 -0
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/UtilsEncryption.java +111 -0
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/UtilsSecret.java +3 -3
  6. package/dist/esm/definitions.d.ts +66 -0
  7. package/dist/esm/definitions.js +37 -0
  8. package/dist/esm/definitions.js.map +1 -1
  9. package/dist/esm/web.d.ts +3 -1
  10. package/dist/esm/web.js +8 -0
  11. package/dist/esm/web.js.map +1 -1
  12. package/dist/plugin.cjs.js +45 -0
  13. package/dist/plugin.cjs.js.map +1 -1
  14. package/dist/plugin.js +45 -0
  15. package/dist/plugin.js.map +1 -1
  16. package/electron/dist/plugin.js +138 -52
  17. package/electron/dist/plugin.js.map +1 -1
  18. package/electron/rollup.config.js +2 -0
  19. package/ios/Plugin/CapacitorSQLite.swift +117 -88
  20. package/ios/Plugin/Database.swift +17 -5
  21. package/ios/Plugin/ImportExportJson/ImportData.swift +434 -0
  22. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +43 -57
  23. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +7 -0
  24. package/ios/Plugin/Utils/UtilsDelete.swift +506 -0
  25. package/ios/Plugin/Utils/UtilsJson.swift +123 -1
  26. package/ios/Plugin/Utils/UtilsSQLCipher.swift +173 -477
  27. package/ios/Plugin/Utils/UtilsSQLStatement.swift +450 -0
  28. package/package.json +2 -2
  29. package/src/definitions.ts +104 -0
  30. package/src/web.ts +10 -0
@@ -11,6 +11,8 @@ var require$$5 = require('jszip');
11
11
  var require$$6 = require('electron');
12
12
  var require$$1$1 = require('better-sqlite3-multiple-ciphers');
13
13
  var require$$3$1 = require('electron-json-storage');
14
+ var require$$1$2 = require('crypto');
15
+ var require$$2$1 = require('crypto-js');
14
16
 
15
17
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
18
 
@@ -23,6 +25,8 @@ var require$$5__default = /*#__PURE__*/_interopDefaultLegacy(require$$5);
23
25
  var require$$6__default = /*#__PURE__*/_interopDefaultLegacy(require$$6);
24
26
  var require$$1__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$1$1);
25
27
  var require$$3__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$3$1);
28
+ var require$$1__default$2 = /*#__PURE__*/_interopDefaultLegacy(require$$1$2);
29
+ var require$$2__default$1 = /*#__PURE__*/_interopDefaultLegacy(require$$2$1);
26
30
 
27
31
  var src = {};
28
32
 
@@ -110,7 +114,6 @@ class UtilsFile {
110
114
  default:
111
115
  console.log('other operating system');
112
116
  }
113
- console.log(`&&& Databases path: ${this.pathDB}`);
114
117
  }
115
118
  /**
116
119
  * Get isEncryption from config
@@ -670,13 +673,13 @@ class UtilsSQLite {
670
673
  let mDB;
671
674
  if (!readonly) {
672
675
  mDB = new this.BCSQLite3(pathDB, {
673
- verbose: console.log,
676
+ // verbose: console.log,
674
677
  fileMustExist: false,
675
678
  });
676
679
  }
677
680
  else {
678
681
  mDB = new this.BCSQLite3(pathDB, {
679
- verbose: console.log,
682
+ // verbose: console.log,
680
683
  readonly: true,
681
684
  fileMustExist: true,
682
685
  });
@@ -3545,55 +3548,20 @@ class ImportFromJson {
3545
3548
  }
3546
3549
  importFromJson.ImportFromJson = ImportFromJson;
3547
3550
 
3548
- var utilsEncryption = {};
3549
-
3550
- Object.defineProperty(utilsEncryption, "__esModule", { value: true });
3551
- utilsEncryption.UtilsEncryption = void 0;
3552
- const utilsFile_1$3 = utilsFile;
3553
- const utilsSQLite_1$4 = utilsSQLite;
3554
- class UtilsEncryption {
3555
- constructor() {
3556
- this.fileUtil = new utilsFile_1$3.UtilsFile();
3557
- this.sqliteUtil = new utilsSQLite_1$4.UtilsSQLite();
3558
- }
3559
- /**
3560
- * EncryptDatabase
3561
- * @param pathDB
3562
- * @param password
3563
- */
3564
- async encryptDatabase(pathDB, password) {
3565
- const msg = 'EncryptDatabase: ';
3566
- const retB = this.fileUtil.isPathExists(pathDB);
3567
- if (retB) {
3568
- try {
3569
- const mDB = await this.sqliteUtil.openOrCreateDatabase(pathDB, '', false);
3570
- this.sqliteUtil.pragmaReKey(mDB, '', password);
3571
- this.sqliteUtil.closeDB(mDB);
3572
- return Promise.resolve();
3573
- }
3574
- catch (err) {
3575
- return Promise.reject(new Error(`${msg} ${err.message} `));
3576
- }
3577
- }
3578
- else {
3579
- return Promise.reject(new Error(`${msg}file path ${pathDB} ` + 'does not exist'));
3580
- }
3581
- }
3582
- }
3583
- utilsEncryption.UtilsEncryption = UtilsEncryption;
3551
+ var utilsJsonEncryption = {};
3584
3552
 
3585
3553
  var utilsSecret = {};
3586
3554
 
3587
3555
  Object.defineProperty(utilsSecret, "__esModule", { value: true });
3588
3556
  utilsSecret.UtilsSecret = void 0;
3589
3557
  const GlobalSQLite_1$2 = GlobalSQLite$1;
3590
- const utilsFile_1$2 = utilsFile;
3591
- const utilsSQLite_1$3 = utilsSQLite;
3558
+ const utilsFile_1$3 = utilsFile;
3559
+ const utilsSQLite_1$4 = utilsSQLite;
3592
3560
  class UtilsSecret {
3593
3561
  constructor() {
3594
3562
  this.globalUtil = new GlobalSQLite_1$2.GlobalSQLite();
3595
- this.sqliteUtil = new utilsSQLite_1$3.UtilsSQLite();
3596
- this.fileUtil = new utilsFile_1$2.UtilsFile();
3563
+ this.sqliteUtil = new utilsSQLite_1$4.UtilsSQLite();
3564
+ this.fileUtil = new utilsFile_1$3.UtilsFile();
3597
3565
  this.storage = require$$3__default$1["default"];
3598
3566
  }
3599
3567
  isSecretStored() {
@@ -3714,6 +3682,103 @@ class UtilsSecret {
3714
3682
  }
3715
3683
  utilsSecret.UtilsSecret = UtilsSecret;
3716
3684
 
3685
+ Object.defineProperty(utilsJsonEncryption, "__esModule", { value: true });
3686
+ utilsJsonEncryption.UtilsJsonEncryption = void 0;
3687
+ const utilsSecret_1$2 = utilsSecret;
3688
+ class UtilsJsonEncryption {
3689
+ constructor() {
3690
+ this.fileSecret = new utilsSecret_1$2.UtilsSecret();
3691
+ this.SALT = 'jeep_capacitor_sqlite';
3692
+ this.Crypto = require$$1__default$2["default"];
3693
+ this.CryptoJS = require$$2__default$1["default"];
3694
+ }
3695
+ /**
3696
+ * deriveKeyFromPassphrase
3697
+ * Function to derive a symmetric key from passphrase and salt using PBKDF2
3698
+ * @param passphrase
3699
+ * @param salt
3700
+ * @returns
3701
+ */
3702
+ deriveKeyFromPassphrase(passphrase, salt) {
3703
+ const iterations = 10000; // Recommended number of iterations for PBKDF2
3704
+ const keyLength = 32;
3705
+ const key = this.Crypto.pbkdf2Sync(passphrase, salt, iterations, keyLength, 'sha256');
3706
+ const keyHex = Buffer.from(key).toString('hex');
3707
+ return keyHex;
3708
+ }
3709
+ /**
3710
+ * encryptJSONObject
3711
+ * Function to encrypt JSON object with AES and return as Base64
3712
+ * @param jsonObj
3713
+ * @returns
3714
+ */
3715
+ encryptJSONObject(jsonObj) {
3716
+ const jsonString = JSON.stringify(jsonObj);
3717
+ // get the passphrase
3718
+ const passphrase = this.fileSecret.getPassphrase();
3719
+ // derived a combined key from passphrase and salt
3720
+ const key = this.deriveKeyFromPassphrase(passphrase, this.SALT);
3721
+ const encrypted = this.CryptoJS.AES.encrypt(jsonString, key).toString();
3722
+ const encryptedBase64 = Buffer.from(encrypted).toString('base64');
3723
+ return encryptedBase64;
3724
+ }
3725
+ /**
3726
+ * decryptJSONObject
3727
+ * Function to decrypt AES encrypted JSON object from Base64
3728
+ * @param encryptedBase64
3729
+ * @returns
3730
+ */
3731
+ decryptJSONObject(encryptedBase64) {
3732
+ const encryptedData = Buffer.from(encryptedBase64, 'base64').toString();
3733
+ // get the passphrase
3734
+ const passphrase = this.fileSecret.getPassphrase();
3735
+ // derived a combined key from passphrase and salt
3736
+ const key = this.deriveKeyFromPassphrase(passphrase, this.SALT);
3737
+ const bytes = this.CryptoJS.AES.decrypt(encryptedData, key);
3738
+ const decryptedString = bytes.toString(this.CryptoJS.enc.Utf8);
3739
+ const decryptedObj = JSON.parse(decryptedString);
3740
+ return decryptedObj;
3741
+ }
3742
+ }
3743
+ utilsJsonEncryption.UtilsJsonEncryption = UtilsJsonEncryption;
3744
+
3745
+ var utilsEncryption = {};
3746
+
3747
+ Object.defineProperty(utilsEncryption, "__esModule", { value: true });
3748
+ utilsEncryption.UtilsEncryption = void 0;
3749
+ const utilsFile_1$2 = utilsFile;
3750
+ const utilsSQLite_1$3 = utilsSQLite;
3751
+ class UtilsEncryption {
3752
+ constructor() {
3753
+ this.fileUtil = new utilsFile_1$2.UtilsFile();
3754
+ this.sqliteUtil = new utilsSQLite_1$3.UtilsSQLite();
3755
+ }
3756
+ /**
3757
+ * EncryptDatabase
3758
+ * @param pathDB
3759
+ * @param password
3760
+ */
3761
+ async encryptDatabase(pathDB, password) {
3762
+ const msg = 'EncryptDatabase: ';
3763
+ const retB = this.fileUtil.isPathExists(pathDB);
3764
+ if (retB) {
3765
+ try {
3766
+ const mDB = await this.sqliteUtil.openOrCreateDatabase(pathDB, '', false);
3767
+ this.sqliteUtil.pragmaReKey(mDB, '', password);
3768
+ this.sqliteUtil.closeDB(mDB);
3769
+ return Promise.resolve();
3770
+ }
3771
+ catch (err) {
3772
+ return Promise.reject(new Error(`${msg} ${err.message} `));
3773
+ }
3774
+ }
3775
+ else {
3776
+ return Promise.reject(new Error(`${msg}file path ${pathDB} ` + 'does not exist'));
3777
+ }
3778
+ }
3779
+ }
3780
+ utilsEncryption.UtilsEncryption = UtilsEncryption;
3781
+
3717
3782
  var utilsUpgrade = {};
3718
3783
 
3719
3784
  Object.defineProperty(utilsUpgrade, "__esModule", { value: true });
@@ -3785,13 +3850,15 @@ const GlobalSQLite_1$1 = GlobalSQLite$1;
3785
3850
  const exportToJson_1 = exportToJson;
3786
3851
  const importFromJson_1 = importFromJson;
3787
3852
  const utilsJson_1$1 = utilsJson;
3853
+ const utilsJsonEncryption_1$1 = utilsJsonEncryption;
3788
3854
  const utilsEncryption_1 = utilsEncryption;
3789
3855
  const utilsFile_1$1 = utilsFile;
3790
3856
  const utilsSQLite_1$1 = utilsSQLite;
3791
3857
  const utilsSecret_1$1 = utilsSecret;
3792
3858
  const utilsUpgrade_1 = utilsUpgrade;
3793
3859
  class Database {
3794
- constructor(dbName, encrypted, mode, version, readonly, upgDict, globalUtil) {
3860
+ constructor(dbName, encrypted, mode, version, isEncryption, readonly, upgDict, globalUtil) {
3861
+ this.jsonEncryptUtil = new utilsJsonEncryption_1$1.UtilsJsonEncryption();
3795
3862
  this.fileUtil = new utilsFile_1$1.UtilsFile();
3796
3863
  this.sqliteUtil = new utilsSQLite_1$1.UtilsSQLite();
3797
3864
  this.jsonUtil = new utilsJson_1$1.UtilsJson();
@@ -3805,6 +3872,7 @@ class Database {
3805
3872
  this.dbName = dbName;
3806
3873
  this._encrypted = encrypted;
3807
3874
  this._mode = mode;
3875
+ this._isEncryption = isEncryption;
3808
3876
  this.version = version;
3809
3877
  this.readonly = readonly;
3810
3878
  this.upgradeVersionDict = upgDict;
@@ -3813,6 +3881,7 @@ class Database {
3813
3881
  this.globalUtil = globalUtil ? globalUtil : new GlobalSQLite_1$1.GlobalSQLite();
3814
3882
  if (this.pathDB.length === 0)
3815
3883
  throw new Error('Could not generate a path to ' + dbName);
3884
+ console.log(`&&& Databases path: ${this.pathDB}`);
3816
3885
  }
3817
3886
  /**
3818
3887
  * IsDBOpen
@@ -4271,13 +4340,21 @@ class Database {
4271
4340
  if (isTable) {
4272
4341
  this.exportToJsonUtil.setLastExportDate(this.database, new Date().toISOString());
4273
4342
  }
4274
- const jsonResult = this.exportToJsonUtil.createExportObject(this.database, inJson);
4343
+ let jsonResult = this.exportToJsonUtil.createExportObject(this.database, inJson);
4275
4344
  const keys = Object.keys(jsonResult);
4276
4345
  if (keys.length === 0) {
4277
4346
  const msg = `ExportJson: return Object is empty ` + `No data to synchronize`;
4278
4347
  throw new Error(msg);
4279
4348
  }
4280
- const isValid = this.jsonUtil.isJsonSQLite(jsonResult);
4349
+ let isValid = this.jsonUtil.isJsonSQLite(jsonResult);
4350
+ if (this._encrypted && this._isEncryption) {
4351
+ jsonResult.overwrite = true;
4352
+ jsonResult.encrypted = true;
4353
+ const base64Str = this.jsonEncryptUtil.encryptJSONObject(jsonResult);
4354
+ jsonResult = {};
4355
+ jsonResult.expData = base64Str;
4356
+ isValid = true;
4357
+ }
4281
4358
  if (isValid) {
4282
4359
  return jsonResult;
4283
4360
  }
@@ -4305,6 +4382,7 @@ exports.CapacitorSQLite = src.CapacitorSQLite = void 0;
4305
4382
  const GlobalSQLite_1 = GlobalSQLite$1;
4306
4383
  const Database_1 = Database$1;
4307
4384
  const utilsJson_1 = utilsJson;
4385
+ const utilsJsonEncryption_1 = utilsJsonEncryption;
4308
4386
  const utilsFile_1 = utilsFile;
4309
4387
  const utilsSQLite_1 = utilsSQLite;
4310
4388
  const utilsSecret_1 = utilsSecret;
@@ -4314,6 +4392,7 @@ class CapacitorSQLite {
4314
4392
  this.databases = {};
4315
4393
  this.fileUtil = new utilsFile_1.UtilsFile();
4316
4394
  this.jsonUtil = new utilsJson_1.UtilsJson();
4395
+ this.jsonEncryptUtil = new utilsJsonEncryption_1.UtilsJsonEncryption();
4317
4396
  this.sqliteUtil = new utilsSQLite_1.UtilsSQLite();
4318
4397
  this.secretUtil = new utilsSecret_1.UtilsSecret();
4319
4398
  this.globalUtil = new GlobalSQLite_1.GlobalSQLite();
@@ -4347,7 +4426,7 @@ class CapacitorSQLite {
4347
4426
  upgrades = this.versionUpgrades[dbName];
4348
4427
  }
4349
4428
  const connName = readonly ? 'RO_' + dbName : 'RW_' + dbName;
4350
- const databaseConnection = new Database_1.Database(dbName + 'SQLite.db', encrypted, inMode, version, readonly, upgrades, this.globalUtil);
4429
+ const databaseConnection = new Database_1.Database(dbName + 'SQLite.db', encrypted, inMode, version, this.isEncryption, readonly, upgrades, this.globalUtil);
4351
4430
  this.databases[connName] = databaseConnection;
4352
4431
  return;
4353
4432
  }
@@ -4644,7 +4723,14 @@ class CapacitorSQLite {
4644
4723
  }
4645
4724
  async importFromJson(options) {
4646
4725
  const jsonString = this.getOptionValue(options, 'jsonstring');
4647
- const jsonObj = JSON.parse(jsonString);
4726
+ let jsonObj = JSON.parse(jsonString);
4727
+ let inMode = 'no-encryption';
4728
+ const key = 'expData';
4729
+ if (key in jsonObj) {
4730
+ // Decrypt the data
4731
+ inMode = 'secret';
4732
+ jsonObj = this.jsonEncryptUtil.decryptJSONObject(jsonObj.expData);
4733
+ }
4648
4734
  const isValid = this.jsonUtil.isJsonSQLite(jsonObj);
4649
4735
  if (!isValid) {
4650
4736
  throw new Error('Must provide a valid JsonSQLite Object');
@@ -4654,12 +4740,12 @@ class CapacitorSQLite {
4654
4740
  const targetDbVersion = vJsonObj.version ?? 1;
4655
4741
  const overwrite = vJsonObj.overwrite ?? false;
4656
4742
  const encrypted = vJsonObj.encrypted ?? false;
4657
- const mode = vJsonObj.mode ?? 'no-encryption';
4743
+ const mode = vJsonObj.mode ?? 'full';
4658
4744
  if (!this.isEncryption && encrypted) {
4659
4745
  throw new Error('Must set electronIsEncryption = true in capacitor.config.ts');
4660
4746
  }
4661
4747
  // Create the database
4662
- const database = new Database_1.Database(dbName, encrypted, mode, targetDbVersion, false, {}, this.globalUtil);
4748
+ const database = new Database_1.Database(dbName, encrypted, inMode, targetDbVersion, this.isEncryption, false, {}, this.globalUtil);
4663
4749
  try {
4664
4750
  if (overwrite && mode === 'full') {
4665
4751
  const isExists = this.fileUtil.isFileExists(dbName);
@@ -4682,7 +4768,7 @@ class CapacitorSQLite {
4682
4768
  // Import the JsonSQLite Object
4683
4769
  const changes = await database.importJson(vJsonObj);
4684
4770
  // Close the database
4685
- await database.dbClose();
4771
+ database.dbClose();
4686
4772
  return { changes: { changes: changes } };
4687
4773
  }
4688
4774
  catch (err) {
@@ -4697,7 +4783,7 @@ class CapacitorSQLite {
4697
4783
  const database = this.getDatabaseConnectionOrThrowError(connName);
4698
4784
  if (database.isDBOpen()) {
4699
4785
  try {
4700
- const exportJsonResult = await database.exportJson(exportMode);
4786
+ const exportJsonResult = database.exportJson(exportMode);
4701
4787
  const resultKeys = Object.keys(exportJsonResult);
4702
4788
  if (resultKeys.includes('message')) {
4703
4789
  throw new Error(`exportToJson: ${exportJsonResult.message}`);