什么 SQLite 与 nodejs 一起使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14355004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What SQLite to use with nodejs
提问by jsist
I am developing application using node.js. In that I am willing to use SQLiteas embedded database. I searched online for SQLitenpm modules. I found various modules:
我正在使用node.js. 因为我愿意SQLite用作嵌入式数据库。我在网上搜索了SQLitenpm 模块。我找到了各种模块:
- https://github.com/grumdrig/node-sqlite
- https://github.com/orlandov/node-sqlite
- https://github.com/developmentseed/node-sqlite3
- https://github.com/grumdrig/node-sqlite
- https://github.com/orlandov/node-sqlite
- 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).
从文档和其他来源,我了解到 (1) 同步运行,而 (2) 和 (3) 异步运行。所以,我放弃了使用(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.
现在,我想知道(2)和(3)之间有什么区别,应该首选哪一个?我用谷歌搜索了很多,但找不到太多帮助。
采纳答案by Trevor Dixon
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.
使用https://github.com/mapbox/node-sqlite3。它是异步的(几乎是必备的),它是维护最积极的,并且它在 GitHub 上拥有最多的星星。
回答by Louis Chatriot
Alternatively, you could use a javascript embedded database. This way you just need to declare the database as a dependency in your package.jsonand require()it in your application.
或者,您可以使用 javascript 嵌入式数据库。这样,你只需要声明的数据库在你的依赖package.json,并require()在你的应用程序。
回答by Aleksey Kontsevich
For my architecture synchronous better-sqlite3appeared to be better:
对于我的架构,同步better-sqlite3似乎更好:
https://www.npmjs.com/package/better-sqlite3
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)
- 全面的交易支持
- 以性能、效率和安全为导向
- 易于使用的同步 API(比异步 API 更快)
- 自定义 SQL 函数支持
- 64 位整数支持(在需要之前不可见)
回答by Konstantin Tarkus
SQLite Client for Node.js Apps/w built-in SQL-based migrations API
用于 Node.js 应用程序的 SQLite 客户端/w 内置基于 SQL 的迁移 API
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.jsv6 and newer (assuming that importand async/awaitlanguage features used in the code are transpalied with Babel). For earlier versions of Node.js use var db = require('sqlite/legacy');.
注意:上面的示例仅适用于Node.jsv6 和更新版本(假设代码中使用的import和async/await语言功能使用Babel 进行了转换)。对于较早版本的 Node.js,请使用var db = require('sqlite/legacy');.
回答by zcserei
Grumdrig's module seems to be the one referenced the most on Stack Overflow, and on other sites as well.
Grumdrig 的模块似乎是 Stack Overflow 和其他网站上引用最多的模块。
Also, the documentation is pretty good: http://github.grumdrig.com/node-sqlite/
此外,文档非常好:http: //github.grumdrig.com/node-sqlite/
I have rather little experience with Node SQLite, but the community seems to have chosen.
我对 Node SQLite 的经验很少,但社区似乎已经选择了。
回答by Qiulang
I have switched from https://github.com/mapbox/node-sqlite3to 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
我已从https://github.com/mapbox/node-sqlite3切换到https://github.com/JoshuaWise/better-sqlite3。原因之一是 Better-sqlites 作者给了我一个彻底的答案,https://github.com/JoshuaWise/better-sqlite3/issues/181,关于为什么https://github.com/JoshuaWise/better-sqlite3#why- should-i-use-this-instead-of-node-sqlite3


