1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd-up: Rapid QEWD API Development |
|
5 | | |
|
6 | | Copyright (c) 2018-19 M/Gateway Developments Ltd, |
|
7 | | Redhill, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 2 October 2019
|
28 |
|
29 | */
|
30 |
|
31 | const ESC = '~~';
|
32 |
|
33 | function pathFromSubs(subsArr) {
|
34 | var path = '';
|
35 | var delim = '';
|
36 | subsArr.forEach(function(subscript) {
|
37 | path = path + delim + subscript;
|
38 | delim = '/';
|
39 | });
|
40 | return path;
|
41 | }
|
42 |
|
43 | function toSubs(path, apply) {
|
44 | var subs = path.split('/');
|
45 | subs.forEach(function(subscript, index) {
|
46 | var sub = subscript.toString().split(ESC).join('/');
|
47 | sub = decodeURI(sub);
|
48 | if (apply === 'toLowerCase') {
|
49 | sub = sub.toLowerCase();
|
50 | }
|
51 | subs[index] = sub;
|
52 | });
|
53 | return subs;
|
54 | }
|
55 |
|
56 | function deleteIndex(docNode, docStoreEvents) {
|
57 |
|
58 | //console.log('docNode: ' + JSON.stringify(docNode));
|
59 |
|
60 | var indexObj;
|
61 | var indexDoc;
|
62 | var indexPath;
|
63 | var matchObj;
|
64 | var before;
|
65 | var routeToIndex;
|
66 | var path;
|
67 |
|
68 | var beforeDeletePath = pathFromSubs(docNode.path);
|
69 | var documentName = docNode.documentName;
|
70 |
|
71 | for (path in docStoreEvents[documentName].indices) {
|
72 | //console.log(documentName + ': ' + docNode.path + ': trying path ' + path);
|
73 | indexObj = docStoreEvents[documentName].indices[path];
|
74 | routeToIndex = indexObj.targetRoute;
|
75 | matchObj = routeToIndex.match(beforeDeletePath);
|
76 | //console.log('matchObj = ' + JSON.stringify(matchObj, null, 2));
|
77 | if (matchObj) {
|
78 | // exact path match, so delete specific index
|
79 | matchObj.value = docNode.value.toString().split('/').join(ESC);
|
80 | indexPath = indexObj.indexRoute.reverse(matchObj);
|
81 | //console.log('deleting index: ' + toSubs(indexPath));
|
82 | indexDoc = this.db.use(indexObj.documentName);
|
83 | indexDoc.$(toSubs(indexPath, indexObj.apply)).delete();
|
84 | }
|
85 | }
|
86 | }
|
87 |
|
88 | module.exports = function(docStoreEvents) {
|
89 |
|
90 | var _this = this;
|
91 |
|
92 | this.documentStore.on('afterSet', function(docNode) {
|
93 | //console.log('&&&& afterSet triggered for ' + JSON.stringify(docNode, null, 2));
|
94 | if (docStoreEvents[docNode.documentName]) {
|
95 | if (docStoreEvents[docNode.documentName].indices) {
|
96 |
|
97 | var afterSetPath = pathFromSubs(docNode.path);
|
98 | var indexObj;
|
99 | var indexDoc;
|
100 | var indexPath;
|
101 | var matchObj;
|
102 | var before;
|
103 | var routeToIndex;
|
104 | var value;
|
105 |
|
106 | for (var path in docStoreEvents[docNode.documentName].indices) {
|
107 | //console.log('trying path ' + path);
|
108 | indexObj = docStoreEvents[docNode.documentName].indices[path];
|
109 | routeToIndex = indexObj.targetRoute;
|
110 | matchObj = routeToIndex.match(afterSetPath);
|
111 | //console.log('matchObj = ' + JSON.stringify(matchObj, null, 2));
|
112 | if (matchObj) {
|
113 | if (indexObj.handler) {
|
114 | indexObj.handler.afterSet.call(_this, docNode);
|
115 | }
|
116 | else if (indexObj.indexRoute && docNode.value !== '') {
|
117 | indexDoc = _this.db.use(indexObj.documentName);
|
118 | //console.log('index document name: ' + indexObj.documentName);
|
119 | before = docNode.before;
|
120 | if (before.exists && before.value !== '' && docNode.value !== before.value) {
|
121 | matchObj.value = before.value.toString().split('/').join(ESC);
|
122 | indexPath = indexObj.indexRoute.reverse(matchObj);
|
123 | //console.log('*** deleting old index: ' + indexPath);
|
124 | indexDoc.$(toSubs(indexPath, indexObj.apply)).delete();
|
125 | }
|
126 | matchObj.value = docNode.value.toString().split('/').join(ESC);
|
127 | indexPath = indexObj.indexRoute.reverse(matchObj);
|
128 | //console.log('creating index: ' + toSubs(indexPath));
|
129 | value = '';
|
130 | if (indexObj.value && matchObj[indexObj.value]) {
|
131 | value = matchObj[indexObj.value];
|
132 | }
|
133 | indexDoc.$(toSubs(indexPath, indexObj.apply)).value = value;
|
134 | }
|
135 | }
|
136 | }
|
137 | }
|
138 | }
|
139 | });
|
140 |
|
141 | this.documentStore.on('beforeDelete', function(docNode) {
|
142 | //console.log('&&&& beforeDelete triggered for ' + JSON.stringify(docNode, null, 2));
|
143 | if (docStoreEvents[docNode.documentName]) {
|
144 | if (docStoreEvents[docNode.documentName].indices) {
|
145 | var rootDoc = new _this.documentStore.DocumentNode(docNode.documentName, docNode.path);
|
146 |
|
147 | if (rootDoc.hasValue) {
|
148 | // deleting a leaf node so find exact index match(es) and delete them
|
149 | deleteIndex.call(_this, rootDoc, docStoreEvents)
|
150 | }
|
151 | else {
|
152 | // deleting at a higher level in node tree
|
153 | // so spin through the leaf nodes at this level and see if any
|
154 | // are indexed - if so, delete them
|
155 |
|
156 | //console.log('deleting at higher level of document');
|
157 |
|
158 | rootDoc.forEachLeafNode(function(value, leafNode) {
|
159 | deleteIndex.call(_this, leafNode, docStoreEvents)
|
160 | });
|
161 | }
|
162 | }
|
163 | }
|
164 | else {
|
165 | //console.log('beforeDelete ignored for ' + docNode.documentName);
|
166 | }
|
167 | });
|
168 |
|
169 | };
|