-
-
Save krackers/2dcc2bd2833941cdfba291d83c75aba8 to your computer and use it in GitHub Desktop.
Revisions
-
konsumer revised this gist
Nov 26, 2014 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,8 @@ /** * Usage: * var db = new DB('demo', '1.0', 'Demo', 2 * 1024 * 1024); * db.query('SELECT * FROM mytable') * .then(function(res){ * console.log(res.rows); * }, function(err){ @@ -13,7 +13,12 @@ * */ function DB(name, version, title, size){ this.db = openDatabase(name, version, title, size); } DB.prototype.query = function(sql, params){ var db = this.db; return new Promise(function(resolve, reject){ if (!db) return reject('no database.'); db.transaction(function(tx){ @@ -32,4 +37,5 @@ function query(sql, params){ }); }); }); }; -
konsumer created this gist
Nov 26, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // https://github.com/Philzen/WebSQL-Polyfill // https://github.com/jakearchibald/es6-promise /** * Usage: * var db = openDatabase('demo', '1.0', 'Demo', 2 * 1024 * 1024); * query('SELECT * FROM mytable') * .then(function(res){ * console.log(res.rows); * }, function(err){ * console.error(err); * }); * */ function query(sql, params){ return new Promise(function(resolve, reject){ if (!db) return reject('no database.'); db.transaction(function(tx){ tx.executeSql(sql, params||[], function(tx,res){ // tidy up var rows = []; for(var i=res.rows.length; i; i--){ rows.unshift(res.rows.item(i-1)); } var out = {rows:rows, rowsAffected:res.rowsAffected}; // don't worry about no insertId try{ out.insertId = res.insertId; }catch(e){} resolve(out); }, function(tx, err){ reject(err.message); }); }); }); }