Web SQL promise helper
npm install sql-promise-helperPromise-based Web SQL interface. Specify Web or Cordova SQL batch in multiple steps (useful with libraries like Oboe.js).
LICENSE: ISC, MIT, or Apache 2.0
This module depends on the standard Promise object. Please use a polyfill if necessary.
``js
var newPromiseHelper = require('sql-promise-helper').newPromiseHelper;
var db = window.openDatabase('test.db', '1.0', 'Test', 510241024);
var helper = newPromiseHelper(db);
var tx = helper.newBatchTransaction();
tx.executeStatement('DROP TABLE IF EXISTS tt');
tx.executeStatement('CREATE TABLE tt(a,b)');
tx.executeStatement('INSERT INTO tt VALUES(?,?)', [101, 'Alice']);
tx.commit().then(function() {
return helper.executeStatement('SELECT * FROM tt', null);
}).then(function(rs) {
alert('GOT RESULT LENGTH: ' + rs.rows.length + ' FIRST: ' + JSON.stringify(rs.rows.item(0)));
}).then(null, function(error) {
alert('GOT ERROR: ' + error.message);
});
`
or with sqlite plugin:
`js
var newPromiseHelper = require('sql-promise-helper').newPromiseHelper;
var db = window.sqlitePlugin.openDatabase({name: 'test.db', location: 'default'});
var helper = newPromiseHelper(db);
var tx = helper.newBatchTransaction();
tx.executeStatement('DROP TABLE IF EXISTS tt');
tx.executeStatement('CREATE TABLE tt(a,b)');
tx.executeStatement('INSERT INTO tt VALUES(?,?)', [101, 'Alice']);
tx.commit().then(function() {
return helper.executeStatement('SELECT * FROM tt', null);
}).then(function(rs) {
alert('GOT RESULT LENGTH: ' + rs.rows.length + ' FIRST: ' + JSON.stringify(rs.rows.item(0)));
}).then(null, function(error) {
alert('GOT ERROR: ' + error.message);
});
`
`js
import {newPromiseHelper} from 'sql-promise-helper';
const db = window.openDatabase('test.db', '1.0', 'Test', 510241024);
const helper = newPromiseHelper(db);
const tx = helper.newBatchTransaction();
tx.executeStatement('DROP TABLE IF EXISTS tt');
tx.executeStatement('CREATE TABLE tt(a,b)');
tx.executeStatement('INSERT INTO tt VALUES(?,?)', [101, 'Alice']);
tx.commit().then(function() {
return helper.executeStatement('SELECT * FROM tt', null);
}).then(function(rs) {
alert('GOT RESULT LENGTH: ' + rs.rows.length + ' FIRST: ' + JSON.stringify(rs.rows.item(0)));
}).then(null, function(error) {
alert('GOT ERROR: ' + error.message);
});
`
Then assemble the bundle using a tool such as RollupJS or JSPM.
GENERAL NOTE: Use of const and let may not work with some older browsers and devices. Possible solutions include:var` instead.
- Just use
- Use a tool like BabelJS (commonly used with JSPM, RollupJS, WebPack, etc.)
- Full sqlBatch call with Promise-based API
- Read-only API functions