Skip to content

brody4hire/sql-promise-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Web SQL promise helper

Promise-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

Promise dependency

This module depends on the standard Promise object. Please use a polyfill if necessary.

Sample usage

Normal CommonJS

var newPromiseHelper = require('sql-promise-helper').newPromiseHelper;

var db = window.openDatabase('test.db', '1.0', 'Test', 5*1024*1024);

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:

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);
});

ES6

import {newPromiseHelper} from 'sql-promise-helper';

const db = window.openDatabase('test.db', '1.0', 'Test', 5*1024*1024);

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:

  • Just use var instead.
  • Use a tool like BabelJS (commonly used with JSPM, RollupJS, WebPack, etc.)

FUTURE TBD

  • Full sqlBatch call with Promise-based API
  • Read-only API functions

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy