diff --git a/ionic/platform/storage/local-storage.ts b/ionic/platform/storage/local-storage.ts index fdef90a2e0..d3b36b93ae 100644 --- a/ionic/platform/storage/local-storage.ts +++ b/ionic/platform/storage/local-storage.ts @@ -30,51 +30,62 @@ import {StorageEngine} from './storage'; * @see {@link /docs/v2/platform/storage/ Storage Platform Docs} */ export class LocalStorage extends StorageEngine { - constructor(options={}) { + constructor(options = {}) { super(); } -/** - * Get the value of a key in LocalStorage - * @param {String} key the key you want to lookup in LocalStorage - */ + /** + * Get the value of a key in LocalStorage + * @param {String} key the key you want to lookup in LocalStorage + */ get(key: string): Promise { return new Promise((resolve, reject) => { try { let value = window.localStorage.getItem(key); resolve(value); - } catch(e) { + } catch (e) { reject(e); } }); } -/** - * Set a key value pair and save it to LocalStorage - * @param {String} key the key you want to save to LocalStorage - * @param {Any} value the value of the key you're saving - */ + /** + * Set a key value pair and save it to LocalStorage + * @param {String} key the key you want to save to LocalStorage + * @param {Any} value the value of the key you're saving + */ set(key: string, value: string): Promise { return new Promise((resolve, reject) => { try { window.localStorage.setItem(key, value); resolve(); - } catch(e) { + } catch (e) { reject(e); } }); } -/** - * Remove a key from LocalStorage - * @param {String} key the key you want to remove from LocalStorage - */ + /** + * Remove a key from LocalStorage + * @param {String} key the key you want to remove from LocalStorage + */ remove(key: string): Promise { return new Promise((resolve, reject) => { try { window.localStorage.removeItem(key); resolve(); - } catch(e) { + } catch (e) { + reject(e); + } + }); + } + + clear(): Promise { + return new Promise((resolve, reject) => { + try { + window.localStorage.clear(); + resolve(); + } catch (e) { reject(e); } }); diff --git a/ionic/platform/storage/sql.ts b/ionic/platform/storage/sql.ts index ea071b43c8..d8ef3577ee 100644 --- a/ionic/platform/storage/sql.ts +++ b/ionic/platform/storage/sql.ts @@ -2,8 +2,8 @@ import {StorageEngine} from './storage'; import {defaults, assign} from '../../util/util'; -const DB_NAME :string = '__ionicstorage'; -const win :any = window; +const DB_NAME: string = '__ionicstorage'; +const win: any = window; /** * SqlStorage uses SQLite or WebSQL (development only!) to store data in a @@ -36,13 +36,13 @@ const win :any = window; * */ export class SqlStorage extends StorageEngine { - static BACKUP_LOCAL = 2; + static BACKUP_LOCAL = 2; static BACKUP_LIBRARY = 1; static BACKUP_DOCUMENTS = 0; private _db: any; - constructor(options={}) { + constructor(options = {}) { super(); let dbOptions = defaults(options, { @@ -68,8 +68,8 @@ export class SqlStorage extends StorageEngine { this._tryInit(); } - _getBackupLocation(dbFlag: number) { - switch(dbFlag) { + _getBackupLocation(dbFlag: number): number { + switch (dbFlag) { case SqlStorage.BACKUP_LOCAL: return 2; case SqlStorage.BACKUP_LIBRARY: @@ -83,11 +83,8 @@ export class SqlStorage extends StorageEngine { // Initialize the DB with our required tables _tryInit() { - this._db.transaction((tx) => { - tx.executeSql('CREATE TABLE IF NOT EXISTS kv (key text primary key, value text)', [], (tx, res) => { - }, (tx, err) => { - console.error('Storage: Unable to create initial storage tables', tx, err); - }); + this.query('CREATE TABLE IF NOT EXISTS kv (key text primary key, value text)').catch(err => { + console.error('Storage: Unable to create initial storage tables', err.tx, err.err); }); } @@ -100,26 +97,17 @@ export class SqlStorage extends StorageEngine { * @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)} */ - query(query, params=[]): Promise { + query(query: string, params = []): Promise { return new Promise((resolve, reject) => { try { this._db.transaction((tx) => { - tx.executeSql(query, params, (tx, res) => { - resolve({ - tx: tx, - res: res - }); - }, (tx, err) => { - reject({ - tx: tx, - err: err - }); - }) - }, err => { - reject(err); - }); - } catch(e) { - reject(e); + tx.executeSql(query, params, + (tx, res) => resolve({ tx: tx, res: res }), + (tx, err) => reject({ tx: tx, err: err })); + }, + (err) => reject({ err: err })); + } catch (err) { + reject({ err: err }); } }) } @@ -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)} */ get(key: string): Promise { - return new Promise((resolve, reject) => { - try { - - 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); + return this.query('select key, value from kv where key = ? limit 1', [key]).then(data => { + if (data.res.rows.length > 0) { + return data.res.rows.item(0); } }); } @@ -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)} */ set(key: string, value: string): Promise { - return new Promise((resolve, reject) => { - 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); - } - }); + return this.query('insert or replace into kv(key, value) values (?, ?)', [key, value]); } /** @@ -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)} */ remove(key: string): Promise { - return new Promise((resolve, reject) => { - 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); - } - }); + return this.query('delete from kv where key = ?', [key]); + } + clear(): Promise { + return this.query('delete from kv'); } } diff --git a/ionic/platform/storage/storage.ts b/ionic/platform/storage/storage.ts index abd6cc2e74..a63f83cb2b 100644 --- a/ionic/platform/storage/storage.ts +++ b/ionic/platform/storage/storage.ts @@ -11,15 +11,17 @@ * @private */ export class Storage { - private _strategy: any; + private _strategy: StorageEngine; constructor(strategyCls: IStorageEngine, options?: any) { this._strategy = new strategyCls(options); } - get(key: string): any { + + get(key: string): Promise { return this._strategy.get(key); } - getJson(key: string): any { + + getJson(key: string): Promise { return this.get(key).then(value => { try { return JSON.parse(value); @@ -29,6 +31,7 @@ export class Storage { } }); } + setJson(key: string, value: any): Promise { try { return this.set(key, JSON.stringify(value)); @@ -36,37 +39,47 @@ export class Storage { return Promise.reject(e); } } + set(key: string, value: any) { return this._strategy.set(key, value); } + remove(key: string) { return this._strategy.remove(key); } + query(query: string, params?: any) { return this._strategy.query(query, params); } + + clear() { + return this._strategy.clear(); + } } export interface IStorageEngine { - new(options: any): StorageEngine; + new (options: any): StorageEngine; } /** * @private */ export class StorageEngine { - constructor(options={}) { - } - get(key, value) { + constructor(options = {}) { } + + get(key: string): Promise { throw Error("get() not implemented for this storage engine"); } - set(key, value) { + set(key: string, value: any): Promise { throw Error("set() not implemented for this storage engine"); } - remove(key) { + remove(key: string): Promise { throw Error("remove() not implemented for this storage engine"); } - query(query, params) { + query(query: string, params?: any): Promise { throw Error("query() not implemented for this storage engine"); } + clear(): Promise { + throw Error("clear() not implemented for this storage engine"); + } }