Merge pull request #5846 from manucorporat/refactor-storage

refactor(storage): improve code reuse and add clean() method
This commit is contained in:
Adam Bradley
2016-03-15 09:27:15 -05:00
3 changed files with 75 additions and 113 deletions

View File

@ -79,4 +79,15 @@ export class LocalStorage extends StorageEngine {
} }
}); });
} }
clear(): Promise<any> {
return new Promise((resolve, reject) => {
try {
window.localStorage.clear();
resolve();
} catch (e) {
reject(e);
}
});
}
} }

View File

@ -68,7 +68,7 @@ export class SqlStorage extends StorageEngine {
this._tryInit(); this._tryInit();
} }
_getBackupLocation(dbFlag: number) { _getBackupLocation(dbFlag: number): number {
switch (dbFlag) { switch (dbFlag) {
case SqlStorage.BACKUP_LOCAL: case SqlStorage.BACKUP_LOCAL:
return 2; return 2;
@ -83,11 +83,8 @@ export class SqlStorage extends StorageEngine {
// Initialize the DB with our required tables // Initialize the DB with our required tables
_tryInit() { _tryInit() {
this._db.transaction((tx) => { this.query('CREATE TABLE IF NOT EXISTS kv (key text primary key, value text)').catch(err => {
tx.executeSql('CREATE TABLE IF NOT EXISTS kv (key text primary key, value text)', [], (tx, res) => { console.error('Storage: Unable to create initial storage tables', err.tx, err.err);
}, (tx, err) => {
console.error('Storage: Unable to create initial storage tables', tx, err);
});
}); });
} }
@ -100,26 +97,17 @@ export class SqlStorage extends StorageEngine {
* @param {array} params the additional params to use for query placeholders * @param {array} params the additional params to use for query placeholders
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
query(query, params=[]): Promise<any> { query(query: string, params = []): Promise<any> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
try { try {
this._db.transaction((tx) => { this._db.transaction((tx) => {
tx.executeSql(query, params, (tx, res) => { tx.executeSql(query, params,
resolve({ (tx, res) => resolve({ tx: tx, res: res }),
tx: tx, (tx, err) => reject({ tx: tx, err: err }));
res: res },
}); (err) => reject({ err: err }));
}, (tx, err) => { } catch (err) {
reject({ reject({ err: err });
tx: tx,
err: err
});
})
}, err => {
reject(err);
});
} catch(e) {
reject(e);
} }
}) })
} }
@ -130,28 +118,9 @@ export class SqlStorage extends StorageEngine {
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
get(key: string): Promise<any> { get(key: string): Promise<any> {
return new Promise((resolve, reject) => { return this.query('select key, value from kv where key = ? limit 1', [key]).then(data => {
try { if (data.res.rows.length > 0) {
return data.res.rows.item(0);
this._db.transaction(tx => {
tx.executeSql("select key, value from kv where key = ? limit 1", [key], (tx, res) => {
if (res.rows.length > 0) {
let item = res.rows.item(0);
resolve(item.value);
}
resolve(null);
}, (tx, err) => {
reject({
tx: tx,
err: err
});
})
}, err => {
reject(err);
});
} catch(e) {
reject(e);
} }
}); });
} }
@ -163,24 +132,7 @@ export class SqlStorage extends StorageEngine {
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
set(key: string, value: string): Promise<any> { set(key: string, value: string): Promise<any> {
return new Promise((resolve, reject) => { return this.query('insert or replace into kv(key, value) values (?, ?)', [key, value]);
try {
this._db.transaction(tx => {
tx.executeSql('insert or replace into kv(key, value) values (?, ?)', [key, value], (tx, res) => {
resolve();
}, (tx, err) => {
reject({
tx: tx,
err: err
});
})
}, err => {
reject(err);
});
} catch(e) {
reject(e);
}
});
} }
/** /**
@ -189,24 +141,10 @@ export class SqlStorage extends StorageEngine {
* @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)}
*/ */
remove(key: string): Promise<any> { remove(key: string): Promise<any> {
return new Promise((resolve, reject) => { return this.query('delete from kv where key = ?', [key]);
try {
this._db.transaction(tx => {
tx.executeSql('delete from kv where key = ?', [key], (tx, res) => {
resolve();
}, (tx, err) => {
reject({
tx: tx,
err: err
});
})
}, err => {
reject(err);
});
} catch(e) {
reject(e);
} }
});
clear(): Promise<any> {
return this.query('delete from kv');
} }
} }

View File

@ -11,15 +11,17 @@
* @private * @private
*/ */
export class Storage { export class Storage {
private _strategy: any; private _strategy: StorageEngine;
constructor(strategyCls: IStorageEngine, options?: any) { constructor(strategyCls: IStorageEngine, options?: any) {
this._strategy = new strategyCls(options); this._strategy = new strategyCls(options);
} }
get(key: string): any {
get(key: string): Promise<any> {
return this._strategy.get(key); return this._strategy.get(key);
} }
getJson(key: string): any {
getJson(key: string): Promise<any> {
return this.get(key).then(value => { return this.get(key).then(value => {
try { try {
return JSON.parse(value); return JSON.parse(value);
@ -29,6 +31,7 @@ export class Storage {
} }
}); });
} }
setJson(key: string, value: any): Promise<any> { setJson(key: string, value: any): Promise<any> {
try { try {
return this.set(key, JSON.stringify(value)); return this.set(key, JSON.stringify(value));
@ -36,15 +39,22 @@ export class Storage {
return Promise.reject(e); return Promise.reject(e);
} }
} }
set(key: string, value: any) { set(key: string, value: any) {
return this._strategy.set(key, value); return this._strategy.set(key, value);
} }
remove(key: string) { remove(key: string) {
return this._strategy.remove(key); return this._strategy.remove(key);
} }
query(query: string, params?: any) { query(query: string, params?: any) {
return this._strategy.query(query, params); return this._strategy.query(query, params);
} }
clear() {
return this._strategy.clear();
}
} }
export interface IStorageEngine { export interface IStorageEngine {
@ -55,18 +65,21 @@ export interface IStorageEngine {
* @private * @private
*/ */
export class StorageEngine { export class StorageEngine {
constructor(options={}) { constructor(options = {}) { }
}
get(key, value) { get(key: string): Promise<any> {
throw Error("get() not implemented for this storage engine"); throw Error("get() not implemented for this storage engine");
} }
set(key, value) { set(key: string, value: any): Promise<any> {
throw Error("set() not implemented for this storage engine"); throw Error("set() not implemented for this storage engine");
} }
remove(key) { remove(key: string): Promise<any> {
throw Error("remove() not implemented for this storage engine"); throw Error("remove() not implemented for this storage engine");
} }
query(query, params) { query(query: string, params?: any): Promise<any> {
throw Error("query() not implemented for this storage engine"); throw Error("query() not implemented for this storage engine");
} }
clear(): Promise<any> {
throw Error("clear() not implemented for this storage engine");
}
} }