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.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章