From 463de4b4371cdb1765030e2c56fc3978c9e8732d Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Sat, 5 Sep 2015 15:37:21 -0500 Subject: [PATCH] Storage: query and comments --- ionic/storage/sql.ts | 45 ++++++++++++++++++++++++++++++++++++++++ ionic/storage/storage.ts | 20 +++++++++++++++--- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/ionic/storage/sql.ts b/ionic/storage/sql.ts index 080f74a377..509640f26a 100644 --- a/ionic/storage/sql.ts +++ b/ionic/storage/sql.ts @@ -58,6 +58,37 @@ export class SqlStorage extends StorageEngine { }); } + /** + * Perform an arbitrary SQL operation on the database. Use this method + * to have full control over the underlying database through SQL operations + * like SELECT, INSERT, and UPDATE. + * + * @param {string} query the query to run + * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} + */ + query(query) { + return new Promise((resolve, reject) => { + this._db.transaction((tx) => { + ts.executeSql(query, [], (tx, res) => { + resolve({ + tx: tx, + res: res + }); + }, (tx, err) => { + reject({ + tx: tx, + err: err + }); + }) + }); + }) + } + + /** + * Get the value in the database identified by the given key. + * @param {string} key the key + * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} + */ get(key) { return new Promise((resolve, reject) => { try { @@ -84,6 +115,13 @@ export class SqlStorage extends StorageEngine { } }); } + + /** + * Set the value in the database for the given key. Existing values will be overwritten. + * @param {string} key the key + * @param {string} value The value (as a string) + * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} + */ set(key, value) { return new Promise((resolve, reject) => { try { @@ -104,6 +142,13 @@ export class SqlStorage extends StorageEngine { } }); } + + /** + * Remove the value in the database for the given key. + * @param {string} key the key + * @param {string} value The value (as a string) + * @return {Promise} that resolves or rejects with an object of the form { tx: Transaction, res: Result (or err)} + */ remove(key) { return new Promise((resolve, reject) => { try { diff --git a/ionic/storage/storage.ts b/ionic/storage/storage.ts index b7b011d785..f1b2ede1fc 100644 --- a/ionic/storage/storage.ts +++ b/ionic/storage/storage.ts @@ -14,22 +14,36 @@ export class Storage { get(key) { return this._strategy.get(key); } + getJson(key) { + try { + return JSON.parse(this._strategy.get(key)); + } catch(e) { + console.warn('Storage getJson(): unable to parse value for key', key,' as JSON') + return null; + } + } set(key, value) { return this._strategy.set(key, value); } remove(key) { return this._strategy.remove(key); } + query(query) { + return this._strategy.query(key); + } } export class StorageEngine { get(key, value) { - throw Error("Not implemented"); + throw Error("get() not implemented for this storage engine"); } set(key, value) { - throw Error("Not implemented"); + throw Error("set() not implemented for this storage engine"); } remove(key) { - throw Error("Not implemented"); + throw Error("remove() not implemented for this storage engine"); + } + query(query) { + throw Error("query() not implemented for this storage engine"); } }