forked from chris-l/mock-couch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addDoc.js
36 lines (30 loc) · 800 Bytes
/
addDoc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*jslint node: true, indent: 2, nomen : true */
'use strict';
var createMD5 = require('./createMD5'),
R = require('ramda');
module.exports = function (name, doc) {
/**
* Add a document to a database
* @param {string} name - the name of the database
* @param {object} doc - object containing the document
*/
var id = doc._id || createMD5();
delete doc._id;
doc._rev = doc._rev || '1-' + createMD5(JSON.stringify(doc));
this.databases[name][id] = doc;
// Don't emit changes for _local documents
if (id.indexOf('_local') !== 0) {
this.changes[name].push({
seq : this.sequence[name],
id : id,
changes : [
{ rev : doc._rev }
],
doc : R.cloneDeep(doc)
});
}
return {
id : id,
_rev : doc._rev
};
};