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

sqlite-pool

v1.2.5

Published

Pooled SQLite client for Node.js with migrations and transactions, based on node-sqlite

Downloads

7

Readme

Pooled SQLite Client for Node.js Apps

A wrapper library that adds ES6 promises, an SQL-based migrations API, connection pooling, and managed transactions to sqlite3 (docs). Originally based on the sqlite library.

How to Install

$ npm install sqlite-pool --save

How to Use

This module has a similar API as the original sqlite3 library (docs), except that its equivalent API methods return ES6 Promises and do not accept callback arguments. Full API listings can be found in API.md.

Below is an example of how to use it with Node.js and Express, using Bluebird instead of native Promises:

const express = require('express');
const Promise = require('bluebird');
const Sqlite = require('sqlite-pool');

const app = express();
const port = process.env.PORT || 3000;
const db = new Sqlite('./database.sqlite', { Promise });

app.get('/post/:id', (req, res, next) => {
  return Promise.all([
    db.get('SELECT * FROM Post WHERE id = ?', req.params.id),
    db.all('SELECT * FROM Category')
  ]).then(([post, categories]) => {
    res.render('post', { post, categories });
  }).catch(next);
});

// Launch Node.js app
app.listen(port);

NOTE: For Node.js v5 and below use var Sqlite = require('sqlite-pool/legacy');.

Multiple Connections

Due to the asynchronous interface which the sqlite3 Node.js library provides, isolation and query ordering is not guaranteed within any given connection. This module uses the generic-pool library to create multiple connections to an SQLite database if desired, and to queue requests made using methods of the Sqlite class.

The minimum/maximum number of open connections can be set with the min and max options when calling new Sqlite() (see the API reference for the full list of options).

Below is an example with the Sqlite.use() method of how one connection may perform a transaction isolated from other calls to the same Sqlite instance:

const express = require('express');
const Promise = require('bluebird');
const Sqlite = require('sqlite-pool');

const app = express();
const port = process.env.PORT || 3000;
const db = new Sqlite('./database.sqlite', { Promise, min: 2, max: 4 });

app.get('/post/:id', (req, res, next) => {
  return Promise.all([
    // This will acquire a database connection, and release it
    // once the returned Promise is resolved or rejected
    db.use((conn) => {
      let id = req.params.id;
      // This Promise chain will begin a transaction, and either
      // commit if successful or rollback if an error is thrown
      return conn.exec('BEGIN IMMEDIATE')
        .then(() => conn.get('SELECT * FROM Post WHERE id = ?', id))
        .then((post) => {
          if (post === undefined) {
            throw new Error(`Post id ${id} not found`);
          }
          return conn.run('UPDATE Post SET views = views + 1 WHERE id = ?', id)
            .then(() => conn.exec('COMMIT'))
            .then(() => post);
        })
        .catch(err => conn.exec('ROLLBACK').then(() => Promise.reject(err)));
    }),
    // This query will run in a separate connection, outside of
    // the above transaction
    db.all('SELECT * FROM Category')
  ]).then(([post, categories]) => {
    res.render('post', { post, categories });
  }).catch(next);
});

// Launch Node.js app
app.listen(port);

Transactions

This module also provides managed transactions, with automatic commit or rollback. Below is a modified version of the above example, using Sqlite.transaction():

const express = require('express');
const Promise = require('bluebird');
const Sqlite = require('sqlite-pool');

const app = express();
const port = process.env.PORT || 3000;
const db = new Sqlite('./database.sqlite', { Promise, min: 2, max: 4 });

app.get('/post/:id', (req, res, next) => {
  return Promise.all([
    // This will acquire a database connection, and release it
    // once the returned Promise is resolved or rejected
    db.transaction((trx) => {
      let id = req.params.id;
      // This Promise chain will begin a transaction, and either
      // commit if successful or rollback if an error is thrown
      return trx.get('SELECT * FROM Post WHERE id = ?', id)
        .then((post) => {
          if (post === undefined) {
            throw new Error(`Post id ${id} not found`);
          }
          return trx.run('UPDATE Post SET views = views + 1 WHERE id = ?', id)
            .then(() => post);
        });
    }),
    // This query will run in a separate connection, outside of
    // the above transaction
    db.all('SELECT * FROM Category')
  ]).then(([post, categories]) => {
    res.render('post', { post, categories });
  }).catch(next);
});

// Launch Node.js app
app.listen(port);

Migrations

This module comes with a lightweight migrations API that works with SQL-based migration files as the following example demonstrates:

migrations/001-initial-schema.sql
-- Up

CREATE TABLE Category (
  id   INTEGER PRIMARY KEY,
  name TEXT    NOT NULL
);

CREATE TABLE Post (
  id          INTEGER PRIMARY KEY,
  categoryId  INTEGER NOT NULL,
  title       TEXT    NOT NULL,
  views       NUMERIC NOT NULL DEFAULT 0,
  isPublished NUMERIC NOT NULL DEFAULT 0,
  CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId)
    REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT Post_ck_isPublished CHECK (isPublished IN (0, 1))
);

INSERT INTO Category (id, name) VALUES (1, 'Business');
INSERT INTO Category (id, name) VALUES (2, 'Technology');

-- Down

DROP TABLE Post;
DROP TABLE Category;
migrations/002-missing-index.sql
-- Up
CREATE INDEX Post_ix_categoryId ON Post (categoryId);

-- Down
DROP INDEX Post_ix_categoryId;
app.js (Node.js/Express)
const express = require('express');
const Promise = require('bluebird');
const Sqlite = require('sqlite-pool');

const app = express();
const port = process.env.PORT || 3000;
const db = new Sqlite('./database.sqlite', { Promise });

app.use(/* app routes */);

Promise.resolve()
  // First, try to update the database schema to the latest version
  .then(() => db.migrate({
    force: 'last',                  // Default: false
    table: 'migrations',            // Default: 'migrations'
    migrationsPath: './migrations'  // Default: './migrations'
  }))
  .catch(err => console.error(err.stack))
  // Finally, launch Node.js app
  .finally(() => app.listen(port));

NOTE: For the development environment, while working on the database schema, you may want to set force: 'last' (default false) that will force the migration API to rollback and re-apply the latest migration over again each time when Node.js app launches.

References

License

The MIT License © 2017 Raymond Neilson. All rights reserved. Original work © 2015-2016 Kriasoft, LLC. All rights reserved.

Original library by Konstantin Tarkus (@koistya) and contributors