{"name":"pull-level","description":"","version":"1.0.0","homepage":"https://github.com/dominictarr/pull-level","repository":{"type":"git","url":"git://github.com/dominictarr/pull-level.git"},"dependencies":{"level-fix-range":"~1.1"},"devDependencies":{"monotonic-timestamp":"0.0.8","levelup":"git://github.com/dominictarr/node-levelup.git#installable-reverse"},"scripts":{"test":"set -e; for t in test/*.js; do node $t; done"},"author":{"name":"Dominic Tarr","email":"dominic.tarr@gmail.com","url":"http://dominictarr.com"},"license":"MIT","_id":"pull-level@1.0.0","dist":{"shasum":"46a5d7972555ff03f3423faaafaf0b6a8a005506","tarball":"http://registry-npm.ywwl.com/pull-level/download/pull-level-1.0.0.tgz","integrity":"sha512-tBQ9HHT9kd9KnYK3uLqFDUENiYtL0pcLcr3RaYeIaO1aQ6bqtirLrdXIqY1rqw8E8LysKZtJztkSQ+qYM9BnbA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDwPZ3aYA4faCAztsAzQ/4er5bXSUEn+R42/gTo3ClhWAIhAPRTCNn527AuRuuNUVlUjK2MacHbjsL77KeS3doQctsb"}],"size":3100,"noattachment":false,"key":"/pull-level/-/pull-level-1.0.0.tgz"},"_from":".","_npmVersion":"1.2.3","_npmUser":{"name":"dominictarr","email":"dominic.tarr@gmail.com"},"maintainers":[{"name":"dominictarr","email":"dominic.tarr@gmail.com"}],"directories":{},"readme":"# pull-level\n\n[pull-stream](https://github.com/dominictarr/pull-stream) interface to\n[levelup](https://github.com/rvagg/node-levelup)\n\n## Example - reading\n\nread items in database.\n\n``` js\nvar pl = require('pull-level')\nvar pull = require('pull-stream')\n\nvar db = require('levelup')('/tmp/pull-level-example')\n\npull(pl.read(db), pull.collect(console.log))\n```\n\nread items in database, plus realtime changes\n\n``` js\npull(\n  pl.read(db, {live: true}),\n  //log data as it comes,\n  //because tail will keep the connection open\n  //so we'll never see the end otherwise.\n  pull.through(console.log),\n  //note, pull-streams will not drain unless something is\n  //pulling the data through, so we have to add drain\n  //even though the data we want is coming from pull.through()\n  pull.drain()\n)\n```\n\nIf you just want the realtime inserts,\nuse `live`\n\n``` js\npull(\n  pl.live(db, {live: true}),\n  pull.through(console.log),\n  pull.drain()\n)\n```\n\n## Example - writing\n\nTo write, pipe batch changes into `write`\n\n``` js\npull(\n  pull.values([\n    {key: 0, value: 'zero', type: 'put'},\n    {key: 1, value: 'one',  type: 'put'},\n    {key: 2, value: 'two',  type: 'put'},\n  ]),\n  pl.write(db)\n)\n```\n\nIf you are lazy/busy, you can leave off `type`.\nIn that case, if `value` is non-null, the change\nis considered a `put` else, a `del`.\n\n``` js\npull(\n  pull.values([\n    {key: 0, value: 'zero'},\n    {key: 1, value: 'one'},\n    {key: 2, value: 'two'},\n  ]), \n  pl.write(db)\n)\n```\n\n\n## Example - indexes!\n\nWith pull-level it's easy to create indexes.\njust save a pointer to the key.\n\nlike this:\n``` js\npull(\n  pull.values([\n    {key: key, value: VALUE, type: 'put'},\n    {key: '~INDEX~' + VALUE.prop, value: key,  type: 'put'},\n  ]),\n  pl.write(db)\n)\n```\n\nthen, when you want to do a `read`, use `asyncMap`\n\n``` js\npull(\n  pl.read(db, {min: '~INDEX~', max: '~INDEX~~'})\n  pull.asyncMap(function (e, cb) {\n    db.get(e.value, function (value) {\n      cb(null, {key: e.value, value: value})\n    })\n  }),\n  pull.collect(console.log)\n)\n```\n\n## Example realtime aggregation\n\nWe want to keep a realtime count of everything in the database.\nWhen ever something is inserted, we increment. But, we need\nto check the records that are *currently* in the database.\n\nSince it takes some time to scan the database, we need to make sure\nwe have done that before giving an answer. We can read it all with\none stream, using `{sync: true}` to be notified of when we have read out all the old records.\n\nFirst all the old records are read from the non-live stream,\nthen you get one `{sync: true}` element, then all the new item.\n\n``` js\nvar sum = 0, ready = false, waiting = []\n\n//call get count to know s\nfunction getSum (cb) {\n  if(!ready) waiting.push(cb)\n  else cb(null, sum)\n}\n\npull(\n  pl.read(db, {sync: true}),\n  pull.drain(function (op) {\n    if(op.sync) {\n      //if we see a data element with this it means\n      ready = true\n      while(waiting.length) waiting.shift()(null, count)\n    }\n    //increment our counter!\n    if(Number.isFinite(+op.value.amount)) //filter out non numbers & NaN.\n      sum += op.value.amount\n  })\n)\n\n```\n\n## License\n\nMIT\n","publish_time":1365077129106,"_cnpm_publish_time":1365077129106,"dist-tags":{"latest":"2.0.4"}}