在Node.js应用:安装使用RethinkDB(Ubuntu)
存储数据是web应用最常用的任务之一。在很长的一段时间里,web应用都依赖于关系型数据库。例如PHP和MySQL,但是并不是所有web应用都必须使用关系型数据库。在这篇文章里我们介绍怎么在Node.js应用中使用RethinkDB-开源的JSON数据库。
RethinkDB的官方介绍:
“RethinkDB is the first open-source, scalable JSON database built from the ground up for the realtime web. It inverts the traditional database architecture by exposing an exciting new access model – instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results to applications in realtime.”
安装Rethinkdb
为了在Ubuntu上安装rethinkdb,我们需要先添加源:
$ source /etc/lsb-release && echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/apt/sources.list.d/rethinkdb.list
$ wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get install rethinkdb
rethinkdb
如果没有问题,会看到类似如下输出:
Listening for intracluster connections on port 29015
Listening for client driver connections on port 28015
Listening for administrative HTTP connections on port 8080
Listening on addresses: 127.0.0.1, 127.0.1.1, ::1
To fully expose RethinkDB on the network, bind to all addresses by running rethinkdb with the `--bind all` command line option.
rethinkdb服务启动并监听28015端口。http://your_server_ip:8080是web管理接口。
安装Node.js Rethinkdbdash客户端
项目地址:https://github.com/neumino/rethinkdbdash
我们使用Rethinkdbash和数据库进行交互,它是Node.js应用和rethinkdbash的桥梁。
安装rethinkdbash:
npm install rethinkdbdash
开始使用RethinkDB
下面我们创建一个示例应用demo,它使用一个名叫Rethink_DB的数据库。首先我们创建一个项目目录demo,在目录里创建一个文件app.js:
mkdir demo
cd demo
touch app.js
编辑app.js:
var r = require('rethinkdbdash')({ // 导入rethinkdbash客户端模块
port: 28015, // rethinkdb端口
host: 'localhost' // rethinkdb主机
});
创建Rethink_DB数据库
r.dbCreate('Rethink_DB')
.run()
.then(function(response){
console.log(response);
})
.error(function(err){
console.log('error ', err);
});
执行,如果成功会输出如下信息:
$ nodejs app.js
Creating a pool connected to localhost:28015
{ config_changes: [ { new_val: [Object], old_val: null } ],
dbs_created: 1 }
进入RethinkDB web管理接口 http:your_server_ip:8080,我们可以看到已经创建了一个叫Rethink_DB的数据库:
创建表
var r = require('rethinkdbdash')({
port: 28015,
host: 'localhost',
db: 'Rethink_DB'
});
// 创建表,主键为name。如果不指定主键,主键默认为id
r.tableCreate('Student', { primaryKey: 'name' }) //r.tableCreate('Student')
.run()
.then(function(response){
console.log(response);
})
.error(function(err){
console.log('error while creating table ', err);
})
插入数据
r.table("Student")
.insert({
name: "LittleMing",
addr: "China"
})
.run()
.then(function(response){
console.log('Success ',response);
})
.error(function(err){
console.log('error occurred ',err);
})
################################
$ nodejs app.js
Creating a pool connected to localhost:28015
Success { deleted: 0,
errors: 0,
inserted: 1,
replaced: 0,
skipped: 0,
unchanged: 0 }
SELECT数据
r.table('Student')
.run()
.then(function(response){
console.log(response);
})
.error(function(err){
console.log(err);
})
[ { addr: 'China', name: 'LittleMing' } ]
根据主键查询
r.table('Student')
.get('LittleMing')
.run()
.then(function(response){
console.log(response);
})
.error(function(err){
console.log(err);
})
{ addr: 'China', name: 'LittleMing' }
实时push更新
引用官网描述:
“Instead of polling for changes, the developer can tell RethinkDB to continuously push updated query results in realtime.”
r.table('Student')
.changes()
.run()
.then(function(cursor){
cursor.each(console.log);
})
.error(function(err){
console.log(err);
});
现在我们再往数据中插入输入,输出类似如下:
{
new_val: {
addr: 'hell',
name: 'BigMing'
},
old_val: null
}
上面是一个插入语句的log。old_val是null,new_val有值,这是为了给以存在记录的更新而设的,把旧值放入到old_val中。
TAG:
除非注明,文章均由 Linux系统学习 整理发布,欢迎转载。如有版权争议,请联系删除。
转载请注明本文地址:http://8u.hn.cn/shujuku/12784.html,网络整理资料,转载时请保留原出处。
留言与评论(共有 0 条评论) |
文章评论