1
-
1
+ var _ = require ( 'lodash' )
2
2
module . exports = function ( backend ) {
3
3
backend . hook = hook . bind ( backend ) ;
4
4
backend . onQuery = onQuery . bind ( backend ) ;
@@ -26,11 +26,33 @@ function onQuery(collectionName, cb) {
26
26
function hook ( method , pattern , fn ) {
27
27
var backend = this ;
28
28
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
+
29
42
backend . use ( 'after submit' , function ( shareRequest , next ) {
30
43
var collectionName , firstDot , fullPath , matches , regExp , relPath , segments , op ;
31
44
32
45
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 ;
33
50
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
+ }
34
56
if ( opData . del || opData . create ) {
35
57
collectionName = pattern ;
36
58
if ( collectionName !== shareRequest . collection ) return next ( ) ;
@@ -44,11 +66,6 @@ function hook(method, pattern, fn) {
44
66
}
45
67
}
46
68
47
- var snapshot = shareRequest . snapshot ;
48
- var docName = shareRequest . docName || shareRequest . id ;
49
- var backend = shareRequest . backend ;
50
- var session = shareRequest . agent . connectSession ;
51
-
52
69
switch ( method ) {
53
70
case 'del' :
54
71
if ( ! opData . del ) return next ( ) ;
0 commit comments