SQLite Client for Node.js Apps
A wrapper library that adds ES6 promises and SQL-based migrations API to sqlite3 (docs).
🔥 Want to strengthen your core JavaScript skills and master ES6?
I would personally recommend this awesome ES6 course by Wes Bos.
How to Install
$ npm install sqlite --save
How to Use
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:
;;; const app = ;const port = processenvPORT || 3000; app; Promise // First, try connect to the database // Finally, launch Node.js app ;
NOTE: For Node.js v5 and below use var db = require('sqlite/legacy');
.
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 (id INTEGER PRIMARY KEY, name TEXT); (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 Category Post;
migrations/002-missing-index.sql
-- Up ON Post (categoryId); -- Down Post_ix_categoryId;
app.js
(Node.js/Express)
;;; const app = ;const port = processenvPORT || 3000; app; Promise // First, try connect to the database and update its schema to the latest version // Finally, launch Node.js app ;
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.
Multiple Connections
The open
method resolves to the db instance which can be used in order to reference multiple open databases.
ES6
import sqlite from 'sqlite';
Promise.all([
sqlite.open('./main.sqlite', { Promise }),
sqlite.open('./users.sqlite', { Promise })
]).then(function([mainDb, usersDb]){
...
});
ES7+ Async/Await
import sqlite from 'sqlite';
async function main() {
const [mainDb, usersDb] = await Promise.all([
sqlite.open('./main.sqlite', { Promise }),
sqlite.open('./users.sqlite', { Promise })
]);
...
}
main();
References
- Using SQLite with Node.js for Rapid Prototyping on Medium.com
- SQLite Documentation, e.g. SQL Syntax, Data Types etc. on SQLite.org
Related Projects
- React Starter Kit — Isomorphic web app boilerplate (Node.js/Express, React.js, GraphQL)
- ASP.NET Core Starter Kit — Single-page app boilerplate (ASP.NET Core, React.js, Web API)
- Babel Starter Kit — JavaScript library boilerplate (ES2015, Babel, Rollup)
- Membership Database — SQL database boilerplate for web app users, roles and auth tokens
Support
- 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
License
The MIT License © 2015-2016 Kriasoft, LLC. All rights reserved.
Made with ♥ by Konstantin Tarkus (@koistya) and contributors