feat(examples) (#11)

* feat(getting-started)

* feat(getting-started): destroy doc

* doc(getting-started)

* feat(examples)
This commit is contained in:
Geoff Cox 2017-07-18 21:45:35 -07:00 committed by GitHub
parent f830ee2ba5
commit deae431cf0
11 changed files with 235 additions and 3 deletions

View file

@ -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)

32
examples/all-throttle.js Normal file
View file

@ -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
});
});

34
examples/all.js Normal file
View file

@ -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
});
});

6
examples/changes.js Normal file
View file

@ -0,0 +1,6 @@
'use strict';
var Slouch = require('../');
var slouch = new Slouch('http://admin:admin@localhost:5984');
// TODO

View file

@ -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
});

View file

@ -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
});

View file

@ -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
});

View file

@ -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
});

6
examples/updates.js Normal file
View file

@ -0,0 +1,6 @@
'use strict';
var Slouch = require('../');
var slouch = new Slouch('http://admin:admin@localhost:5984');
// TODO

26
examples/upsert.js Normal file
View file

@ -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
});

6
examples/view.js Normal file
View file

@ -0,0 +1,6 @@
'use strict';
var Slouch = require('../');
var slouch = new Slouch('http://admin:admin@localhost:5984');
// TODO