|
1 | | -# couchbase-promises |
2 | | -A lightweight A+ Promises wrapper for the Couchbase SDK. |
| 1 | +# Couchbase Promises |
| 2 | +A lightweight, drop-in replacement for the Couchnode module with added support for A+ Promises. |
| 3 | + |
| 4 | +[](https://travis-ci.org/dsfields/couchbase-promises) |
| 5 | + |
| 6 | +## Overview |
| 7 | +Just like the [Couchbase Node.js module](http://developer.couchbase.com/documentation/server/4.0/sdks/node-2.0/introduction.html), but with the addition of `*Async()` methods that return A+ Promises for all methods that contain a Node.js callback parameter. Both the normal Couchnode and the mock Couchnode APIs have been fully promisified. |
| 8 | + |
| 9 | +The current version supports Couchbase Node.js SDK version 2.1.2. |
| 10 | + |
| 11 | +Promises are created using the [Bluebird](http://bluebirdjs.com/docs/getting-started.html) Promises library. |
| 12 | + |
| 13 | +## General Usage |
| 14 | +Usage is almost exactly the same as the native SDK, but with the added ability to use Promises instead of callbacks. |
| 15 | + |
| 16 | +A user repository module with a simple lookup... |
| 17 | + |
| 18 | +```js |
| 19 | +var couchbase = require('couchbase-promises'); |
| 20 | +var cluster = new couchbase.Cluster('couchbase://127.0.0.1'); |
| 21 | +var bucket = cluster.openBucket(); |
| 22 | + |
| 23 | +function UserNotFoundError() { |
| 24 | + Error.call(this); |
| 25 | + Error.captureStackTrace(this, UserNotFoundError); |
| 26 | + this.message = "User not found."; |
| 27 | +} |
| 28 | + |
| 29 | +module.exports = { |
| 30 | + UserNotFoundError: UserNotFoundError, |
| 31 | + getUserAsync: function(userId) { |
| 32 | + return bucket.getAsync(userId) |
| 33 | + .then(function(result) { |
| 34 | + return { |
| 35 | + user: result.value, |
| 36 | + meta: { |
| 37 | + etag: result.cas |
| 38 | + } |
| 39 | + }; |
| 40 | + }).catch(couchbase.Error, function(e) { |
| 41 | + if (e.code == couchbase.errors.keyNotFound) |
| 42 | + throw new UserNotFoundError(); |
| 43 | + |
| 44 | + throw e; |
| 45 | + }); |
| 46 | + } |
| 47 | +}; |
| 48 | +``` |
0 commit comments