livelycouch初步认识

 

  LivelyCouch是为了使开发CouchDB更简单化而生成的.众所周知,CouchDB是通过HTTP对其数据进行操作,而LivelyCouch就是利用了node.js的HTTP对CouchDB进行操作.他对couchDB的切入点就是1.1.0以后加入的httpproxy模块,此模块也可以说是couchDB给自己做了一个扩展,对外来说是訪問couchdb,然而是訪問指定的url.

  LivelyCouch主要有两个部分,

  1、LivelyHandle,在这里几乎可以实现对couchdb所有的操作,还可以对信息进行处理.例:

 

exports.run = function (parameters, response, handlerLib) {
  var client = handlerLib.couchdb.createClient(5984, '127.0.0.1', 'user', 'password');
  var userDb = client.db('people');
  var hairColour = parameters.query.haircolour;
  var startAge = parameters.query.startage;
  var endAge = parameters.query.endage;
  
  // calling a view to get all people between 30 and 40
  userDb.view('design_doc', 'people_per_age', {startkey=startAge, endkey=endAge}, function (data) {
    // filtering all rows that have the specified hair colour
    var filteredRows = data.rows.filter(function (row) {
    return row.value.haircolor == hairColour;
  });

  // writing out all filtered rows to the response:
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.write(JSON.stringify(filteredRows));
  response.end();
  })
}
 

 

  如果想通过http去执行上段代碼,则需要把上面文档以附件形式上传到lively_handler的一个文档中,文档格式如下:

    {

  "id": "your_app",
  "_attachments": [
    "filtered_people.js": {...},
    "another_handler.js": {...}
  ],
  "mapping": {
    "/filtered_people": "filtered_people.js",
    "/another_path": "another_handler.js"
  }
}
 

   通过http://127.0.0.1/_node/your_app/filered_people?haircolour=blond&startage=30&endage=40访問.請确保people数据庫中有design_doc/people_per_age,和在LivelyCouch的handlers-deployed有上传的js文件.路径:your_path/LivelyCouch/

handler_deployed/your_app/filered_people.js.

2、Lively Event System

主要还是当触发某个事件的時候做一些事情,比如文件更新,則给讀者发送一封邮件,Lively Event也是通过http去触发,实现方法如下,在lively_event中加入文档:

{
  "_id": "email_event",
  "trigger": [{
    "path": "/send_email"
  }],
  "workers": [{
    "name": "send_email",
    "parameters": {
      "recipient": "support@yourcompany.com"
    }
  }]
}
 

 path是触发路径,workers是执行的事情,work也是存儲在lively_work的数据库中,格式如下:

 

{  "_id": "send_email",
  "delegate": "sendmail.js",
  "_attachments": {
    "sendmail.js": {...},
    "someOtherScript.js": {...}
  }
}
 delegate是执行附件中的哪个js,当worker启动后上面传的recipient会用stdin以http传给他.
===========================================================================================
对于LivelyCouch我很看好,但就目前而言一些关键的問題还没有解决,如文档同步.期待正式版发布.
更詳細内容参见:http://livelycouch.org.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值