@capacitor-community/sqlite 3.4.2-2 → 3.4.2-5

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 (32) hide show
  1. package/README.md +16 -0
  2. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLite.java +43 -2
  3. package/android/src/main/java/com/getcapacitor/community/database/sqlite/CapacitorSQLitePlugin.java +25 -1
  4. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/Database.java +45 -22
  5. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ExportToJson.java +13 -14
  6. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/ImportFromJson.java +18 -9
  7. package/android/src/main/java/com/getcapacitor/community/database/sqlite/SQLite/ImportExportJson/JsonSQLite.java +26 -1
  8. package/dist/esm/definitions.d.ts +27 -3
  9. package/dist/esm/definitions.js +42 -0
  10. package/dist/esm/definitions.js.map +1 -1
  11. package/dist/esm/web.d.ts +1 -0
  12. package/dist/esm/web.js +19 -0
  13. package/dist/esm/web.js.map +1 -1
  14. package/dist/plugin.cjs.js +63 -2
  15. package/dist/plugin.cjs.js.map +1 -1
  16. package/dist/plugin.js +1227 -1166
  17. package/dist/plugin.js.map +1 -1
  18. package/electron/dist/plugin.js +89 -35
  19. package/electron/dist/plugin.js.map +1 -1
  20. package/ios/Plugin/CapacitorSQLite.swift +73 -3
  21. package/ios/Plugin/CapacitorSQLitePlugin.m +1 -0
  22. package/ios/Plugin/CapacitorSQLitePlugin.swift +28 -1
  23. package/ios/Plugin/Database.swift +24 -14
  24. package/ios/Plugin/ImportExportJson/ExportToJson.swift +27 -12
  25. package/ios/Plugin/ImportExportJson/ImportFromJson.swift +6 -3
  26. package/ios/Plugin/ImportExportJson/JsonSQLite.swift +6 -0
  27. package/ios/Plugin/Utils/UtilsBinding.swift +2 -2
  28. package/ios/Plugin/Utils/UtilsDrop.swift +8 -4
  29. package/ios/Plugin/Utils/UtilsJson.swift +22 -8
  30. package/ios/Plugin/Utils/UtilsSQLCipher.swift +14 -2
  31. package/ios/Plugin/Utils/UtilsUpgrade.swift +2 -1
  32. package/package.json +2 -2
