websql-helper
A promise based wrapper library for the work with WebSQL. It's intended for simple cordova-apps and offer some new functions and a migration-system.
How to install
Like always
npm i websql-helper
How to use
In every file you want access to a WebSQL database simply require the library and use it right away in any async-function (or as a promise). You have to setup it once. Also you can give the migration sqls to the constructor at the start of the program.
anyServerFile.js
const DB = ;; async { let row = await ; console;}
And that's it!
One global instance
A normal, simple application is mostly working with only one database. To make the class management more easy, this library does the access-control for you - mainly as a singleton. (But you can create a new instance to access other databases.)
You need to call the library once in the beginning of your code and thus setting it up.
index.js
const DB = ; // The first call creates the global instance with your settings
After that you can use the library without parameter:
anotherAPIFile.js
const DB = ; // a second call directly returns the global instanceasync { let row = await ; console;}
New Functions
This class implements shorthand methods for WebSQL.
async { // shorthand for db.prepare('SELECT * FROM users').all(); let allUsers = await ; // result: [{id: 1, firstName: 'a', lastName: 'b', email: 'foo@b.ar'},{},...] // result for no result: [] // shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(userId); let row = await ; // result: {id: 1, firstName: 'a', lastName: 'b', email: 'foo@b.ar'} // result for no result: undefined // shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(999) || {}; let id firstname = await ; // result: id = 1; firstName = 'a' // result for no result: id = undefined; firstName = undefined // shorthand for db.prepare('SELECT * FROM users WHERE id=?').pluck(true).get(userId); let email = await ; // result: 'foo@b.ar' // result for no result: undefined // shorthand for db.prepare('SELECT * FROM users').all().map(e => e.email); let emails = await ; // result: ['foo@b.ar', 'foo2@b.ar', ...] // result for no result: [] // shorthand for db.prepare('SELECT * FROM users').all().reduce((o, e) => {o[e.lastName] = e.email; return o;}, {}); let emailsByLastName = await ; // result: {b: 'foo@b.ar', c: 'foo2@b.ar', ...} // result for no result: {}}
Insert, Update and Replace
There are shorthands for update
, insert
and replace
. They are intended to make programming of CRUD-Rest-API-functions easier. With a blacklist
or a whitelist
it's even possible to send a request's query (or body) directly into the database.
Update
// const numberOfChangedRows = DB().update(table, data, where, whitelist = undefined) // simple use with a object as where and no whitelistawait // data from a request and a array as a where and only editing of lastName and firstName is allowedawait // update with blacklist (id and email is not allowed; only valid columns of the table are allowed) and where is a shorthand for ['id = ?', req.body.id]await
Insert and replace
// const lastInsertID = DB().insert(table, datas, whitelist = undefined)// const lastInsertID = DB().replace(table, datas, whitelist = undefined) // simple use with an object and no whitelistawait // inserting two usersawait // data from a request and only lastName and firstName are setawait // replace with blacklist (id and email is not allowed; only valid columns of the table are allowed)await // or insertWithBlackList
Try and catch
If you want to put invalid values into the database, the functions will throw an error. So don't forget to surround the functions with a try-catch
. Here is an example for an express-server:
const Router = const bodyParser = const DB = router
Migrations
The migration in this library mimics the migration system of the excellent sqlite by Kriasoft.
To use this feature you have to give the migration queries to the initialization of the object.