Nodejs 有没有人用过 node-sqlcipher?
Nodejs 有没有人用过 node-sqlcipher?
想研究一下里面是怎么调用系统的 sqlite 的,不过 sqlcipher.js
文件在文首定义了这个变量。
var binding = require('./node_sqlcipher.node');
var sqlite3 = module.exports = exports = binding;
这里的 .node
文件是个什么东东啊,打开都是乱码。有木有大神解惑?
Nodejs 有没有人用过 node-sqlcipher?
问题描述
最近在尝试使用 node-sqlcipher
这个库来操作加密的 SQLite 数据库。然而,在查看源码时遇到了一些困惑,特别是在理解 .node
文件的作用。希望有人能提供一些指导。
背景信息
node-sqlcipher
是一个用于 Node.js 的 SQLCipher 库,它允许你在 Node.js 中使用加密的 SQLite 数据库。源码中定义了一个变量 binding
,通过 require
引入了 ./node_sqlcipher.node
文件。
var binding = require('./node_sqlcipher.node');
var sqlite3 = module.exports = exports = binding;
疑问
-
.node
文件是什么?.node
文件是一种预编译的二进制文件,通常由 C/C++ 代码编译生成。它们被 Node.js 用来直接与底层系统进行交互,从而提高性能。- 在这个例子中,
node_sqlcipher.node
是由 C/C++ 代码编译生成的,用于封装 SQLCipher 的功能,并提供给 JavaScript 代码调用。
-
如何使用
node-sqlcipher
?- 下面是一个简单的示例,展示如何使用
node-sqlcipher
创建一个加密的 SQLite 数据库并插入数据。
- 下面是一个简单的示例,展示如何使用
const sqlite3 = require('sqlcipher');
// 打开数据库(如果不存在则创建)
const db = new sqlite3.Database(':memory:', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => {
if (err) {
console.error(err.message);
return;
}
console.log('Connected to the in-memory database.');
});
// 设置密钥
db.run('PRAGMA key = "mySecretKey"');
// 创建表
db.run(`CREATE TABLE lorem (info TEXT)`, (err) => {
if (err) {
console.error(err.message);
return;
}
console.log('Table created.');
});
// 插入数据
const insertData = 'INSERT INTO lorem VALUES (?)';
const data = ['some info'];
db.run(insertData, data, function(err) {
if (err) {
console.error(err.message);
return;
}
console.log(`Row inserted: ${this.changes} changes`);
});
// 查询数据
db.all('SELECT * FROM lorem', [], (err, rows) => {
if (err) {
console.error(err.message);
return;
}
rows.forEach((row) => {
console.log(row.info);
});
});
// 关闭数据库连接
db.close((err) => {
if (err) {
console.error(err.message);
return;
}
console.log('Close the database connection.');
});
总结
node-sqlcipher
是一个强大的工具,可以让你在 Node.js 中使用加密的 SQLite 数据库。.node
文件则是预编译的二进制文件,用于封装底层的 C/C++ 代码,以实现高性能的数据操作。希望上述示例能够帮助你更好地理解和使用 node-sqlcipher
。
这是一个C++扩展,如果你想看它的源码,请看src
目录:https://github.com/delaballe/node-sqlcipher/tree/master/src
已经会用了,还是多谢解惑
node-sqlcipher
是一个用于 Node.js 的 SQLCipher 扩展库,它允许你在 Node.js 应用中使用加密的 SQLite 数据库。node-sqlcipher.node
文件是经过编译的二进制文件,它是通过绑定到 SQLite 和 SQLCipher 的 C/C++ 代码生成的。这种 .node
文件类似于其他语言中的动态链接库(如 .dll 或 .so),它们包含可以直接由 Node.js 调用的原生代码。
在 node-sqlcipher
中,.node
文件包含对 SQLCipher 功能的访问,并且是通过 require
加载的。这意味着它包含了实际执行数据库操作的底层逻辑。这也就是为什么你会看到一个直接导入 .node
文件的命令:
var binding = require('./node_sqlcipher.node');
var sqlite3 = module.exports = exports = binding;
在这个例子中,binding
变量包含了从 node_sqlcipher.node
加载的所有函数和方法。之后,这些功能被导出作为 sqlite3
对象,使得你可以像操作普通 SQLite 数据库一样使用它,只是现在你的数据库是加密的。
示例代码
以下是如何使用 node-sqlcipher
创建加密数据库、插入数据和查询数据的简单示例:
-
安装
node-sqlcipher
:npm install node-sqlcipher
-
示例代码:
const sqlite3 = require('node-sqlcipher'); // 打开或创建加密的数据库 let db = new sqlite3.Database(':memory:', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, (err) => { if (err) { console.error(err.message); return; } console.log('Connected to the in-memory SQlite database.'); }); // 创建表 db.run(`CREATE TABLE lorem (info TEXT)`, (err) => { if (err) { console.error(err.message); return; } console.log('Table created.'); }); // 插入数据 const insertData = 'INSERT INTO lorem VALUES (?)'; const info = ['important information']; db.run(insertData, info, function(err) { if (err) { console.error(err.message); return; } console.log(`Inserted row ${this.lastID}`); }); // 查询数据 db.all(`SELECT rowid AS id, info FROM lorem`, [], (err, rows) => { if (err) { console.error(err.message); return; } rows.forEach((row) => { console.log(row.id + ": " + row.info); }); }); // 关闭数据库连接 db.close();
此代码演示了如何连接到内存中的 SQLCipher 数据库,创建一个表,插入一条记录,并检索该记录。注意,为了保持示例简短,这里使用的是内存中的数据库(:memory:
),如果你想要持久化存储,可以指定一个文件路径来保存数据库文件。