44

I am developing application using node.js. In that I am willing to use SQLite as embedded database. I searched online for SQLite npm modules. I found various modules:

  1. https://github.com/grumdrig/node-sqlite
  2. https://github.com/orlandov/node-sqlite
  3. https://github.com/developmentseed/node-sqlite3

From documentation and other sources, I understood that (1) operates synchronously, while (2) and (3) works asynchronously. So, I dropped the plan to use (1).

Now, I want to know what is the difference between (2) and (3) and which one should be preferred? I googled a lot but could not find much to help.

1

6 Answers 6

35

Use https://github.com/mapbox/node-sqlite3. It's asynchronous (almost a must-have), it's the most actively maintained, and it has the most stars on GitHub.

Sign up to request clarification or add additional context in comments.

2 Comments

Hasn't been updated in about a year. So not the most active.
Updated to Node 8 just recently (5th August 2017 at the time of this writing).
17

Alternatively, you could use a javascript embedded database. This way you just need to declare the database as a dependency in your package.json and require() it in your application.

Check out NeDB (which I wrote) or nStore for example.

2 Comments

Check out also FinalDB (npmjs.org/package/final-db) as an another embedded db solution.
These are probably the best solutions for document based databases.
14

For my architecture synchronous better-sqlite3 appeared to be better:

https://www.npmjs.com/package/better-sqlite3

  • Full transaction support
  • Geared for performance, efficiency, and safety
  • Easy-to-use synchronous API (faster than an asynchronous API)
  • Custom SQL function support
  • 64-bit integer support (invisible until you need it)

2 Comments

Wouldn't these synchronous calls block the main thread? Like if you have an application that deals with precise timing, it would be randomly freezing when you perform sql queries, wouldn't it? And each insert/update requires IO operations, right?
npmjs.com/package/better-sqlite3 has some specs, if they're to be believed it's ridiculously faster and that means block away and it's still faster. Furthermore, I can imagine if you want async functionality, you can wrap your own async function around any calls.
5

SQLite Client for Node.js Apps /w built-in SQL-based migrations API

NPM version NPM downloads Online Chat

import express from 'express';
import db from 'sqlite';                                       // <=
import Promise from 'bluebird';

const app = express();
const port = process.env.PORT || 3000;

app.get('/posts', async (req, res, next) => {
  try {
    const posts = await db.all('SELECT * FROM Post LIMIT 10'); // <=
    res.send(posts);
  } catch (err) {
    next(err);
  }
});

Promise.resolve()
  // First, try to open the database
  .then(() => db.open('./database.sqlite', { Promise })        // <=
  // Update db schema to the latest version using SQL-based migrations
  .then(() => db.migrate({ force: 'last' })                    // <=
  // Display error message if something went wrong
  .catch((err) => console.error(err.stack))
  // Finally, launch the Node.js app
  .finally(() => app.listen(port));

NOTE: The example above only works with Node.js v6 and newer (assuming that import and async/await language features used in the code are transpalied with Babel). For earlier versions of Node.js use var db = require('sqlite/legacy');.

2 Comments

This implementation made my app more interesting. Thanks! :)
Here the link to your article about that medium.com/@tarkus/…
1

I have switched from https://github.com/mapbox/node-sqlite3 to https://github.com/JoshuaWise/better-sqlite3. One of reasons is better-sqlites author gave me a thorough answer, https://github.com/JoshuaWise/better-sqlite3/issues/181, about why https://github.com/JoshuaWise/better-sqlite3#why-should-i-use-this-instead-of-node-sqlite3

Comments

-1

Grumdrig's module seems to be the one referenced the most on Stack Overflow, and on other sites as well.

Also, the documentation is pretty good: http://github.grumdrig.com/node-sqlite/

I have rather little experience with Node SQLite, but the community seems to have chosen.

1 Comment

Don't use this one (mine). It's old, abandoned, and only synchronous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.