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