与nodejs一起使用的SQLite

问题描述 投票:39回答:6

我正在使用node.js开发应用程序。在那,我愿意使用SQLite作为嵌入式数据库。我在网上搜索了SQLite npm模块。我找到了各种模块:

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

从文档和其他来源,我明白(1)同步操作,而(2)和(3)异步工作。所以,我放弃了使用计划(1)。

现在,我想知道(2)和(3)之间有什么区别,哪一个应该是首选的?我google了很多,但找不到多少帮助。

node.js sqlite
6个回答
32
投票

使用https://github.com/mapbox/node-sqlite3。它是异步的(几乎是必须的),它是最积极维护的,它在GitHub上拥有最多的星星。


16
投票

或者,您可以使用javascript嵌入式数据库。这样你只需要在你的应用程序中将数据库声明为package.jsonrequire()中的依赖项。

查看NeDB(我写的)或nStore例如。


8
投票

对于我的架构同步better-sqlite3似乎更好:

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

  • 完整的交易支持
  • 面向性能,效率和安全性
  • 易于使用的同步API(比异步API更快)
  • 自定义SQL函数支持
  • 64位整数支持(在您需要之前不可见)

4
投票

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));

注意:上面的示例仅适用于Node.js v6和更新版本(假设代码中使用的importasync/await语言特征与Babel一起使用)。对于早期版本的Node.js,请使用var db = require('sqlite/legacy');


0
投票

Grumdrig的模块似乎是Stack Overflow上引用最多的模块,也是其他网站上的模块。

此外,文档非常好:http://github.grumdrig.com/node-sqlite/

我对Node SQLite的经验相当少,但社区似乎已经选择了。


© www.soinside.com 2019 - 2024. All rights reserved.