@capacitor-community/sqlite
Advanced tools
Comparing version 2.4.0 to 2.4.1-devversion.1
@@ -1,3 +0,9 @@ | ||
## 2.4.0-beta.3 (2020-08-07) | ||
## 2.4.1-devversion.1 (2020-08-31) | ||
### Added Features (alpha not to be used in production) | ||
- add Handling version in Electron platform | ||
## 2.4.0 (2020-08-07) | ||
### Chores | ||
@@ -4,0 +10,0 @@ |
@@ -89,2 +89,9 @@ declare module '@capacitor/core' { | ||
setSyncDate(options: capSQLiteOptions): Promise<capSQLiteResult>; | ||
/** | ||
* Add an upgrade statement | ||
* @param {string} database Database Name | ||
* @param {capSQLiteVersionUpgrade} upgrade {fromVersion: number, toVersion: number, statement?: string, set?: Array<capSQLiteSet>} | ||
* @returns {Promise<capSQLiteResult>} {result:boolean} | ||
*/ | ||
addUpgradeStatement(database: string, upgrade: capSQLiteVersionUpgrade): Promise<capSQLiteResult>; | ||
} | ||
@@ -122,2 +129,6 @@ export interface capSQLiteOptions { | ||
/*** | ||
* Set Database Version | ||
*/ | ||
version?: number; | ||
/*** | ||
* Set the JSON object to import | ||
@@ -171,1 +182,7 @@ * | ||
} | ||
export interface capSQLiteVersionUpgrade { | ||
fromVersion: number; | ||
toVersion: number; | ||
statement?: string; | ||
set?: Array<capSQLiteSet>; | ||
} |
@@ -0,1 +1,2 @@ | ||
import { capSQLiteVersionUpgrade } from '../definitions'; | ||
import { JsonSQLite } from './JsonUtils'; | ||
@@ -5,4 +6,6 @@ export declare class DatabaseSQLiteHelper { | ||
private _databaseName; | ||
private _databaseVersion; | ||
private _upgradeStatements; | ||
private _utils; | ||
constructor(dbName: string); | ||
constructor(dbName: string, dbVersion: number | undefined, upgradeStatements: Record<string, Record<number, capSQLiteVersionUpgrade>>); | ||
private _openDB; | ||
@@ -15,2 +18,3 @@ createSyncTable(): Promise<any>; | ||
execSet(set: Array<any>): Promise<any>; | ||
private executeSet; | ||
run(statement: string, values: Array<any>): Promise<any>; | ||
@@ -39,2 +43,5 @@ private prepare; | ||
private getSyncDate; | ||
private getDBVersion; | ||
private updateDatabaseVersion; | ||
private onUpgrade; | ||
} |
@@ -14,7 +14,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
export class DatabaseSQLiteHelper { | ||
constructor(dbName /*, encrypted:boolean = false, mode:string = "no-encryption", | ||
secret:string = "",newsecret:string=""*/) { | ||
constructor(dbName, dbVersion = 1, upgradeStatements) { | ||
this.isOpen = false; | ||
this._utils = new UtilsSQLite(); | ||
this._databaseName = dbName; | ||
this._databaseVersion = dbVersion; | ||
this._upgradeStatements = upgradeStatements; | ||
// this._encrypted = encrypted; | ||
@@ -27,11 +28,42 @@ // this._mode = mode; | ||
_openDB() { | ||
const db = this._utils.connection(this._databaseName, false /*,this._secret*/); | ||
if (db != null) { | ||
this.isOpen = true; | ||
db.close(); | ||
} | ||
else { | ||
this.isOpen = false; | ||
console.log('openDB: Error Database connection failed'); | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let db = this._utils.connection(this._databaseName, false /*,this._secret*/); | ||
if (db != null) { | ||
this.isOpen = true; | ||
// check if the database got a version | ||
let curVersion = yield this.getDBVersion(db); | ||
if (curVersion === -1 || curVersion === 0) { | ||
this.updateDatabaseVersion(db, 1); | ||
curVersion = 1; | ||
} | ||
// check if the database version is Ok | ||
if (curVersion !== this._databaseVersion) { | ||
// version not ok | ||
if (this._databaseVersion < curVersion) { | ||
this.isOpen = false; | ||
console.log('openDB: Error Database version lower then current version'); | ||
} | ||
else if (Object.keys(this._upgradeStatements).length !== 0 || | ||
Object.keys(this._upgradeStatements[this._databaseName]).length !== 0) { | ||
this.isOpen = false; | ||
console.log('openDB: Error No upgrade statements found for that database'); | ||
} | ||
else { | ||
db = this.onUpgrade(this._databaseName, db, curVersion, this._databaseVersion); | ||
if (db != null) { | ||
this.isOpen = true; | ||
} | ||
else { | ||
this.isOpen = false; | ||
console.log('openDB: Error Failed on database version upgrading'); | ||
} | ||
} | ||
} | ||
db.close(); | ||
} | ||
else { | ||
this.isOpen = false; | ||
console.log('openDB: Error Database connection failed'); | ||
} | ||
}); | ||
} | ||
@@ -141,13 +173,34 @@ createSyncTable() { | ||
let retRes = { changes: -1, lastId: lastId }; | ||
const db = this._utils.connection(this._databaseName, false /*,this._secret*/); | ||
if (db === null) { | ||
this.isOpen = false; | ||
console.log('run: Error Database connection failed'); | ||
resolve(retRes); | ||
} | ||
const db = yield this._utils.connection(this._databaseName, false /*,this._secret*/); | ||
let retB = yield this.beginTransaction(db); | ||
if (!retB) { | ||
console.log('executeSet: Error beginTransaction failed'); | ||
db.close(); | ||
resolve(retRes); | ||
} | ||
retRes = yield this.executeSet(db, set); | ||
if (retRes.changes === -1) { | ||
console.log('executeSet: Error executeSet failed'); | ||
db.close(); | ||
return retRes; | ||
} | ||
retB = yield this.endTransaction(db); | ||
if (!retB) { | ||
console.log('executeSet: Error endTransaction failed'); | ||
db.close(); | ||
return retRes; | ||
} | ||
db.close(); | ||
resolve(retRes); | ||
})); | ||
} | ||
executeSet(db, set) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let lastId = -1; | ||
let retRes = { changes: -1, lastId: lastId }; | ||
if (db === null) { | ||
this.isOpen = false; | ||
console.log('executeSet: Error Database connection failed'); | ||
return retRes; | ||
} | ||
for (let i = 0; i < set.length; i++) { | ||
@@ -157,24 +210,16 @@ const statement = 'statement' in set[i] ? set[i].statement : null; | ||
if (statement == null || values == null) { | ||
console.log('execSet: Error statement or values are null'); | ||
db.close(); | ||
resolve(retRes); | ||
console.log('executeSet: Error statement or values are null'); | ||
return retRes; | ||
} | ||
lastId = yield this.prepare(db, statement, values); | ||
if (lastId === -1) { | ||
console.log('execSet: Error return lastId= -1'); | ||
db.close(); | ||
resolve(retRes); | ||
console.log('executeSet: Error return lastId= -1'); | ||
return retRes; | ||
} | ||
} | ||
retB = yield this.endTransaction(db); | ||
if (!retB) { | ||
db.close(); | ||
resolve(retRes); | ||
} | ||
const changes = yield this.dbChanges(db); | ||
retRes.changes = changes; | ||
retRes.lastId = lastId; | ||
db.close(); | ||
resolve(retRes); | ||
})); | ||
return retRes; | ||
}); | ||
} | ||
@@ -871,3 +916,106 @@ run(statement, values) { | ||
} | ||
getDBVersion(db) { | ||
return new Promise((resolve) => __awaiter(this, void 0, void 0, function* () { | ||
const query = `PRAGMA user_version;`; | ||
const resQuery = yield this.select(db, query, []); | ||
if (resQuery.length > 0) { | ||
return resolve(resQuery[0].user_version); | ||
} | ||
else { | ||
return resolve(-1); | ||
} | ||
})); | ||
} | ||
updateDatabaseVersion(db, newVersion) { | ||
db.run('PRAGMA user_version = $version;', { | ||
$version: newVersion, | ||
}); | ||
} | ||
onUpgrade(dbName, db, currentVersion, targetVersion) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
/** | ||
* When upgrade statements for current database are missing | ||
*/ | ||
if (!this._upgradeStatements[dbName]) { | ||
console.log(`Error PRAGMA user_version failed : Version mismatch! Expected Versi | ||
on ${targetVersion} found Version ${currentVersion}. Missing Upgrade S | ||
tatements for Database '${dbName}' Version ${currentVersion}.`); | ||
return null; | ||
} | ||
else if (!this._upgradeStatements[dbName][currentVersion]) { | ||
/** | ||
* When upgrade statements for current version are missing | ||
*/ | ||
console.log(`Error PRAGMA user_version failed : Version mismatch! Expected Versi | ||
on ${targetVersion} found Version ${currentVersion}. Missing Upgrade S | ||
tatements for Database '${dbName}' Version ${currentVersion}.`); | ||
return null; | ||
} | ||
const upgrade = this._upgradeStatements[dbName][currentVersion]; | ||
/** | ||
* When the version after an upgrade would be greater than the targeted version | ||
*/ | ||
if (targetVersion < upgrade.toVersion) { | ||
console.log(`Error PRAGMA user_version failed : Version mismatch! Expected Versi | ||
on ${targetVersion} found Version ${currentVersion}. Upgrade Stateme | ||
nt would upgrade to version ${upgrade.toVersion}, but target versio | ||
n is ${targetVersion}.`); | ||
return null; | ||
} | ||
let retB = yield this.beginTransaction(db); | ||
if (!retB) { | ||
console.log('executeSet: Error beginTransaction failed'); | ||
return null; | ||
} | ||
// TODO | ||
// -> copy database on temp_dbName | ||
// -> get the list of existing columns in each table of temp_dbName | ||
// Here we assume all the tables schema are given in the upgrade statement | ||
if (upgrade.statement) { | ||
const result = yield this.execute(db, upgrade.statement); | ||
if (result.changes < 0) { | ||
console.log(`Error PRAGMA user_version failed : Version mismatch! Expected Versi | ||
on ${targetVersion} found Version ${currentVersion}. Upgrade Stateme | ||
nt returned error.`); | ||
return null; | ||
} | ||
} | ||
// TODO | ||
// -> get the list of columns in each table of the database | ||
// -> get the intersection containing all columns in each table | ||
// -> restore the tables data for old existing columns | ||
// | ||
// here we assume that the Set contains only | ||
// - the data for new tables as INSERT statements | ||
// - the data for new columns in existing tables as UPDATE statements | ||
if (upgrade.set) { | ||
const result = yield this.executeSet(db, upgrade.set); | ||
if (result.changes < 0) { | ||
console.log(`Error PRAGMA user_version failed : Version mismatch! Expected Versi | ||
on ${targetVersion} found Version ${currentVersion}. Upgrade Stateme | ||
nt Set returned error.`); | ||
return null; | ||
} | ||
} | ||
this.updateDatabaseVersion(db, upgrade.toVersion); | ||
// TODO | ||
// -> DROP all Tables in temp_dbName | ||
retB = yield this.endTransaction(db); | ||
if (!retB) { | ||
console.log('executeSet: Error endTransaction failed'); | ||
return null; | ||
} | ||
/* Can you explain in which cases they will be some more updates to do | ||
// When there are still updates to do | ||
if (targetVersion > upgrade.toVersion) { | ||
return this.onUpgrade(dbName, db, upgrade.toVersion, targetVersion); | ||
} | ||
*/ | ||
// TODO | ||
// -> Delete temp_dbName | ||
return db; | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=DatabaseSQLiteHelper.js.map |
import { WebPlugin } from '@capacitor/core'; | ||
import { CapacitorSQLitePlugin, capSQLiteOptions, capSQLiteResult } from './definitions'; | ||
import { CapacitorSQLitePlugin, capSQLiteOptions, capSQLiteResult, capSQLiteVersionUpgrade } from './definitions'; | ||
export declare class CapacitorSQLitePluginElectron extends WebPlugin implements CapacitorSQLitePlugin { | ||
private mDb; | ||
private versionUpgrades; | ||
constructor(); | ||
@@ -24,4 +25,5 @@ echo(options: { | ||
setSyncDate(options: capSQLiteOptions): Promise<capSQLiteResult>; | ||
addUpgradeStatement(database: string, upgrade: capSQLiteVersionUpgrade): Promise<capSQLiteResult>; | ||
} | ||
declare const CapacitorSQLiteElectron: CapacitorSQLitePluginElectron; | ||
export { CapacitorSQLiteElectron }; |
@@ -22,2 +22,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}); | ||
this.versionUpgrades = {}; | ||
} | ||
@@ -30,2 +31,3 @@ echo(options) { | ||
open(options) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -39,2 +41,3 @@ if (typeof options.database === 'undefined') { | ||
const dbName = options.database; | ||
const dbVersion = (_a = options.version) !== null && _a !== void 0 ? _a : 1; | ||
/* | ||
@@ -46,3 +49,3 @@ let encrypted: boolean = options.encrypted ? options.encrypted : false; | ||
*/ | ||
this.mDb = new DatabaseSQLiteHelper(`${dbName}SQLite.db` /*,encrypted,inMode,secretKey,newsecretKey*/); | ||
this.mDb = new DatabaseSQLiteHelper(`${dbName}SQLite.db`, dbVersion, this.versionUpgrades /*,encrypted,inMode,secretKey,newsecretKey*/); | ||
if (!this.mDb.isOpen) { | ||
@@ -248,2 +251,3 @@ return Promise.reject({ | ||
importFromJson(options) { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -268,3 +272,4 @@ const retRes = { changes: -1 }; | ||
const dbName = `${jsonObj.database}SQLite.db`; | ||
this.mDb = new DatabaseSQLiteHelper(dbName); | ||
const dbVersion = (_a = options.version) !== null && _a !== void 0 ? _a : 1; | ||
this.mDb = new DatabaseSQLiteHelper(dbName, dbVersion, this.versionUpgrades); | ||
const ret = yield this.mDb.importJson(jsonObj); | ||
@@ -317,2 +322,16 @@ this.mDb.close(dbName); | ||
} | ||
addUpgradeStatement(database, upgrade) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.versionUpgrades[database]) { | ||
this.versionUpgrades[database] = {}; | ||
} | ||
this.versionUpgrades[database][upgrade.fromVersion] = { | ||
fromVersion: upgrade.fromVersion, | ||
toVersion: upgrade.toVersion, | ||
statement: upgrade.statement, | ||
set: upgrade.set, | ||
}; | ||
return Promise.resolve({ result: true }); | ||
}); | ||
} | ||
} | ||
@@ -319,0 +338,0 @@ const CapacitorSQLiteElectron = new CapacitorSQLitePluginElectron(); |
import { WebPlugin } from '@capacitor/core'; | ||
import { CapacitorSQLitePlugin, capSQLiteOptions, capSQLiteResult } from './definitions'; | ||
import { CapacitorSQLitePlugin, capSQLiteOptions, capSQLiteResult, capSQLiteVersionUpgrade } from './definitions'; | ||
export declare class CapacitorSQLiteWeb extends WebPlugin implements CapacitorSQLitePlugin { | ||
@@ -23,4 +23,5 @@ constructor(); | ||
setSyncDate(options: capSQLiteOptions): Promise<capSQLiteResult>; | ||
addUpgradeStatement(database: string, upgrade: capSQLiteVersionUpgrade): Promise<capSQLiteResult>; | ||
} | ||
declare const CapacitorSQLite: CapacitorSQLiteWeb; | ||
export { CapacitorSQLite }; |
@@ -102,2 +102,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
} | ||
addUpgradeStatement(database, upgrade) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.log('addUpgradeStatement', database, upgrade); | ||
return Promise.resolve({ result: false, message: 'Not implemented' }); | ||
}); | ||
} | ||
} | ||
@@ -104,0 +110,0 @@ const CapacitorSQLite = new CapacitorSQLiteWeb(); |
{ | ||
"name": "@capacitor-community/sqlite", | ||
"version": "2.4.0", | ||
"version": "2.4.1-devversion.1", | ||
"description": "Capacitor SQLite Plugin", | ||
@@ -31,3 +31,3 @@ "homepage": "https://github.com/capacitor-community/sqlite", | ||
"prettier": "^2.0.5", | ||
"prettier-plugin-java": "^0.8.0", | ||
"prettier-plugin-java": "^0.8.1", | ||
"pretty-quick": "^2.0.1", | ||
@@ -34,0 +34,0 @@ "rimraf": "^3.0.0", |
@@ -176,2 +176,3 @@ <p align="center"><br><img src="https://user-images.githubusercontent.com/236501/85893648-1c92e880-b7a8-11ea-926d-95355b8175c7.png" width="128" height="128" /></p> | ||
| isDBExists | ✅ | ✅ | ✅ | ❌ | | ||
| addUpgradeStatement | ❌ | ❌ | ✅ | ❌ | | ||
@@ -323,2 +324,5 @@ ## Documentation | ||
</tr> | ||
<tr> | ||
<td align="center"><a href="https://github.com/karyfars"><img src="https://avatars3.githubusercontent.com/u/303016" width="100px;" alt=""/><br /><sub><b>Adam</b></sub></a><br /><a href="https://github.com/capacitor-community/sqlite/commits?author=karyfars" title="Code">💻</a></td> | ||
</tr> | ||
</table> | ||
@@ -325,0 +329,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3714
334
9899776
154
1