Skip to content

Commit 65ca147

Browse files
committed
initial commit
0 parents  commit 65ca147

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/.idea/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## Derby-hook

index.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
2+
module.exports = function(store) {
3+
4+
store.hook = function(method, pattern, fn) {
5+
return store.shareClient.use('after submit', function(shareRequest, next) {
6+
var backend, collectionName, docName, firstDot, fullPath, matches, op, opData, regExp, relPath, segments, session, snapshot, _i, _len, _ref;
7+
opData = shareRequest.opData;
8+
if (opData.del || opData.create) {
9+
collectionName = pattern;
10+
if (collectionName !== shareRequest.collection) {
11+
return next();
12+
}
13+
} else {
14+
firstDot = pattern.indexOf('.');
15+
if (firstDot === -1) {
16+
if (!patternToRegExp(pattern).test(collectionName)) {
17+
return next();
18+
}
19+
} else {
20+
collectionName = pattern.slice(0, firstDot);
21+
if (collectionName !== shareRequest.collection) {
22+
return next();
23+
}
24+
}
25+
}
26+
snapshot = shareRequest.snapshot, docName = shareRequest.docName, backend = shareRequest.backend;
27+
session = shareRequest.agent.connectSession;
28+
switch (method) {
29+
case 'del':
30+
if (!opData.del) {
31+
return next();
32+
}
33+
fn(docName, shareRequest.prev.data, session, backend);
34+
break;
35+
case 'create':
36+
if (!opData.create) {
37+
return next();
38+
}
39+
fn(docName, shareRequest.snapshot.data, session, backend);
40+
break;
41+
case 'change':
42+
_ref = opData.op;
43+
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
44+
op = _ref[_i];
45+
segments = op.p;
46+
if (op.si || op.sd) {
47+
segments = segments.slice(0, -1);
48+
}
49+
relPath = segments.join('.');
50+
fullPath = collectionName + '.' + docName + '.' + relPath;
51+
regExp = patternToRegExp(pattern);
52+
matches = regExp.exec(fullPath);
53+
if (matches) {
54+
fn.apply(null, __slice.call(matches.slice(1)).concat([lookup(segments, snapshot.data)], [op], [session], [backend]));
55+
}
56+
}
57+
}
58+
return next();
59+
});
60+
61+
};
62+
63+
store.onQuery = function(collectionName, source, cb) {
64+
return this.shareClient.use('query', function(shareRequest, next) {
65+
var session;
66+
if (shareRequest.options.backend !== source) {
67+
return next();
68+
}
69+
session = shareRequest.agent.connectSession;
70+
shareRequest.query = deepCopy(shareRequest.query);
71+
if (collectionName === '*') {
72+
return cb(shareRequest.collection, shareRequest.query, session, next);
73+
} else {
74+
return cb(shareRequest.query, session, next);
75+
}
76+
});
77+
};
78+
return true;
79+
};
80+
81+
82+
function patternToRegExp(pattern) {
83+
var end;
84+
end = pattern.slice(pattern.length - 2, pattern.length);
85+
if (end === '**') {
86+
pattern = pattern.slice(0, pattern.length - 2);
87+
} else {
88+
end = '';
89+
}
90+
pattern = pattern.replace(/\./g, "\\.").replace(/\*/g, "([^.]*)");
91+
return new RegExp(pattern + (end ? '.*' : '$'));
92+
};
93+
94+
function lookup(segments, doc) {
95+
var curr, part, _i, _len;
96+
curr = doc;
97+
for (_i = 0, _len = segments.length; _i < _len; _i++) {
98+
part = segments[_i];
99+
if (curr !== void 0) {
100+
curr = curr[part];
101+
}
102+
}
103+
return curr;
104+
};

package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "derby-hook",
3+
"version": "0.0.1",
4+
"description": "Derby store hook",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"derby",
11+
"hook"
12+
],
13+
"author": "Artur Zayats <[email protected]>",
14+
"license": "MIT"
15+
}

0 commit comments

Comments
 (0)