Skip to content

Commit efd98ad

Browse files
committed
support all JSON schemas
1 parent 89e88a5 commit efd98ad

File tree

5 files changed

+83
-180
lines changed

5 files changed

+83
-180
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ language: node_js
22
cache:
33
- node_modules
44
node_js:
5-
- "4.0"
5+
- "4"
6+
- "5"
67
services:
78
- couchdb
89
before_script:

README.md

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,28 @@ var PouchDB = require('pouchdb');
1818
var seed = require('pouchdb-seed-design');
1919
var db = new PouchDB('http://localhost:5984/design');
2020

21-
seed(db, {
22-
person: {
23-
views: {
24-
byFirstName: function (doc) {
25-
emit(doc.firstName);
26-
},
27-
byLastName: function (doc) {
28-
emit(doc.lastName);
29-
},
30-
byFullName: function (doc) {
31-
emit(doc.firstName + ' ' + doc.lastName);
32-
}
33-
}
21+
var ddoc = {
22+
person: {
23+
views: {
24+
byFirstName: function (doc) {
25+
emit(doc.firstName);
26+
},
27+
byLastName: function (doc) {
28+
emit(doc.lastName);
29+
},
30+
byFullName: function (doc) {
31+
emit(doc.firstName + ' ' + doc.lastName);
32+
}
33+
}
34+
}
35+
};
36+
37+
var promise = seed(db, ddoc).then(function(updated) {
38+
if(updated) {
39+
console.log('DDocs updated!');
40+
} else {
41+
console.log('No update was necessary');
3442
}
35-
}, function () {
36-
console.dir(arguments);
3743
});
3844
```
3945

@@ -49,21 +55,15 @@ Creates a set of CouchDB design documents basing on `design` object. Each key in
4955

5056
If no changes between remote design documents and `design` object are detected, no updates are sent to CouchDB.
5157

52-
In addition to invoking the optional callback, seed also returns a [Bluebird Promise](https://github.com/petkaantonov/bluebird/blob/master/API.md).
53-
54-
```js
55-
seed(db, design)
56-
.then(function(results) {
57-
console.log(results);
58-
}, function(err){
59-
console.log(err);
60-
});
61-
```
58+
Seed will return a Promise that fulfills with `false` if no updates were necessary, or the result of the `bulkDocs` operation if changes were pushed. (You will need a `Promise` shim if you are using an older browser or version of Node.)
6259

6360
## Updates
6461

62+
##### (0.3.0) 2016-05-08
63+
You can now use docs with absolutely any JSON schema. All functions in the tree are converted to strings. This will future proof `pouchdb-seed-design` as the design doc standards evolve. Added browser support.
64+
6565
##### (0.2.0) 2015-09-16
66-
Added support for `filters`, `lists`, `shows`, and `validate_doc_update` thanks to [Will Holley](https://github.com/colinskow/pouchdb-seed-design/pull/2). Updated to PouchDB `4.0.X`. Removed Bluebird Promise dependency, preferring native and using [lie](https://github.com/calvinmetcalf/lie) as a fallback.
66+
Added support for `filters`, `lists`, `shows`, and `validate_doc_update` thanks to [Will Holley](https://github.com/colinskow/pouchdb-seed-design/pull/2).
6767

6868
## Credits
6969

index.js

Lines changed: 27 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,37 @@
11
var objmap = require('object-map');
2-
var objfilter = require('object-filter');
32
var objkeysmap = require('object-keys-map');
4-
var objmaptoarr = require('object-map-to-array');
5-
var objsome = require('object-some');
3+
var deepEqual = require('deep-equal');
64

7-
if(typeof Promise !== 'function') {
8-
var Promise = require('lie');
9-
}
5+
var extend = typeof Object.assign === 'function' ? Object.assign : require('node-extend');
106

117
function addDesign(s) {
128
return '_design/' + s;
139
}
1410

1511
function normalizeDoc(doc, id) {
16-
var normalized = {
17-
_id: id || doc._id,
18-
_rev: doc._rev
19-
};
20-
if(doc.views) normalized.views = objmap(doc.views, normalizeView);
21-
if(doc.updates) normalized.updates = objmap(doc.updates, stringify);
22-
if(doc.filters) normalized.filters = objmap(doc.filters, stringify);
23-
if(doc.lists) normalized.lists = objmap(doc.lists, stringify);
24-
if(doc.shows) normalized.shows = objmap(doc.shows, stringify);
25-
if(doc.validate_doc_update) normalized.validate_doc_update = stringify(doc.validate_doc_update);
26-
return normalized;
27-
}
28-
29-
function stringify(obj) {
30-
return obj.toString();
31-
}
32-
33-
function normalizeView(view) {
34-
var r = {};
35-
if (typeof view === 'function' || typeof view === 'string') {
36-
return { map: view.toString() };
37-
}
38-
// Make sure that functions are stringified.
39-
if (view.map) {
40-
r.map = view.map.toString();
41-
}
42-
if (view.reduce) {
43-
r.reduce = view.reduce.toString();
44-
}
45-
return r;
46-
}
4712

48-
function objEqual(a, b) {
49-
// If neither are specified, they are equal
50-
if(!a && !b) {
51-
return true;
52-
}
53-
// if either a or b exist, but one of them is undefined they are not equal
54-
if((a || b) && (!a || !b)) {
55-
return false;
56-
}
57-
// Crawl
58-
return !objsome(a, function (v, k) {
59-
return v !== b[k];
60-
});
61-
}
62-
63-
function viewEqual(a, b) {
64-
if(!a && !b) {
65-
return true;
66-
}
67-
if((a || b) && (!a || !b)) {
68-
return false;
13+
function normalize(doc) {
14+
doc = extend({}, doc);
15+
Object.keys(doc).forEach(function(prop) {
16+
var type = typeof doc[prop];
17+
if(type === 'object') {
18+
doc[prop] = normalize(doc[prop]);
19+
} else if(type === 'function') {
20+
doc[prop] = doc[prop].toString();
21+
}
22+
});
23+
return doc;
6924
}
70-
return b && a.map === b.map && a.reduce === b.reduce;
71-
}
7225

73-
function viewsEqual(a, b) {
74-
// If neither are specified, they are equal
75-
if(!a && !b) {
76-
return true;
77-
}
78-
// if either a or b exist, but one of them is undefined they are not equal
79-
if((a || b) && (!a || !b)) {
80-
return false;
81-
}
82-
return !objsome(a, function (v, k) {
83-
return !viewEqual(v, b[k]);
84-
});
26+
var output = normalize(doc);
27+
output._id = id || doc._id;
28+
output._rev = doc._rev;
29+
return output;
8530
}
8631

8732
function docEqual(local, remote) {
88-
if (!remote) {
89-
return false;
90-
}
91-
92-
return viewsEqual(local.views, remote.views) &&
93-
objEqual(local.updates, remote.updates) &&
94-
objEqual(local.filters, remote.filters) &&
95-
objEqual(local.lists, remote.lists) &&
96-
objEqual(local.shows, remote.shows) &&
97-
local.validate_doc_update === remote.validate_doc_update;
33+
if(!remote) return false;
34+
return deepEqual(local, remote, {strict: true});
9835
}
9936

10037
module.exports = function (db, design, cb) {
@@ -108,24 +45,20 @@ module.exports = function (db, design, cb) {
10845

10946
.then(function (docs) {
11047

111-
var diff;
11248
var remote = {};
113-
var update;
11449

11550
docs.rows.forEach(function (doc) {
11651
if (doc.doc) {
117-
remote[doc.key] = normalizeDoc(doc.doc);
52+
remote[doc.key] = doc.doc;
11853
}
11954
});
12055

121-
update = objmaptoarr(objfilter(local, function (value, key) {
122-
return !docEqual(value, remote[key]);
123-
}), function (v, k) {
124-
if (remote[k]) {
125-
v._rev = remote[k]._rev;
126-
}
127-
128-
return v;
56+
var update = Object.keys(local).filter(function(key) {
57+
if(!remote[key]) return true;
58+
local[key]._rev = remote[key]._rev;
59+
return !docEqual(local[key], remote[key]);
60+
}).map(function(key) {
61+
return local[key];
12962
});
13063

13164
if (update.length > 0) {
@@ -144,6 +77,7 @@ module.exports = function (db, design, cb) {
14477
if(typeof cb === 'function') {
14578
cb(err, null);
14679
}
80+
console.log(err);
14781
return Promise.reject(err);
14882
});
14983

package.json

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
],
1212
"description": "Seed CouchDB's design documents with PouchDB",
1313
"devDependencies": {
14-
"chai": "^3.2.0",
15-
"gulp": "^3.9.0",
16-
"gulp-jshint": "^1.11.2",
17-
"gulp-mocha": "^2.1.3",
18-
"jshint-stylish": "^2.0.1",
19-
"mocha": "^2.2.5",
20-
"pouchdb": "^4.0.3"
14+
"chai": "^3.5.0",
15+
"gulp": "^3.9.1",
16+
"gulp-jshint": "^2.0.0",
17+
"gulp-mocha": "^2.2.0",
18+
"jshint": "^2.9.2",
19+
"jshint-stylish": "^2.2.0",
20+
"mocha": "^2.4.5",
21+
"pouchdb": "^5.3.2"
2122
},
2223
"repository": {
2324
"type": "git",
@@ -27,12 +28,10 @@
2728
"test": "gulp test"
2829
},
2930
"dependencies": {
30-
"lie": "^3.0.1",
31-
"object-filter": "~1.0.1",
31+
"deep-equal": "^1.0.1",
32+
"node-extend": "^0.2.0",
3233
"object-keys-map": "~1.0.1",
33-
"object-map": "0.0.0",
34-
"object-map-to-array": "~1.0.0",
35-
"object-some": "~1.0.0"
34+
"object-map": "0.0.0"
3635
},
3736
"license": "MIT"
3837
}

0 commit comments

Comments
 (0)