node package manager

Introducing npm Enterprise add-ons. Integrate third-party dev tools into npm…

sqlite

SQLite client for Node.js applications with SQL-based migrations API

A wrapper library that adds ES6 promises and SQL-based migrations API to sqlite3 (docs).

$ npm install sqlite --save

This module has the same API as the original sqlite3 library (docs), except that all its API methods return ES6 Promises and do not accept callback arguments.

Below is an example of how to use it with Node.js, Express and Babel:

import express from 'express';
import Promise from 'bluebird';
import db from 'sqlite';
 
const app = express();
const port = process.env.PORT || 3000;
 
app.get('/post/:id', async (req, res, next) => {
  try {
    const [post, categories] = await Promise.all([
      db.get('SELECT * FROM Post WHERE id = ?', req.params.id),
      db.all('SELECT * FROM Category');
    ]);
    res.render('post', { post, categories });
  } catch (err) {
    next(err);
  }
});
 
Promise.resolve()
  // First, try connect to the database 
  .then(() => db.open('./database.sqlite', { Promise }))
  .catch(err => console.error(err.stack))
  // Finally, launch Node.js app 
  .finally(() => app.listen(port));

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

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

-- Up 
CREATE TABLE Category (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE Post (id INTEGER PRIMARY KEY, categoryId INTEGER, title TEXT,
  CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId)
    REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE);
INSERT INTO Category (id, name) VALUES (1'Business');
INSERT INTO Category (id, name) VALUES (2'Technology');
 
-- Down 
DROP TABLE Category
DROP TABLE Post;
-- Up 
CREATE INDEX Post_ix_categoryId ON Post (categoryId);
 
-- Down 
DROP INDEX Post_ix_categoryId;
import express from 'express';
import Promise from 'bluebird';
import db from 'sqlite';
 
const app = express();
const port = process.env.PORT || 3000;
 
app.use(/* app routes */);
 
Promise.resolve()
  // First, try connect to the database and update its schema to the latest version 
  .then(() => db.open('./database.sqlite', { Promise }))
  .then(() => db.migrate({ force: 'last' }))
  .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.

  • Join #node-sqlite chat room on Gitter to stay up to date regarding the project
  • Join #sqlite IRC chat room on Freenode about general discussion about SQLite

The MIT License © 2015-2016 Kriasoft, LLC. All rights reserved.


Made with ♥ by Konstantin Tarkus (@koistya) and contributors