Skip to content

Commit c4901ba

Browse files
committed
add update event
1 parent e81e009 commit c4901ba

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

index.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
var _ = require('lodash')
22
module.exports = function(backend) {
33
backend.hook = hook.bind(backend);
44
backend.onQuery = onQuery.bind(backend);
@@ -26,11 +26,33 @@ function onQuery(collectionName, cb) {
2626
function hook(method, pattern, fn) {
2727
var backend = this;
2828

29+
backend.use('apply', function(shareRequest, done) {
30+
const stream = shareRequest.agent.stream || {}
31+
32+
const opData = shareRequest.op
33+
const snapshot = shareRequest.snapshot
34+
35+
if (!opData.create && !opData.del && !shareRequest.originalSnapshot){
36+
shareRequest.originalSnapshot = _.cloneDeep(snapshot)
37+
}
38+
39+
done()
40+
})
41+
2942
backend.use('after submit', function (shareRequest, next) {
3043
var collectionName, firstDot, fullPath, matches, regExp, relPath, segments, op;
3144

3245
var opData = shareRequest.opData || shareRequest.op;
46+
var snapshot = shareRequest.snapshot;
47+
var docName = shareRequest.docName || shareRequest.id;
48+
var backend = shareRequest.backend;
49+
var session = shareRequest.agent.connectSession;
3350

51+
if (method === 'update') {
52+
if (shareRequest.collection !== pattern) return next()
53+
fn(docName, snapshot.data, shareRequest.originalSnapshot && shareRequest.originalSnapshot.data, session, backend);
54+
return next()
55+
}
3456
if (opData.del || opData.create) {
3557
collectionName = pattern;
3658
if (collectionName !== shareRequest.collection) return next();
@@ -44,11 +66,6 @@ function hook(method, pattern, fn) {
4466
}
4567
}
4668

47-
var snapshot = shareRequest.snapshot;
48-
var docName = shareRequest.docName || shareRequest.id;
49-
var backend = shareRequest.backend;
50-
var session = shareRequest.agent.connectSession;
51-
5269
switch (method) {
5370
case 'del':
5471
if (!opData.del) return next();

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,8 @@
2121
"bugs": {
2222
"url": "git://github.com/zag2art/derby-hook/issues"
2323
},
24-
"license": "MIT"
24+
"license": "MIT",
25+
"dependencies": {
26+
"lodash": "^4.17.10"
27+
}
2528
}

0 commit comments

Comments
 (0)