Skip to content

Commit 93689f3

Browse files
committed
Take into account new ShareDb API
1 parent e00861a commit 93689f3

File tree

3 files changed

+41
-61
lines changed

3 files changed

+41
-61
lines changed

README.md

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Derby-hook
1+
## sharedb-hooks
22

33
The way to hook db-interactions on the server
44

@@ -11,10 +11,10 @@ server.js
1111
```js
1212
// ...
1313

14-
var derbyHook = require('derby-hook');
14+
var shareDbHooks = require('sharedb-hooks');
1515

1616
// Add 'hook' and 'onQuery' functions to the store
17-
derbyHook(store);
17+
shareDbHooks(store);
1818

1919
// ...
2020
```
@@ -49,24 +49,6 @@ then you can use the function to hook model events, for example:
4949

5050
```
5151

52-
## MIT License
53-
Copyright (c) 2014 by Artur Zayats
52+
## MIT License 2015
5453

55-
Permission is hereby granted, free of charge, to any person obtaining a copy
56-
of this software and associated documentation files (the "Software"), to deal
57-
in the Software without restriction, including without limitation the rights
58-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
59-
copies of the Software, and to permit persons to whom the Software is
60-
furnished to do so, subject to the following conditions:
61-
62-
The above copyright notice and this permission notice shall be included in
63-
all copies or substantial portions of the Software.
64-
65-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71-
THE SOFTWARE.
7254

index.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,48 @@
22
module.exports = function(store) {
33

44
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;
5+
var emitter = store.backend || store.shareClient;
6+
7+
emitter.use('after submit', function(shareRequest, next) {
8+
var collectionName, firstDot, fullPath, matches, regExp, relPath, segments, op;
9+
10+
var opData = shareRequest.opData || shareRequest.op;
11+
812
if (opData.del || opData.create) {
913
collectionName = pattern;
10-
if (collectionName !== shareRequest.collection) {
11-
return next();
12-
}
14+
if (collectionName !== shareRequest.collection) return next();
1315
} else {
1416
firstDot = pattern.indexOf('.');
1517
if (firstDot === -1) {
16-
if (!patternToRegExp(pattern).test(collectionName)) {
17-
return next();
18-
}
18+
if (!patternToRegExp(pattern).test(shareRequest.collection)) return next();
1919
} else {
2020
collectionName = pattern.slice(0, firstDot);
21-
if (collectionName !== shareRequest.collection) {
22-
return next();
23-
}
21+
if (collectionName !== shareRequest.collection) return next();
2422
}
2523
}
26-
snapshot = shareRequest.snapshot, docName = shareRequest.docName, backend = shareRequest.backend;
27-
session = shareRequest.agent.connectSession;
24+
25+
var snapshot = shareRequest.snapshot;
26+
var docName = shareRequest.docName || shareRequest.id;
27+
var backend = shareRequest.backend;
28+
var session = shareRequest.agent.connectSession;
29+
2830
switch (method) {
2931
case 'del':
30-
if (!opData.del) {
31-
return next();
32-
}
32+
if (!opData.del) return next();
3333
fn(docName, shareRequest, session, backend);
3434
break;
3535
case 'create':
36-
if (!opData.create) {
37-
return next();
38-
}
36+
if (!opData.create) return next();
3937
fn(docName, shareRequest.snapshot.data, session, backend);
4038
break;
4139
case 'change':
42-
_ref = opData.op;
43-
if (_ref) {
44-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
45-
op = _ref[_i];
40+
var ops = opData.op;
41+
if (ops) {
42+
for (var i = 0; i < ops.length; i++) {
43+
op = ops[i];
4644
segments = op.p;
47-
if (op.si || op.sd) {
48-
segments = segments.slice(0, -1);
49-
}
45+
if (op.si || op.sd) segments = segments.slice(0, -1);
46+
5047
relPath = segments.join('.');
5148
fullPath = collectionName + '.' + docName + '.' + relPath;
5249
regExp = patternToRegExp(pattern);
@@ -57,28 +54,29 @@ module.exports = function(store) {
5754
}
5855
}
5956
}
60-
return next();
57+
next();
6158
});
6259

6360
};
6461

6562
store.onQuery = function(collectionName, cb) {
66-
return this.shareClient.use('query', function(shareRequest, next) {
67-
var session;
63+
var emitter = store.backend || store.shareClient;
64+
65+
emitter.use('query', function(shareRequest, next) {
6866

69-
session = shareRequest.agent.connectSession;
67+
var session = shareRequest.agent.connectSession;
7068

7169
if (collectionName === '*') {
7270
return cb(shareRequest.collection, shareRequest.query, session, next);
73-
} else {
74-
if (shareRequest.collection !== collectionName) {
75-
return next();
76-
}
77-
return cb(shareRequest.query, session, next);
7871
}
72+
73+
if (shareRequest.collection !== collectionName) return next();
74+
75+
cb(shareRequest.query, session, next);
76+
7977
});
8078
};
81-
return true;
79+
8280
};
8381

8482

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "derby-hook",
2+
"name": "sharedb-hooks",
33
"version": "0.0.6",
4-
"description": "Derby store hook",
4+
"description": "ShareDb hooks",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"

0 commit comments

Comments
 (0)