{"_id":"sreeix-cradle","_rev":"16-b1a07f0c66815d514ff23eacdaef23b9","name":"sreeix-cradle","description":"the high-level, caching, CouchDB library","dist-tags":{"latest":"0.0.6"},"versions":{"0.0.1":{"name":"sreeix-cradle","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"contributors":[],"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"dependencies":{"vargs":">=0.1.0"},"devDependencies":{"async":"0.1.x","request":"2.2.x","vows":"0.6.x"},"version":"0.0.1","main":"./lib/cradle","directories":{"test":"./test"},"engines":{"node":">=0.3.6"},"_npmUser":{"name":"sreeix","email":"sreeix@gmail.com"},"_id":"sreeix-cradle@0.0.1","_engineSupported":true,"_npmVersion":"1.1.0-alpha-2","_nodeVersion":"v0.6.3","_defaultsLoaded":true,"dist":{"shasum":"288bf35f6743d04a18e92b82eb766be224341926","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.1.tgz","integrity":"sha512-G+RGK2uh86SeGdgUvC/njXtWWEHmj6K9/1+05fRUl75EMwgH/z1UFzq0qeEnh+DcRZoDYDXKdL0jkrYB180vzg==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDyX5ZrtWiBnKpR5+LyUE5wxYz9g64TFRLl3lvxWzjkfwIhAJ+Ro0MiZOMK3jakWxYquJw8u2YiZbWP37xTM8yVmb+3"}]},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]},"0.0.2":{"name":"sreeix-cradle","version":"0.0.2","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"contributors":[{"name":"Charlie Robbins","email":"charlie@nodejitsu.com"},{"name":"Maciej Malecki","email":"maciej@nodejitsu.com"}],"main":"./lib/cradle","dependencies":{"follow":"0.7.x","request":"2.x.x","vargs":"0.1.0"},"devDependencies":{"async":"~0.1.x","vows":"0.6.x"},"scripts":{"test":"node test/helpers/seed.js && vows --spec"},"engines":{"node":">=0.6.0"},"_npmUser":{"name":"sreeix","email":"gabbar@activesphere.com"},"_id":"sreeix-cradle@0.0.2","optionalDependencies":{},"_engineSupported":true,"_npmVersion":"1.1.0-3","_nodeVersion":"v0.8.0","_defaultsLoaded":true,"dist":{"shasum":"c64c8a7004a8dcaaeec65b59fd5b8238bf1c6090","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.2.tgz","integrity":"sha512-wxLfr6SUFqGR5DMpZUx6VJSZphd/h0LFXl1ReDKVOdn201BX0oeZr5Z/hVaIXU7DP6WjWuxhehsSr0uWauoUag==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDrT4DT8dGXzj8YrbQwQ+L/OVDkzxEL6inVQr9j2z+CAAiEAw0pR4JLoLbmSaC0FDBAbMXpwsN/1ka7Z/rli6LdKSV8="}]},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]},"0.0.3":{"name":"sreeix-cradle","version":"0.0.3","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"contributors":[{"name":"Charlie Robbins","email":"charlie@nodejitsu.com"},{"name":"Maciej Malecki","email":"maciej@nodejitsu.com"}],"main":"./lib/cradle","dependencies":{"follow":"0.7.x","request":"2.x.x","vargs":"0.1.0"},"devDependencies":{"async":"~0.1.x","vows":"0.6.x"},"scripts":{"test":"node test/helpers/seed.js && vows --spec"},"engines":{"node":">=0.6.0"},"_npmUser":{"name":"sreeix","email":"gabbar@activesphere.com"},"_id":"sreeix-cradle@0.0.3","optionalDependencies":{},"_engineSupported":true,"_npmVersion":"1.1.0-3","_nodeVersion":"v0.8.0","_defaultsLoaded":true,"dist":{"shasum":"d2b944c0f9d936f74852ea600568f4f24c6d854e","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.3.tgz","integrity":"sha512-NwA4zPlWqS5cVnT6kBblI4k18DlARJcImhDpyh5H1guOz/4yOSTVQxn51hloc60pSdB7zLPFyBFQZ8qqINytHA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIEFKOiF6H+iQpnfB3qI2h1bO/ikfYyR8+U2PMVA3YQKtAiAWKvf6gT0GtZC2QDMXg6hhEQXEU7p0ALn6h5MSnYxYZg=="}]},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]},"0.0.4":{"name":"sreeix-cradle","version":"0.0.4","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"contributors":[{"name":"Charlie Robbins","email":"charlie@nodejitsu.com"},{"name":"Maciej Malecki","email":"maciej@nodejitsu.com"}],"main":"./lib/cradle","dependencies":{"follow":"0.7.x","request":"2.x.x","vargs":"0.1.0"},"devDependencies":{"async":"~0.1.x","vows":"0.6.x"},"scripts":{"test":"node test/helpers/seed.js && vows --spec"},"engines":{"node":">=0.6.0"},"_npmUser":{"name":"sreeix","email":"gabbar@activesphere.com"},"_id":"sreeix-cradle@0.0.4","optionalDependencies":{},"_engineSupported":true,"_npmVersion":"1.1.0-3","_nodeVersion":"v0.8.0","_defaultsLoaded":true,"dist":{"shasum":"a37f99d1cbba51eb503439c847d4067138c920d6","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.4.tgz","integrity":"sha512-nYU0qrG+FwmP8ZrF29Zz8PwR4ODksC5cIs5NUTTQODHPmDTXSz7SjJOyIHkvY4p89rmJiribADC5c3mKp05sQQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC02Iy7PnMVrb3YDZHzWjT0lvVf8neEXH95XCl9EutbXwIgNlDrEuU7Jpsn/Q/5pHRuqRiqIFi2+lPu2K1WAgBMTc8="}]},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]},"0.0.5":{"name":"sreeix-cradle","version":"0.0.5","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"contributors":[{"name":"Charlie Robbins","email":"charlie@nodejitsu.com"},{"name":"Maciej Malecki","email":"maciej@nodejitsu.com"}],"main":"./lib/cradle","dependencies":{"follow":"0.7.x","request":"2.x.x","vargs":"0.1.0"},"devDependencies":{"async":"~0.1.x","vows":"0.6.x"},"scripts":{"test":"node test/helpers/seed.js && vows --spec"},"engines":{"node":">=0.6.0"},"_npmUser":{"name":"sreeix","email":"gabbar@activesphere.com"},"_id":"sreeix-cradle@0.0.5","_engineSupported":true,"_npmVersion":"1.1.0-beta-10","_nodeVersion":"v0.6.7","_defaultsLoaded":true,"dist":{"shasum":"9024a2752cbb5ad3d9cd83c944b2e93224f4ed72","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.5.tgz","integrity":"sha512-WNfyJd19rpnX6UYK3sa/oEW1OQZcThB/5bjMsPT+SZeooqGx68xQvLQ9cao7Ih3OUi4X0/BWuLOuTJI97vRPuQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIDHm+v54lQW0Gdmy7Msa9VhZX6xeUakrYZv6kTKeFYyGAiByM4DHPZ9SsrfPVhxu0u7IOWpmrAkFDjS38QHJFwz+tA=="}]},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]},"0.0.6":{"name":"sreeix-cradle","version":"0.0.6","description":"the high-level, caching, CouchDB library","url":"http://cloudhead.io/cradle","keywords":["couchdb","database","couch"],"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"contributors":[{"name":"Charlie Robbins","email":"charlie@nodejitsu.com"},{"name":"Maciej Malecki","email":"maciej@nodejitsu.com"}],"main":"./lib/cradle","dependencies":{"follow":"0.7.x","request":"2.x.x","vargs":"0.1.0"},"devDependencies":{"async":"~0.1.x","vows":"0.6.x"},"scripts":{"test":"node test/helpers/seed.js && vows --spec"},"engines":{"node":">=0.6.0"},"readme":"cradle\n======\n\nA high-level, caching, CouchDB client for Node.js\n\nintroduction\n------------\n\nCradle is an asynchronous javascript client for [CouchDB](http://couchdb.apache.org).\nIt is somewhat higher-level than most other CouchDB clients, requiring a little less knowledge of CouchDB's REST API.\nCradle also has built-in write-through caching, giving you an extra level of speed, and making document _updates_ and _deletion_ easier.\nCradle was built from the love of CouchDB and Node.js, and tries to make the most out of this wonderful marriage of technologies.\n\nphilosophy\n----------\n\nThe key concept here is the common ground shared by CouchDB and Node.js, that is, _javascript_. The other important aspect of this marriage is the asynchronous behaviors of both these technologies. Cradle tries to make use of these symmetries, whenever it can.\nCradle's API, although closely knit with CouchDB's, isn't overly so. Whenever the API can be abstracted in a friendlier, simpler way, that's the route it takes. So even though a large part of the `Cradle <--> CouchDB` mappings are one to one, some Cradle functions, such as `save()`, can perform more than one operation, depending on how they are used.\n\nsynopsis\n--------\n\n``` js\n  var cradle = require('cradle');\n  var db = new(cradle.Connection)().database('starwars');\n\n  db.get('vader', function (err, doc) {\n      doc.name; // 'Darth Vader'\n      assert.equal(doc.force, 'dark');\n  });\n\n  db.save('skywalker', {\n      force: 'light',\n      name: 'Luke Skywalker'\n  }, function (err, res) {\n      if (err) {\n          // Handle error\n      } else {\n          // Handle success\n      }\n  });\n```\n\ninstallation\n------------\n\n``` bash\n  $ npm install cradle\n```\n\nAPI\n---\n\nCradle's API builds right on top of Node's asynch API. Every asynch method takes a callback as its last argument. The return value is an `event.EventEmitter`, so listeners can also be optionally added.\n\n### Opening a connection ###\n\n``` js\n  new(cradle.Connection)('http://living-room.couch', 5984, {\n      cache: true,\n      raw: false\n  });\n```\n\n_Defaults to `127.0.0.1:5984`_\n\nNote that you can also use `cradle.setup` to set a global configuration:\n\n``` js\n  cradle.setup({\n    host: 'living-room.couch',\n    cache: true, \n    raw: false\n  });\n\n  var c = new(cradle.Connection),\n     cc = new(cradle.Connection)('173.45.66.92');\n```\n\n### creating a database ###\n\n``` js\n  var db = c.database('starwars');\n  db.create();\n```\n\n#### checking for database existence ####\n\nYou can check if a database exists with the `exists()` method.\n\n``` js\n  db.exists(function (err, exists) {\n    if (err) {\n      console.log('error', err);\n    } else if (exists) {\n      console.log('the force is with you.');\n    } else {\n      console.log('database does not exists.');\n      db.create();\n      /* populate design documents */\n    }\n  });\n```\n\n### destroy a database ###\n\n``` js\n  db.destroy(cb);\n```\n\n### fetching a document _(GET)_ ###\n\n``` js\n  db.get('vader', function (err, doc) {\n      console.log(doc);\n  });\n```\n\n> If you want to get a specific revision for that document, you can pass it as the 2nd parameter to `get()`.\n\nCradle is also able to fetch multiple documents if you have a list of ids, just pass an array to `get`:\n\n``` js\n  db.get(['luke', 'vader'], function (err, doc) { ... });\n```\n\n### Querying a view ###\n\n``` js\n  db.view('characters/all', function (err, res) {\n      res.forEach(function (row) {\n          console.log(\"%s is on the %s side of the force.\", row.name, row.force);\n      });\n  });\n```\n\n#### Querying a row with a specific key ####\nLets suppose that you have a design document that you've created:\n\n``` js\n  db.save('_design/user', {\n    views: {\n      byUsername: {\n        map: 'function (doc) { if (doc.resource === 'User') { emit(doc.username, doc) } }'\n      }\n    }\n  });\n```\n\nIn CouchDB you could query this view directly by making an HTTP request to:\n\n```\n  /_design/User/_view/byUsername/?key=\"luke\"\n```\n\nIn `cradle` you can make this same query by using the `.view()` database function: \n\n``` js\n  db.view('user/byUsername', { key: 'luke' }, function (err, doc) {\n      console.dir(doc);\n  });\n```\n\n### creating/updating documents ###\n\nIn general, document creation is done with the `save()` method, while updating is done with `merge()`.\n\n#### creating with an id _(PUT)_ ####\n\n``` js\n  db.save('vader', {\n      name: 'darth', force: 'dark'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\n#### creating without an id _(POST)_ ####\n\n``` js\n  db.save({\n      force: 'dark', name: 'Darth'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\n#### updating an existing document with the revision ####\n\n``` js\n  db.save('luke', '1-94B6F82', {\n      force: 'dark', name: 'Luke'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\nNote that when saving a document this way, CouchDB overwrites the existing document with the new one. If you want to update only certain fields of the document, you have to fetch it first (with `get`), make your changes, then resave the modified document with the above method.\n\nIf you only want to update one or more attributes, and leave the others untouched, you can use the `merge()` method:\n\n``` js\n  db.merge('luke', {jedi: true}, function (err, res) {\n      // Luke is now a jedi,\n      // but remains on the dark side of the force.\n  });\n```\n\nNote that we didn't pass a `_rev`, this only works because we previously saved a full version of 'luke', and the `cache` option is enabled.\n\n#### bulk insertion ####\n\nIf you want to insert more than one document at a time, for performance reasons, you can pass an array to `save()`:\n\n``` js\n  db.save([\n      { name: 'Yoda' },\n      { name: 'Han Solo' },\n      { name: 'Leia' }\n  ], function (err, res) {\n      // Handle response\n  });\n```\n\n#### creating views ####\n\nHere we create a design document named 'characters', with two views: 'all' and 'darkside'.\n\n``` js\n  db.save('_design/characters', {\n      all: {\n          map: function (doc) {\n              if (doc.name) emit(doc.name, doc);\n          }\n      },\n      darkside: {\n          map: function (doc) {\n              if (doc.name && doc.force == 'dark') {\n                  emit(null, doc);\n              }\n          }\n      }\n  });\n```\n\nThese views can later be queried with `db.view('characters/all')`, for example.\n\nHere we create a temporary view. WARNING: do not use this in production as it is\nextremely slow (use it to test views).\n\n``` js\n  db.temporaryView({\n      map: function (doc) {\n        if (doc.color) emit(doc._id, doc);\n      }\n    }, function (err, res) {\n      if (err) console.log(err);\n      console.log(res);\n  });\n```\n\n### creating validation ###\n\nwhen saving a design document, cradle guesses you want to create a view, mention views explicitly to work around this.\n\n``` js\n  db.save('_design/laws', {\n    views: {},\n    validate_doc_update: \n      function (newDoc, oldDoc, usrCtx) {\n        if(! /^(light|dark|neutral)$/(newDoc.force))\n          throw { error: \"invalid value\", reason:\"force must be dark, light, or neutral\" }\n      }\n    }\n  });\n```\n\n### removing documents _(DELETE)_ ###\n\nTo remove a document, you call the `remove()` method, passing the latest document revision.\n\n``` js\n  db.remove('luke', '1-94B6F82', function (err, res) {\n      // Handle response\n  });\n```\n\n\nIf `remove` is called without a revision, and the document was recently fetched from the database, it will attempt to use the cached document's revision, providing caching is enabled.\n\nConnecting with authentication and SSL\n--------------------------------------\n\n``` js\n  var connection = new(cradle.Connection)('https://couch.io', 443, {\n      auth: { username: 'john', password: 'fha82l' }\n  });\n```\n\nor\n\n``` js\n  var connection = new(cradle.Connection)('couch.io', 443, {\n      secure: true,\n      auth: { username: 'john', password: 'fha82l' }\n  });\n```\n\nChanges API\n-----------\n\nFor a one-time `_changes` query, simply call `db.changes` with a callback:\n\n``` js\n  db.changes(function (list) {\n      list.forEach(function (change) { console.log(change) });\n  });\n```\n\nOr if you want to see changes since a specific sequence number:\n\n``` js\n  db.changes({ since: 42 }, function (list) {\n      ...\n  });\n```\n\nThe callback will receive the list of changes as an *Array*. If you want to include\nthe affected documents, simply pass `include_docs: true` in the options.\n\n### Streaming #\n\nYou can also *stream* changes, by calling `db.changes` without the callback. This API uses the **excellent** [follow][0] library from [IrisCouch][1]:\n\n``` js\n  var feed = db.changes({ since: 42 });\n  \n  feed.on('change', function (change) {\n      console.log(change);\n  });\n```\n\nIn this case, it returns an instance of `follow.Feed`, which behaves very similarly to node's `EventEmitter` API. For full documentation on the options available to you when monitoring CouchDB with `.changes()` see the [follow documentation][0].\n\n\nOther API methods\n-----------------\n\n### CouchDB Server level ###\n\n``` js\n  new(cradle.Connection)().*\n```\n\n- `databases()`: Get list of databases\n- `config()`: Get server config\n- `info()`: Get server information\n- `stats()`: Statistics overview\n- `activeTasks()`: Get list of currently active tasks\n- `uuids(count)`: Get _count_ list of UUIDs\n- `replicate(options)`: Replicate a database.\n\n### database level ###\n\n``` js\n  new(cradle.Connection)().database('starwars').*\n```\n\n- `info()`: Database information\n- `all()`: Get all documents\n- `compact()`: Compact database\n- `viewCleanup()`: Cleanup old view data\n- `replicate(target, options)`: Replicate this database to `target`.\n\n[0]: https://github.com/iriscouch/follow\n[1]: http://iriscouch.com\n","_id":"sreeix-cradle@0.0.6","dist":{"shasum":"6ffadcef84e0561a3ea71bb4255611bffdfc8564","tarball":"https://registry.npmjs.org/sreeix-cradle/-/sreeix-cradle-0.0.6.tgz","integrity":"sha512-1ZFvmwrYKCRxC7FObUX11iaGN7nfBoL748/AyBOa4DIzSxHFn7gHF7OozybqTAhz1a8BfMHMo1jNQmxglTOx0A==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCgkb35obzI0CGRlM/5bhzCmGyOU0oo9q2JL0jVy/XLWwIgYe2VpVHw6DYcxzY3rd2k5+EXsKO4iF4+sGEZs4q3Ta8="}]},"_npmVersion":"1.1.59","_npmUser":{"name":"sreeix","email":"gabbar@activesphere.com"},"maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}]}},"readme":"cradle\n======\n\nA high-level, caching, CouchDB client for Node.js\n\nintroduction\n------------\n\nCradle is an asynchronous javascript client for [CouchDB](http://couchdb.apache.org).\nIt is somewhat higher-level than most other CouchDB clients, requiring a little less knowledge of CouchDB's REST API.\nCradle also has built-in write-through caching, giving you an extra level of speed, and making document _updates_ and _deletion_ easier.\nCradle was built from the love of CouchDB and Node.js, and tries to make the most out of this wonderful marriage of technologies.\n\nphilosophy\n----------\n\nThe key concept here is the common ground shared by CouchDB and Node.js, that is, _javascript_. The other important aspect of this marriage is the asynchronous behaviors of both these technologies. Cradle tries to make use of these symmetries, whenever it can.\nCradle's API, although closely knit with CouchDB's, isn't overly so. Whenever the API can be abstracted in a friendlier, simpler way, that's the route it takes. So even though a large part of the `Cradle <--> CouchDB` mappings are one to one, some Cradle functions, such as `save()`, can perform more than one operation, depending on how they are used.\n\nsynopsis\n--------\n\n``` js\n  var cradle = require('cradle');\n  var db = new(cradle.Connection)().database('starwars');\n\n  db.get('vader', function (err, doc) {\n      doc.name; // 'Darth Vader'\n      assert.equal(doc.force, 'dark');\n  });\n\n  db.save('skywalker', {\n      force: 'light',\n      name: 'Luke Skywalker'\n  }, function (err, res) {\n      if (err) {\n          // Handle error\n      } else {\n          // Handle success\n      }\n  });\n```\n\ninstallation\n------------\n\n``` bash\n  $ npm install cradle\n```\n\nAPI\n---\n\nCradle's API builds right on top of Node's asynch API. Every asynch method takes a callback as its last argument. The return value is an `event.EventEmitter`, so listeners can also be optionally added.\n\n### Opening a connection ###\n\n``` js\n  new(cradle.Connection)('http://living-room.couch', 5984, {\n      cache: true,\n      raw: false\n  });\n```\n\n_Defaults to `127.0.0.1:5984`_\n\nNote that you can also use `cradle.setup` to set a global configuration:\n\n``` js\n  cradle.setup({\n    host: 'living-room.couch',\n    cache: true, \n    raw: false\n  });\n\n  var c = new(cradle.Connection),\n     cc = new(cradle.Connection)('173.45.66.92');\n```\n\n### creating a database ###\n\n``` js\n  var db = c.database('starwars');\n  db.create();\n```\n\n#### checking for database existence ####\n\nYou can check if a database exists with the `exists()` method.\n\n``` js\n  db.exists(function (err, exists) {\n    if (err) {\n      console.log('error', err);\n    } else if (exists) {\n      console.log('the force is with you.');\n    } else {\n      console.log('database does not exists.');\n      db.create();\n      /* populate design documents */\n    }\n  });\n```\n\n### destroy a database ###\n\n``` js\n  db.destroy(cb);\n```\n\n### fetching a document _(GET)_ ###\n\n``` js\n  db.get('vader', function (err, doc) {\n      sys.puts(doc);\n  });\n```\n\n> If you want to get a specific revision for that document, you can pass it as the 2nd parameter to `get()`.\n\nCradle is also able to fetch multiple documents if you have a list of ids, just pass an array to `get`:\n\n``` js\n  db.get(['luke', 'vader'], function (err, doc) { ... });\n```\n\n### Querying a view ###\n\n``` js\n  db.view('characters/all', function (err, res) {\n      res.forEach(function (row) {\n          sys.puts(row.name + \" is on the \" +\n                   row.force + \" side of the force.\");\n      });\n  });\n```\n\n#### Querying a row with a specific key ####\nLets suppose that you have a design document that you've created:\n\n``` js\n  db.save('_design/user', {\n    views: {\n      byUsername: {\n        map: 'function (doc) { if (doc.resource === 'User') { emit(doc.username, doc) } }'\n      }\n    }\n  });\n```\n\nIn CouchDB you could query this view directly by making an HTTP request to:\n\n```\n  /_design/User/_view/byUsername/?key=\"luke\"\n```\n\nIn `cradle` you can make this same query by using the `.view()` database function: \n\n``` js\n  db.view('user/byUsername', { key: 'luke' }, function (err, doc) {\n      console.dir(doc);\n  });\n```\n\n### creating/updating documents ###\n\nIn general, document creation is done with the `save()` method, while updating is done with `merge()`.\n\n#### creating with an id _(PUT)_ ####\n\n``` js\n  db.save('vader', {\n      name: 'darth', force: 'dark'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\n#### creating without an id _(POST)_ ####\n\n``` js\n  db.save({\n      force: 'dark', name: 'Darth'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\n#### updating an existing document with the revision ####\n\n``` js\n  db.save('luke', '1-94B6F82', {\n      force: 'dark', name: 'Luke'\n  }, function (err, res) {\n      // Handle response\n  });\n```\n\nNote that when saving a document this way, CouchDB overwrites the existing document with the new one. If you want to update only certain fields of the document, you have to fetch it first (with `get`), make your changes, then resave the modified document with the above method.\n\nIf you only want to update one or more attributes, and leave the others untouched, you can use the `merge()` method:\n\n``` js\n  db.merge('luke', {jedi: true}, function (err, res) {\n      // Luke is now a jedi,\n      // but remains on the dark side of the force.\n  });\n```\n\nNote that we didn't pass a `_rev`, this only works because we previously saved a full version of 'luke', and the `cache` option is enabled.\n\n#### bulk insertion ####\n\nIf you want to insert more than one document at a time, for performance reasons, you can pass an array to `save()`:\n\n``` js\n  db.save([\n      { name: 'Yoda' },\n      { name: 'Han Solo' },\n      { name: 'Leia' }\n  ], function (err, res) {\n      // Handle response\n  });\n```\n\n#### creating views ####\n\nHere we create a design document named 'characters', with two views: 'all' and 'darkside'.\n\n``` js\n  db.save('_design/characters', {\n      all: {\n          map: function (doc) {\n              if (doc.name) emit(doc.name, doc);\n          }\n      },\n      darkside: {\n          map: function (doc) {\n              if (doc.name && doc.force == 'dark') {\n                  emit(null, doc);\n              }\n          }\n      }\n  });\n```\n\nThese views can later be queried with `db.view('characters/all')`, for example.\n\nHere we create a temporary view. WARNING: do not use this in production as it is\nextremely slow (use it to test views).\n\n``` js\n  db.temporaryView({\n      map: function (doc) {\n        if (doc.color) emit(doc._id, doc);\n      }\n    }, function (err, res) {\n      if (err) console.log(err);\n      console.log(res);\n  });\n```\n\n### creating validation ###\n\nwhen saving a design document, cradle guesses you want to create a view, mention views explicitly to work around this.\n\n``` js\n  db.save('_design/laws', {\n    views: {},\n    validate_doc_update: \n      function (newDoc, oldDoc, usrCtx) {\n        if(! /^(light|dark|neutral)$/(newDoc.force))\n          throw { error: \"invalid value\", reason:\"force must be dark, light, or neutral\" }\n      }\n    }\n  });\n```\n\n### removing documents _(DELETE)_ ###\n\nTo remove a document, you call the `remove()` method, passing the latest document revision.\n\n``` js\n  db.remove('luke', '1-94B6F82', function (err, res) {\n      // Handle response\n  });\n```\n\n\nIf `remove` is called without a revision, and the document was recently fetched from the database, it will attempt to use the cached document's revision, providing caching is enabled.\n\nConnecting with authentication and SSL\n--------------------------------------\n\n``` js\n  var connection = new(cradle.Connection)('https://couch.io', 443, {\n      auth: { username: 'john', password: 'fha82l' }\n  });\n```\n\nor\n\n``` js\n  var connection = new(cradle.Connection)('couch.io', 443, {\n      secure: true,\n      auth: { username: 'john', password: 'fha82l' }\n  });\n```\n\nChanges API\n-----------\n\nFor a one-time `_changes` query, simply call `db.changes` with a callback:\n\n``` js\n  db.changes(function (list) {\n      list.forEach(function (change) { console.log(change) });\n  });\n```\n\nOr if you want to see changes since a specific sequence number:\n\n``` js\n  db.changes({ since: 42 }, function (list) {\n      ...\n  });\n```\n\nThe callback will receive the list of changes as an *Array*. If you want to include\nthe affected documents, simply pass `include_docs: true` in the options.\n\n### Streaming #\n\nYou can also *stream* changes, by calling `db.changes` without the callback:\n\n``` js\n  db.changes({ since: 42 }).on('response', function (res) {\n      res.on('data', function (change) {\n          console.log(change);\n      });\n      res.on('end', function () { ... });\n  });\n```\n\nIn this case, it returns an `EventEmitter`, which behaves very similarly to node's `Stream` API.\n\n\nOther API methods\n-----------------\n\n### CouchDB Server level ###\n\n``` js\n  new(cradle.Connection)().*\n```\n\n- `databases()`: Get list of databases\n- `config()`: Get server config\n- `info()`: Get server information\n- `stats()`: Statistics overview\n- `activeTasks()`: Get list of currently active tasks\n- `uuids(count)`: Get _count_ list of UUIDs\n- `replicate(options)`: Replicate a database.\n\n### database level ###\n\n``` js\n  new(cradle.Connection)().database('starwars').*\n```\n\n- `info()`: Database information\n- `all()`: Get all documents\n- `allBySeq()`: Get all documents by sequence\n- `compact()`: Compact database\n- `viewCleanup()`: Cleanup old view data\n- `replicate(target, options)`: Replicate this database to `target`.\n\n","maintainers":[{"name":"sreeix","email":"sreeix@gmail.com"}],"time":{"modified":"2022-06-26T23:24:13.946Z","created":"2011-12-08T12:58:42.145Z","0.0.1":"2011-12-08T12:58:46.196Z","0.0.2":"2012-07-10T16:05:56.656Z","0.0.3":"2012-07-10T16:07:57.807Z","0.0.4":"2012-07-12T12:09:27.084Z","0.0.5":"2012-07-14T19:45:33.842Z","0.0.6":"2012-09-24T21:17:36.915Z"},"author":{"name":"Alexis Sellier","email":"self@cloudhead.net"},"repository":{"type":"git","url":"git://github.com/sreeix/cradle.git"},"users":{"hellboy81":true}}