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": "[email protected]"
    }
  }]
}
 

 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.

 

你可能感兴趣的:(CouchDB,json)