npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

websql-helper

v1.1.2

Published

A wrapper library that eases the work with websql with some new functions and promises

Downloads

12

Readme

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 = require('websql-helper');
DB({
  db: openDatabase('demo', '1.0', 'Demo', 2 * 1024 * 1024),
  migration: [
`-- Up
CREATE TABLE users (
  id INTEGER PRIMARY KEY, 
  firstName TEXT NOT NULL, 
  lastName TEXT NOT NULL, 
  email TEXT NOT NULL
);

-- Down
DROP TABLE IF EXISTS users;`
  ]
});

(async () => {
  let row = await DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
  console.log(row.firstName, row.lastName, row.email);
})()

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 = require('websql-helper');

// The first call creates the global instance with your settings
DB({
  db: openDatabase('demo', '1.0', 'Demo', 2 * 1024 * 1024),
  migration: [ require('migration/001.sql'), require('migration/002.sql') ]
})

After that you can use the library without parameter:

anotherAPIFile.js
const DB = require('websql-helper');

// a second call directly returns the global instance
(async ()=>{
  let row = await DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
  console.log(row.firstName, row.lastName, row.email);
})()

New Functions

This class implements shorthand methods for WebSQL.

(async ()=>{
  // shorthand for db.prepare('SELECT * FROM users').all(); 
  let allUsers = await DB().query('SELECT * FROM users');
  // result: [{id: 1, firstName: 'a', lastName: 'b', email: '[email protected]'},{},...]
  // result for no result: []

  // shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(userId); 
  let row = await DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
  // result: {id: 1, firstName: 'a', lastName: 'b', email: '[email protected]'}
  // result for no result: undefined

  // shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(999) || {}; 
  let {id, firstname} = await DB().queryFirstRowObject('SELECT * FROM users WHERE id=?', userId);
  // 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 DB().queryFirstCell('SELECT email FROM users WHERE id=?', userId);
  // result: '[email protected]'
  // result for no result: undefined

  // shorthand for db.prepare('SELECT * FROM users').all().map(e => e.email); 
  let emails = await DB().queryColumn('email', 'SELECT email FROM users');
  // result: ['[email protected]', '[email protected]', ...]
  // 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 DB().queryKeyAndColumn('lastName', 'email', 'SELECT lastName, name FROM users');
  // result: {b: '[email protected]', c: '[email protected]', ...}
  // 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 whitelist
await DB().update('users', {
  lastName: 'Mustermann',
  firstName: 'Max'
}, {
  email: '[email protected]'
})

// data from a request and a array as a where and only editing of lastName and firstName is allowed
await DB().update('users', req.body, ['email = ?', req.body.email], ['lastName', 'firstName'])


// 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 DB().updateWithBlackList('users', req.body, req.body.id, ['id', 'email'])

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 whitelist
await DB().insert('users', {
  lastName: 'Mustermann',
  firstName: 'Max',
  email: '[email protected]'
})

// inserting two users
await DB().insert('users', [{
  lastName: 'Mustermann',
  firstName: 'Max',
  email: '[email protected]'
}, {
  lastName: 'Mustermann2',
  firstName: 'Max2',
  email: '[email protected]'
}])

// data from a request and only lastName and firstName are set
await DB().replace('users', req.body, ['lastName', 'firstName'])


// replace with blacklist (id and email is not allowed; only valid columns of the table are allowed)
await DB().replaceWithBlackList('users', req.body, ['id', 'email']) // 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 } = require('express')
const bodyParser = require('body-parser')
const DB = require('sqlite3-helper')

router.patch('/user/:id', bodyParser.json(), async function (req, res, next) {
  try {
    if (!req.params.id) {
      res.status(400).json({error: 'missing id'})
      return
    }
    await DB().updateWithBlackList(
      'users',
      req.body,
      req.params.id,
      ['id']
    )

    res.statusCode(200)
  } catch (e) {
    console.error(e)
    res.status(503).json({error: e.message})
  }
})

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.

License

MIT