From deae431cf04a85e617409f17b5b6af201621fbfe Mon Sep 17 00:00:00 2001 From: Geoff Cox Date: Tue, 18 Jul 2017 21:45:35 -0700 Subject: [PATCH] feat(examples) (#11) * feat(getting-started) * feat(getting-started): destroy doc * doc(getting-started) * feat(examples) --- README.md | 8 +++++--- examples/all-throttle.js | 32 +++++++++++++++++++++++++++++++ examples/all.js | 34 +++++++++++++++++++++++++++++++++ examples/changes.js | 6 ++++++ examples/create-or-update.js | 26 +++++++++++++++++++++++++ examples/get-merge-upsert.js | 26 +++++++++++++++++++++++++ examples/get-modify-upsert.js | 32 +++++++++++++++++++++++++++++++ examples/getting-started.js | 36 +++++++++++++++++++++++++++++++++++ examples/updates.js | 6 ++++++ examples/upsert.js | 26 +++++++++++++++++++++++++ examples/view.js | 6 ++++++ 11 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 examples/all-throttle.js create mode 100644 examples/all.js create mode 100644 examples/changes.js create mode 100644 examples/create-or-update.js create mode 100644 examples/get-merge-upsert.js create mode 100644 examples/get-modify-upsert.js create mode 100644 examples/getting-started.js create mode 100644 examples/updates.js create mode 100644 examples/upsert.js create mode 100644 examples/view.js diff --git a/README.md b/README.md index 1543c25..59f4dd3 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ A JS API for CouchDB that does the heavy lifting -## Slouch is a good alternative to nano as: +## Slouch is a good alternative to nano: - You don't have to create an instance for each DB - Supports native promises @@ -18,6 +18,8 @@ A JS API for CouchDB that does the heavy lifting - Designed for both CouchDB 1 and CouchDB 2 -## Install +## Table of Contents - $ npm install couch-slouch +* [Getting Started](https://github.com/redgeoff/slouch/wiki/Getting-Started) +* [Don't just relax. Slouch!](https://github.com/redgeoff/slouch/wiki/Don%27t-just-relax.-Slouch%21) +* [Reference](https://github.com/redgeoff/slouch/wiki#table-of-contents) diff --git a/examples/all-throttle.js b/examples/all-throttle.js new file mode 100644 index 0000000..63ae1aa --- /dev/null +++ b/examples/all-throttle.js @@ -0,0 +1,32 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +var Throttler = require('squadron').Throttler; +var throttler = new Throttler(5); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { foo: 'bar' }); + +}).then(function () { + + // Create another doc + return slouch.doc.create('mydb', { foo: 'nar' }); + +}).then(function () { + + return slouch.doc.all('mydb', { include_docs: true }).each(function (item) { + + return Promise.resolve('foo => ' + item.foo); + + }, throttler).then(function () { + + // Done iterating through all docs + + }); + +}); diff --git a/examples/all.js b/examples/all.js new file mode 100644 index 0000000..540eca1 --- /dev/null +++ b/examples/all.js @@ -0,0 +1,34 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { foo: 'bar' }); + +}).then(function () { + + // Create another doc + return slouch.doc.create('mydb', { foo: 'nar' }); + +}).then(function () { + + return slouch.doc.all('mydb', { include_docs: true }).each(function (item) { + + // If we return a promise then the all() iterator won't move on to the next item until the + // promise resolves. This allows us to process a iterate through a large number of docs without + // consuming a lot of memory loading all the docs into memory. It also allows us to control the + // flow of the items and process them sequentially so that we don't end up thrashing the + // processor with concurrent processes. + return Promise.resolve('foo => ' + item.foo); + + }).then(function () { + + // Done iterating through all docs + + }); + +}); diff --git a/examples/changes.js b/examples/changes.js new file mode 100644 index 0000000..8a239dd --- /dev/null +++ b/examples/changes.js @@ -0,0 +1,6 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// TODO diff --git a/examples/create-or-update.js b/examples/create-or-update.js new file mode 100644 index 0000000..9a9924d --- /dev/null +++ b/examples/create-or-update.js @@ -0,0 +1,26 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.createOrUpdate('mydb', { _id: '1', foo: 'bar' }); + +}).then(function (doc) { + + // Update the doc + return slouch.doc.createOrUpdate('mydb', { _id: '1', foo: 'yar' }); + +}).then(function () { + + // Destroy the database + return slouch.db.destroy('mydb'); + +}).then(function () { + + // Database was destroyed + +}); diff --git a/examples/get-merge-upsert.js b/examples/get-merge-upsert.js new file mode 100644 index 0000000..4688e5d --- /dev/null +++ b/examples/get-merge-upsert.js @@ -0,0 +1,26 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { _id: '1', foo: 'bar' }); + +}).then(function (doc) { + + // Add the `yar` attr to the doc and ignore any conflicts + return slouch.doc.getMergeUpsert('mydb', { _id: '1', yar: 'nar' }); + +}).then(function () { + + // Destroy the database + return slouch.db.destroy('mydb'); + +}).then(function () { + + // Database was destroyed + +}); diff --git a/examples/get-modify-upsert.js b/examples/get-modify-upsert.js new file mode 100644 index 0000000..074a84c --- /dev/null +++ b/examples/get-modify-upsert.js @@ -0,0 +1,32 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { _id: '1', foo: 'bar' }); + +}).then(function (doc) { + + // Add the `yar` attr to the doc via a callback and ignore any conflicts + return slouch.doc.getModifyUpsert('mydb', '1', function (doc) { + + doc.yar = (new Date()).getTime(); + + return doc; + + }); + +}).then(function () { + + // Destroy the database + return slouch.db.destroy('mydb'); + +}).then(function () { + + // Database was destroyed + +}); diff --git a/examples/getting-started.js b/examples/getting-started.js new file mode 100644 index 0000000..fb63f29 --- /dev/null +++ b/examples/getting-started.js @@ -0,0 +1,36 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { foo: 'bar' }); + +}).then(function (doc) { + + // Update the doc + return slouch.doc.update('mydb', { _id: doc.id, _rev: doc.rev, foo: 'yar' }); + +}).then(function (doc) { + + // Get the doc + return slouch.doc.get('mydb', doc._id); + +}).then(function (doc) { + + // Destroy the doc + return slouch.doc.destroy('mydb', doc._id, doc._rev); + +}).then(function () { + + // Destroy the database + return slouch.db.destroy('mydb'); + +}).then(function () { + + // Database was destroyed + +}); diff --git a/examples/updates.js b/examples/updates.js new file mode 100644 index 0000000..8a239dd --- /dev/null +++ b/examples/updates.js @@ -0,0 +1,6 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// TODO diff --git a/examples/upsert.js b/examples/upsert.js new file mode 100644 index 0000000..fa692ee --- /dev/null +++ b/examples/upsert.js @@ -0,0 +1,26 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// Create the database +slouch.db.create('mydb').then(function () { + + // Create a doc + return slouch.doc.create('mydb', { _id: '1', foo: 'bar' }); + +}).then(function (doc) { + + // Update the doc + return slouch.doc.upsert('mydb', { _id: '1', foo: 'yar' }); + +}).then(function () { + + // Destroy the database + return slouch.db.destroy('mydb'); + +}).then(function () { + + // Database was destroyed + +}); diff --git a/examples/view.js b/examples/view.js new file mode 100644 index 0000000..8a239dd --- /dev/null +++ b/examples/view.js @@ -0,0 +1,6 @@ +'use strict'; + +var Slouch = require('../'); +var slouch = new Slouch('http://admin:admin@localhost:5984'); + +// TODO