@@ -495,6 +495,7 @@ class UtilsDrop {
495
495
  */
496
496
  async dropElements(db, type) {
497
497
  let msg = '';
498
+ let stmt1 = `AND name NOT LIKE ('sqlite_%')`;
498
499
  switch (type) {
499
500
  case 'index':
500
501
  msg = 'DropIndexes';
@@ -504,6 +505,7 @@ class UtilsDrop {
504
505
  break;
505
506
  case 'table':
506
507
  msg = 'DropTables';
508
+ stmt1 += ` AND name NOT IN ('sync_table')`;
507
509
  break;
508
510
  case 'view':
509
511
  msg = 'DropViews';
@@ -513,7 +515,7 @@ class UtilsDrop {
513
515
  }
514
516
  // get the element's names
515
517
  let stmt = 'SELECT name FROM sqlite_master WHERE ';
516
- stmt += `type = '${type}' AND name NOT LIKE 'sqlite_%';`;
518
+ stmt += `type = '${type}' ${stmt1};`;
517
519
  try {
518
520
  const elements = await this._uSQLite.queryAll(db, stmt, []);
519
521
  if (elements.length > 0) {
@@ -589,12 +591,12 @@ utilsDrop.UtilsDrop = UtilsDrop;
589
591
 
590
592
  Object.defineProperty(utilsJson, "__esModule", { value: true });
591
593
  utilsJson.UtilsJson = void 0;
592
- const utilsDrop_1$2 = utilsDrop;
594
+ const utilsDrop_1$3 = utilsDrop;
593
595
  const utilsSQLite_1$4 = utilsSQLite;
594
596
  class UtilsJson {
595
597
  constructor() {
596
598
  this._uSQLite = new utilsSQLite_1$4.UtilsSQLite();
597
- this._uDrop = new utilsDrop_1$2.UtilsDrop();
599
+ this._uDrop = new utilsDrop_1$3.UtilsDrop();
598
600
  }
599
601
  /**
600
602
  * isLastModified
@@ -1026,6 +1028,7 @@ class UtilsJson {
1026
1028
  const keyFirstLevel = [
1027
1029
  'database',
1028
1030
  'version',
1031
+ 'overwrite',
1029
1032
  'encrypted',
1030
1033
  'mode',
1031
1034
  'tables',
@@ -1041,6 +1044,8 @@ class UtilsJson {
1041
1044
  return false;
1042
1045
  if (key === 'version' && typeof obj[key] != 'number')
1043
1046
  return false;
1047
+ if (key === 'overwrite' && typeof obj[key] != 'boolean')
1048
+ return false;
1044
1049
  if (key === 'encrypted' && typeof obj[key] != 'boolean')
1045
1050
  return false;
1046
1051
  if (key === 'mode' && typeof obj[key] != 'string')
@@ -1999,14 +2004,14 @@ var importFromJson = {};
1999
2004
 
2000
2005
  Object.defineProperty(importFromJson, "__esModule", { value: true });
2001
2006
  importFromJson.ImportFromJson = void 0;
2002
- const utilsDrop_1$1 = utilsDrop;
2007
+ const utilsDrop_1$2 = utilsDrop;
2003
2008
  const utilsSQLite_1$2 = utilsSQLite;
2004
2009
  const utilsJson_1$3 = utilsJson;
2005
2010
  class ImportFromJson {
2006
2011
  constructor() {
2007
2012
  this._uJson = new utilsJson_1$3.UtilsJson();
2008
2013
  this._uSQLite = new utilsSQLite_1$2.UtilsSQLite();
2009
- this._uDrop = new utilsDrop_1$1.UtilsDrop();
2014
+ this._uDrop = new utilsDrop_1$2.UtilsDrop();
2010
2015
  }
2011
2016
  /**
2012
2017
  * CreateDatabaseSchema
@@ -2017,14 +2022,16 @@ class ImportFromJson {
2017
2022
  let changes = -1;
2018
2023
  const version = jsonData.version;
2019
2024
  try {
2020
- // set Foreign Keys On
2021
- await this._uSQLite.setForeignKeyConstraintsEnabled(mDB, true);
2022
2025
  // set User Version PRAGMA
2023
2026
  await this._uSQLite.setVersion(mDB, version);
2024
2027
  // DROP ALL when mode="full"
2025
2028
  if (jsonData.mode === 'full') {
2029
+ // set Foreign Keys Off
2030
+ await this._uSQLite.setForeignKeyConstraintsEnabled(mDB, false);
2026
2031
  await this._uDrop.dropAll(mDB);
2027
2032
  }
2033
+ // set Foreign Keys On
2034
+ await this._uSQLite.setForeignKeyConstraintsEnabled(mDB, true);
2028
2035
  // create database schema
2029
2036
  changes = await this._uJson.createSchema(mDB, jsonData);
2030
2037
  return Promise.resolve(changes);
@@ -2580,14 +2587,14 @@ var utilsUpgrade = {};
2580
2587
  Object.defineProperty(utilsUpgrade, "__esModule", { value: true });
2581
2588
  utilsUpgrade.UtilsUpgrade = void 0;
2582
2589
  const utilsJson_1$2 = utilsJson;
2583
- const utilsDrop_1 = utilsDrop;
2590
+ const utilsDrop_1$1 = utilsDrop;
2584
2591
  const utilsFile_1$2 = utilsFile;
2585
2592
  const utilsSQLite_1$1 = utilsSQLite;
2586
2593
  class UtilsUpgrade {
2587
2594
  constructor() {
2588
2595
  this._uSQLite = new utilsSQLite_1$1.UtilsSQLite();
2589
2596
  this._uFile = new utilsFile_1$2.UtilsFile();
2590
- this._uDrop = new utilsDrop_1.UtilsDrop();
2597
+ this._uDrop = new utilsDrop_1$1.UtilsDrop();
2591
2598
  this._uJson = new utilsJson_1$2.UtilsJson();
2592
2599
  this._alterTables = {};
2593
2600
  this._commonColumns = {};
@@ -2899,6 +2906,7 @@ const exportToJson_1 = exportToJson;
2899
2906
  const importFromJson_1 = importFromJson;
2900
2907
  const utilsJson_1$1 = utilsJson;
2901
2908
  //import { UtilsEncryption } from './utilsEncryption';
2909
+ const utilsDrop_1 = utilsDrop;
2902
2910
  const utilsFile_1$1 = utilsFile;
2903
2911
  const utilsSQLite_1 = utilsSQLite;
2904
2912
  const utilsUpgrade_1 = utilsUpgrade;
@@ -2910,6 +2918,7 @@ class Database {
2910
2918
  this._uFile = new utilsFile_1$1.UtilsFile();
2911
2919
  this._uSQLite = new utilsSQLite_1.UtilsSQLite();
2912
2920
  this._uJson = new utilsJson_1$1.UtilsJson();
2921
+ this._uDrop = new utilsDrop_1.UtilsDrop();
2913
2922
  // private _uGlobal: GlobalSQLite = new GlobalSQLite();
2914
2923
  // private _uEncrypt: UtilsEncryption = new UtilsEncryption();
2915
2924
  this._uUpg = new utilsUpgrade_1.UtilsUpgrade();
@@ -2968,31 +2977,21 @@ class Database {
2968
2977
  password,*/);
2969
2978
  const curVersion = await this._uSQLite.getVersion(this._mDB);
2970
2979
  this._isDBOpen = true;
2971
- if (this._version > curVersion) {
2972
- const keys = Object.keys(this._vUpgDict);
2973
- if (keys.length > 0) {
2974
- try {
2975
- // execute the upgrade flow process
2976
- await this._uUpg.onUpgrade(this._mDB, this._vUpgDict, this._dbName, curVersion, this._version);
2977
- // delete the backup database
2978
- await this._uFile.deleteFileName(`backup-${this._dbName}`);
2979
- }
2980
- catch (err) {
2981
- // restore the database from backup
2982
- try {
2983
- await this._uFile.restoreFileName(this._dbName, 'backup');
2984
- }
2985
- catch (err) {
2986
- return Promise.reject(`Open: ${err}`);
2987
- }
2988
- }
2980
+ if (this._version > curVersion &&
2981
+ Object.keys(this._vUpgDict).length > 0) {
2982
+ try {
2983
+ // execute the upgrade flow process
2984
+ await this._uUpg.onUpgrade(this._mDB, this._vUpgDict, this._dbName, curVersion, this._version);
2985
+ // delete the backup database
2986
+ await this._uFile.deleteFileName(`backup-${this._dbName}`);
2989
2987
  }
2990
- else {
2988
+ catch (err) {
2989
+ // restore the database from backup
2991
2990
  try {
2992
- await this._uSQLite.setVersion(this._mDB, this._version);
2991
+ await this._uFile.restoreFileName(this._dbName, 'backup');
2993
2992
  }
2994
2993
  catch (err) {
2995
- return Promise.reject(`SetVersion: ${this._version} ${err}`);
2994
+ return Promise.reject(`Open: ${err}`);
2996
2995
  }
2997
2996
  }
2998
2997
  }
@@ -3142,6 +3141,10 @@ class Database {
3142
3141
  return Promise.reject('No last_modified column in tables');
3143
3142
  }
3144
3143
  }
3144
+ else {
3145
+ changes = 0;
3146
+ }
3147
+ console.log(`>>> CreateSyncTable changes: ${changes}`);
3145
3148
  return Promise.resolve(changes);
3146
3149
  }
3147
3150
  catch (err) {
@@ -3348,6 +3351,20 @@ class Database {
3348
3351
  }
3349
3352
  }
3350
3353
  }
3354
+ async getTableList() {
3355
+ if (!this._isDBOpen) {
3356
+ let msg = `GetTableList: Database ${this._dbName} `;
3357
+ msg += `not opened`;
3358
+ return Promise.reject(msg);
3359
+ }
3360
+ try {
3361
+ const retArr = await this._uDrop.getTablesNames(this._mDB);
3362
+ return Promise.resolve(retArr);
3363
+ }
3364
+ catch (err) {
3365
+ return Promise.reject(`GetTableList: ${err}`);
3366
+ }
3367
+ }
3351
3368
  async importJson(jsonData) {
3352
3369
  let changes = 0;
3353
3370
  if (this._isDBOpen) {
@@ -3493,9 +3510,7 @@ class CapacitorSQLite {
3493
3510
  const dbName = options.database;
3494
3511
  keys = Object.keys(this._dbDict);
3495
3512
  if (!keys.includes(dbName)) {
3496
- return Promise.reject('CloseConnection command failed: No ' +
3497
- 'available connection for ' +
3498
- dbName);
3513
+ return Promise.resolve();
3499
3514
  }
3500
3515
  const mDB = this._dbDict[dbName];
3501
3516
  if (mDB.isDBOpen()) {
@@ -3583,7 +3598,28 @@ class CapacitorSQLite {
3583
3598
  return Promise.resolve(ret);
3584
3599
  }
3585
3600
  catch (err) {
3586
- return Promise.reject(`Open: ${err}`);
3601
+ return Promise.reject(`GetVersion: ${err}`);
3602
+ }
3603
+ }
3604
+ async getTableList(options) {
3605
+ let keys = Object.keys(options);
3606
+ if (!keys.includes('database')) {
3607
+ return Promise.reject('Must provide a database name');
3608
+ }
3609
+ const dbName = options.database;
3610
+ keys = Object.keys(this._dbDict);
3611
+ if (!keys.includes(dbName)) {
3612
+ return Promise.reject(`Open: No available connection for ${dbName}`);
3613
+ }
3614
+ const mDB = this._dbDict[dbName];
3615
+ try {
3616
+ const tableList = await mDB.getTableList();
3617
+ const ret = {};
3618
+ ret.values = tableList;
3619
+ return Promise.resolve(ret);
3620
+ }
3621
+ catch (err) {
3622
+ return Promise.reject(`GetTableList: ${err}`);
3587
3623
  }
3588
3624
  }
3589
3625
  async execute(options) {
@@ -3823,7 +3859,7 @@ class CapacitorSQLite {
3823
3859
  }
3824
3860
  }
3825
3861
  async importFromJson(options) {
3826
- var _a;
3862
+ var _a, _b;
3827
3863
  const keys = Object.keys(options);
3828
3864
  if (!keys.includes('jsonstring')) {
3829
3865
  return Promise.reject('Must provide a json object');
@@ -3837,14 +3873,32 @@ class CapacitorSQLite {
3837
3873
  const vJsonObj = jsonObj;
3838
3874
  const dbName = `${vJsonObj.database}SQLite.db`;
3839
3875
  const dbVersion = (_a = vJsonObj.version) !== null && _a !== void 0 ? _a : 1;
3876
+ const mode = vJsonObj.mode;
3877
+ const overwrite = (_b = vJsonObj.overwrite) !== null && _b !== void 0 ? _b : false;
3840
3878
  // const encrypted: boolean = vJsonObj.encrypted ?? false;
3841
3879
  // const mode: string = encrypted ? 'secret' : 'no-encryption';
3842
3880
  // Create the database
3843
3881
  const mDb = new Database_1.Database(dbName,
3844
3882
  /*encrypted, mode, */ dbVersion, {});
3845
3883
  try {
3884
+ if (overwrite && mode === 'full') {
3885
+ const isExists = this._uFile.isFileExists(dbName);
3886
+ if (isExists) {
3887
+ await this._uFile.deleteFileName(dbName);
3888
+ }
3889
+ }
3846
3890
  // Open the database
3847
3891
  await mDb.open();
3892
+ const tableList = await mDb.getTableList();
3893
+ if (mode === 'full' && tableList.length > 0) {
3894
+ const curVersion = await mDb.getVersion();
3895
+ if (dbVersion < curVersion) {
3896
+ return Promise.reject(`ImportFromJson: Cannot import a version lower than ${curVersion}`);
3897
+ }
3898
+ if (curVersion === dbVersion) {
3899
+ return Promise.resolve({ changes: { changes: 0 } });
3900
+ }
3901
+ }
3848
3902
  // Import the JsonSQLite Object
3849
3903
  const changes = await mDb.importJson(vJsonObj);
3850
3904
  // Close the database