import { Injectable } from '@angular/core'; import { DbSvc } from '../db/db'; @Injectable() export class Migration { constructor(public db: DbSvc) { } private list: any = []; private tableName: string = "app_migration"; private idColumn: string = "app_migration_uu"; public init() { let promise = new Promise((resolve, reject) => { this.db.query("Select * from " + this.tableName).then((result) => { this.list = this.db.fetchAll(result); resolve(this.list); }, (reject) => { reject(this.list); }); }); return promise; } public all() { return this.list; } public remove(appMigration) { let sql = "Delete from " + this.tableName + " WHERE " + this.idColumn + " = ? "; this.db.query(sql, [appMigration[this.idColumn]]).then((result) => { this.list.splice(this.list.indexOf(appMigration), 1); }); } public get(AppMigrationsID) { for ( let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == AppMigrationsID) { return this.list[i]; } } return null; } add(row) { this.db.addTableRow(this.tableName, row, true).then((result) => { //because of updation as default commit 21 pass true as demo or avoid error this.list.push(row); }); } update(row) { let obj = row; if (obj) { this.db.updateTableRow(this.tableName, obj).then((result) => { for ( let i = 0; i < this.list.length; i++) { if (this.list[i][this.idColumn] == obj[this.idColumn]) { this.list[i] = obj; break; } } }); } } addArray(objects) { objects.forEach(value => { if (this.get(value[this.idColumn]) == null) { this.add(value); } else { this.update(value); } }); } getmigrationByVersion(version) { if (this.list.version = version) return this.list[0].app_migration_uu; } }