Sur*_*ran 2 sqlite node.js phantomjs browserify
我一直在使用phantomjs在服务器端dom
环境中为我做一些繁重的工作.直到现在我一直把数据结构放在内存中(即对它们没什么特别的),一切都很好.但最近在一些用例中我开始遇到以下问题:
这迫使我寻找一个用于幻像的数据库解决方案,但在决定解决方案时我又遇到了问题:
谁能引导我找到满意的解决方案?
注意:我几乎已经决定,sqlite
但从幻影连接到它仍然是一个问题.Nodejs提供sqlite3
节点模块,我正在尝试browserify
使用幻像.
注意注意: Browserify没有用!回到地面零!:-(
Thanx提前!
Phantomjs的文件系统API允许您读取和写入二进制文件:
buf = fs.read(FILENAME, 'b') and
fs.write(FILENAME, buf, 'b')
Run Code Online (Sandbox Code Playgroud)
sql.js(https://github.com/kripken/sql.js/)为您提供了一个可以在phantomjs中运行的JavaScript SQLite实现.
结合2,您就拥有了一个快速,持久,可查询的SQL数据库.
示例演练
获取javascript SQLite实现(保存到/tmp/sql.js)
$ wget https://raw.githubusercontent.com/kripken/sql.js/master/js/sql.js -O /tmp/sql.js
使用命令行sqlite3应用程序创建一个测试SQLite数据库(显示它是持久的并且在phantomjs应用程序外部).
sqlite3 /tmp/eg.db
sqlite> CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY AUTOINCREMENT,创建INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP);
sqlite> .quit
保存此测试phantomjs脚本以将条目添加到测试数据库并验证行为.
$ cat /tmp/eg.js
var fs = require('fs'),
sqlite3 = require('./sql.js'),
dbfile = '/tmp/eg.db',
sql = 'INSERT INTO test(id) VALUES (NULL)',
// fs.read returns binary 'string' (not 'String' or 'Uint8Array')
read = fs.read(dbfile, 'b'),
// Database argument must be a 'string' (binary) not 'Uint8Array'
db = new sqlite3.Database(read),
write,
uint8array;
try {
db.run(sql);
} catch (e) {
console.error('ERROR: ' + e);
phantom.exit();
}
// db.export() returns 'Uint8Array' but we must pass binary 'string' to write
uint8array = db.export();
write = String.fromCharCode.apply(null, Array.prototype.slice.apply(uint8array));
fs.write(dbfile, write, 'b');
db.close();
phantom.exit();
Run Code Online (Sandbox Code Playgroud)
运行phantomjs脚本进行测试
$ /usr/local/phantomjs-2.0.0-macosx/bin/phantomjs /tmp/eg.js
使用外部工具验证更改是否持久.
sqlite3 /tmp/eg.db
sqlite> SELECT*FROM test;
id已创建
1 2015-03-28 10:21:09
sqlite的>
要注意的一些事项:
归档时间: |
|
查看次数: |
1309 次 |
最近记录: |