43

我正在使用node.js. 在那我愿意SQLite用作嵌入式数据库。我在网上搜索了SQLitenpm 模块。我找到了各种模块:

  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)之间有什么区别,应该首选哪一个?我用谷歌搜索了很多,但找不到太多帮助。

4

6 回答 6

34

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

于 2013-04-09T23:46:26.937 回答
17

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

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

于 2013-05-24T16:27:18.987 回答
13

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

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

  • 完整的交易支持
  • 为性能、效率和安全而生
  • 易于使用的同步 API(比异步 API 更快)
  • 自定义 SQL 函数支持
  • 64 位整数支持(在需要之前不可见)
于 2017-10-09T04:33:01.463 回答
5

用于 Node.js 应用程序的 SQLite 客户端/w 内置基于 SQL 的迁移 API

NPM 版本 NPM 下载 在线聊天

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

于 2016-05-22T07:48:31.310 回答
0

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

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

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

于 2013-04-09T23:39:14.153 回答
0

我已从https://github.com/mapbox/node-sqlite3切换到https://github.com/JoshuaWise/better-sqlite3。原因之一是 better-sqlite 作者给了我一个彻底的答案,https://github.com/JoshuaWise/better-sqlite3/issues/181,关于为什么https://github.com/JoshuaWise/better-sqlite3#why-我应该使用这个而不是节点-sqlite3

于 2018-12-02T05:32:11.240 回答