UNPKG

1.73 kBJavaScriptView Raw
1// Generated by CoffeeScript 2.3.1
2(function() {
3 var apply_cmd, semantic_for;
4
5 semantic_for = function(db, fromJS) {
6 return {
7 create: function(msg) {
8 var doc;
9 doc = msg.get('doc').set('_id', msg.get('id'));
10 return db.put(doc.toJS()).catch(function() {
11 return true;
12 });
13 },
14 update: function(msg) {
15 var id, msg_doc, operations, rev;
16 id = msg.get('id');
17 rev = msg.get('rev');
18 msg_doc = msg.get('doc');
19 operations = msg.get('operations');
20 return db.get(id, {rev}).then(function(doc) {
21 var old_doc;
22 doc = fromJS(doc);
23 old_doc = doc;
24 if (msg_doc != null) {
25 // The entries in the `.doc` field are interpreted as simple `set` operations.
26 doc = doc.merge(msg_doc);
27 }
28 if (operations != null) {
29 // The entries in the `.operations` field are executed by `json_path`.
30 // Notice that these might fail, in which case the update will not be saved.
31 doc = operations.reduce(apply_cmd, doc);
32 }
33 // Do not modify the database if the document was not modified.
34 if (doc.equals(old_doc)) {
35 return;
36 }
37 return db.put(doc.toJS());
38 }).catch(function() {
39 return true;
40 });
41 },
42 delete: function(msg) {
43 var id, rev;
44 id = msg.get('id');
45 rev = msg.get('rev');
46 return db.delete({
47 _id: id,
48 _rev: rev
49 }).catch(function() {
50 return true;
51 });
52 }
53 };
54 };
55
56 module.exports = semantic_for;
57
58 apply_cmd = require('./apply-command');
59
60}).call(this);