executeSqlしか使わないという鉄の制約を課したうえで、sqliteプラグインのモックを使う
import { Injectable } from "@angular/core";
declare var window: any;
@Injectable()
export class DbMock {
mockdb;
databaseFilename: string = "mock.db";
constructor() {
this.mockdb = window.openDatabase(this.databaseFilename, "0.1", this.databaseFilename, 1024 * 1024);
}
public openDatabase() {}
public executeSql(sql, param) {
return new Promise((resolve, reject) => {
if (typeof param === "string") {
param = [];
}
this.mockdb.transaction(tx => tx.executeSql(sql, param, (tran, resp) => resolve(resp)), error => reject(error), () => {});
});
}
public sqlBatch(sqlArray) {
return new Promise((resolve, reject) => {
for (let i = 0; i < sqlArray.length; i++) {
this.executeSql(sqlArray[i], "")
.then(res => {})
.catch(error => reject(error));
}
resolve(true);
});
}
}
if (this.platform.is("cordova")) {
this.sqlite.create(config).then(db => {
this.initializeDatabaseTables(db, dbConfig).subscribe(
res => { },
error => console.error(error) ,
() => {
this.db = db
})
} else {
let db = this.dbMock
this.initializeDatabaseTables(db, dbConfig).subscribe(
res => { },
error => observer.error(error),
() => {
this.db = db
}
)
}