@paralect/hive 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/starter/src/db.js +9 -10
- package/starter/src/helpers/importHandlers.js +29 -0
- package/starter/src/lib/node-mongo/src/index.js +20 -8
- package/starter/src/lib/node-mongo/src/mongo-service.js +27 -9
- package/starter/src/resources/users/handlers/test.js +1 -0
- package/starter/src/routes/index.js +1 -0
package/package.json
CHANGED
package/starter/src/db.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import fs from "fs";
|
|
2
2
|
import _ from "lodash";
|
|
3
|
-
import requireDir from "require-dir";
|
|
4
3
|
import getSchemas from "helpers/getSchemas";
|
|
5
4
|
import getResources from "helpers/getResources";
|
|
5
|
+
import importHandlers from "helpers/importHandlers";
|
|
6
6
|
import config from "app-config";
|
|
7
7
|
import { connect } from "lib/node-mongo";
|
|
8
8
|
|
|
@@ -17,7 +17,7 @@ db.init = async () => {
|
|
|
17
17
|
await Promise.all(_.map(
|
|
18
18
|
schemaPaths,
|
|
19
19
|
async ({ file: schemaFile, resourceName, name: schemaName }) => {
|
|
20
|
-
let schema = (await import(schemaFile))
|
|
20
|
+
let { default: schema, secureFields = [] } = (await import(schemaFile));
|
|
21
21
|
|
|
22
22
|
if (process.env.HIVE_SRC) {
|
|
23
23
|
let extendSchemaPath = `${process.env.HIVE_SRC}/resources/${resourceName}/${schemaName}.extends.schema.js`;
|
|
@@ -27,25 +27,24 @@ db.init = async () => {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
db.schemas[schemaName] = schema;
|
|
30
|
+
|
|
30
31
|
db.services[schemaName] = db.createService(`${resourceName}`, {
|
|
31
|
-
validate: (obj) => {
|
|
32
|
+
validate: (obj) => {
|
|
32
33
|
return { value: schema.passthrough().parse(obj) };
|
|
33
34
|
},
|
|
35
|
+
secureFields: secureFields,
|
|
34
36
|
});
|
|
35
|
-
|
|
36
37
|
}
|
|
37
38
|
));
|
|
38
39
|
|
|
39
40
|
const resourcePaths = await getResources();
|
|
40
|
-
|
|
41
|
+
|
|
41
42
|
setTimeout(() => {
|
|
42
|
-
_.each(resourcePaths, ({
|
|
43
|
-
|
|
44
|
-
requireDir(`${dir}/handlers`);
|
|
45
|
-
}
|
|
43
|
+
_.each(resourcePaths, ({ name }) => {
|
|
44
|
+
importHandlers(name)
|
|
46
45
|
});
|
|
47
46
|
}, 0);
|
|
48
|
-
|
|
47
|
+
|
|
49
48
|
(await import("autoMap/addHandlers")).default();
|
|
50
49
|
(await import("autoMap/mapSchema")).default();
|
|
51
50
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import requireDir from "require-dir";
|
|
4
|
+
|
|
5
|
+
export default (resourceName) => {
|
|
6
|
+
if (fs.existsSync(`${process.env.HIVE_SRC}/resources/${resourceName}/handlers`)) {
|
|
7
|
+
requireDir(`${process.env.HIVE_SRC}/resources/${resourceName}/handlers`, {
|
|
8
|
+
mapValue: (handler, handlerName) => {
|
|
9
|
+
console.log(
|
|
10
|
+
`[handlers] Registering handler ${handlerName}`
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
return handler;
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (fs.existsSync(`${__dirname}/../resources/${resourceName}/handlers`)) {
|
|
19
|
+
requireDir(`${__dirname}/../resources/${resourceName}/handlers`, {
|
|
20
|
+
mapValue: (handler, handlerName) => {
|
|
21
|
+
console.log(
|
|
22
|
+
`[handlers] Registering handler ${handlerName}`
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return handler;
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
@@ -1,20 +1,25 @@
|
|
|
1
|
-
import monk from
|
|
2
|
-
import _ from
|
|
3
|
-
import MongoService from
|
|
4
|
-
import MongoQueryService from
|
|
1
|
+
import monk from 'monk';
|
|
2
|
+
import _ from 'lodash';
|
|
3
|
+
import MongoService from './mongo-service';
|
|
4
|
+
import MongoQueryService from './mongo-query-service';
|
|
5
|
+
|
|
5
6
|
const logger = global.logger || console;
|
|
6
|
-
|
|
7
|
+
|
|
8
|
+
export const connect = (connectionString, settings) => {
|
|
7
9
|
const connectionSettings = _.defaults({}, settings, {
|
|
8
10
|
connectTimeoutMS: 20000,
|
|
9
11
|
});
|
|
10
12
|
const db = monk(connectionString, connectionSettings);
|
|
13
|
+
|
|
11
14
|
db.on("error-opening", (err) => {
|
|
12
15
|
logger.error(err, "Failed to connect to the mongodb on start");
|
|
13
16
|
throw err;
|
|
14
17
|
});
|
|
18
|
+
|
|
15
19
|
db.on("open", () => {
|
|
16
|
-
logger.info(`Connected to mongodb
|
|
20
|
+
logger.info(`Connected to mongodb`);
|
|
17
21
|
});
|
|
22
|
+
|
|
18
23
|
db.on("close", (err) => {
|
|
19
24
|
if (err) {
|
|
20
25
|
logger.error(err, `Lost connection with mongodb: ${connectionString}`);
|
|
@@ -22,31 +27,38 @@ const connect = (connectionString, settings) => {
|
|
|
22
27
|
logger.warn(`Closed connection with mongodb: ${connectionString}`);
|
|
23
28
|
}
|
|
24
29
|
});
|
|
30
|
+
|
|
25
31
|
db.on("connected", (err) => {
|
|
26
32
|
if (err) {
|
|
27
33
|
logger.error(err);
|
|
28
34
|
} else {
|
|
29
|
-
logger.info(`Connected to mongodb
|
|
35
|
+
logger.info(`Connected to mongodb`);
|
|
30
36
|
}
|
|
31
37
|
});
|
|
38
|
+
|
|
32
39
|
db.createService = (collectionName, options = {}) => {
|
|
33
40
|
const collection = db.get(collectionName, { castIds: false });
|
|
41
|
+
|
|
34
42
|
return new MongoService(collection, options);
|
|
35
43
|
};
|
|
44
|
+
|
|
36
45
|
db.setServiceMethod = (name, method) => {
|
|
37
46
|
MongoService.prototype[name] = function customMethod(...args) {
|
|
38
47
|
return method.apply(this, [this, ...args]);
|
|
39
48
|
};
|
|
40
49
|
};
|
|
50
|
+
|
|
41
51
|
db.createQueryService = (collectionName, options = {}) => {
|
|
42
52
|
const collection = db.get(collectionName, { castIds: false });
|
|
53
|
+
|
|
43
54
|
return new MongoQueryService(collection, options);
|
|
44
55
|
};
|
|
56
|
+
|
|
45
57
|
db.setQueryServiceMethod = (name, method) => {
|
|
46
58
|
MongoQueryService.prototype[name] = function customMethod(...args) {
|
|
47
59
|
return method.apply(this, [this, ...args]);
|
|
48
60
|
};
|
|
49
61
|
};
|
|
62
|
+
|
|
50
63
|
return db;
|
|
51
64
|
};
|
|
52
|
-
export { connect };
|
|
@@ -10,6 +10,7 @@ const defaultOptions = {
|
|
|
10
10
|
useStringId: true,
|
|
11
11
|
validate: undefined,
|
|
12
12
|
emitter: undefined,
|
|
13
|
+
secureFields: ['verySecureField'],
|
|
13
14
|
|
|
14
15
|
onBeforeCreated: ({ docs }) => docs,
|
|
15
16
|
};
|
|
@@ -111,20 +112,28 @@ class MongoService extends MongoQueryService {
|
|
|
111
112
|
return this._bus.on(`${this._collection.name}:${eventName}`, handler);
|
|
112
113
|
}
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
onUpdated(fieldNames = [], handler = (e) => { }) {
|
|
115
116
|
return this.on(`${this._collection.name}:${eventName}`, (event) => {
|
|
116
117
|
const { doc, prevDoc } = event;
|
|
117
118
|
|
|
118
119
|
let isFieldChanged = false;
|
|
119
120
|
|
|
120
|
-
_.
|
|
121
|
-
|
|
121
|
+
if (_.isArray(fieldNames)) {
|
|
122
|
+
_.forEach(fieldNames, (fieldName) => {
|
|
123
|
+
if (!_.isEqual(doc[fieldName], prevDoc[fieldName])) {
|
|
124
|
+
isFieldChanged = true;
|
|
125
|
+
return false; // break loop
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return true;
|
|
129
|
+
});
|
|
130
|
+
} else if (_.isObject(fieldNames)) {
|
|
131
|
+
const fieldName = Object.keys(fieldNames)[0];
|
|
132
|
+
|
|
133
|
+
if (_.isEqual(_.get(doc, fieldName), fieldNames[fieldName]) && !_.isEqual(_.get(doc, fieldName), _.get(prevDoc, fieldName))) {
|
|
122
134
|
isFieldChanged = true;
|
|
123
|
-
return false; // break loop
|
|
124
135
|
}
|
|
125
|
-
|
|
126
|
-
return true;
|
|
127
|
-
});
|
|
136
|
+
}
|
|
128
137
|
|
|
129
138
|
if (isFieldChanged) handler(event);
|
|
130
139
|
});
|
|
@@ -158,6 +167,10 @@ class MongoService extends MongoQueryService {
|
|
|
158
167
|
});
|
|
159
168
|
});
|
|
160
169
|
|
|
170
|
+
created = created.map(doc => {
|
|
171
|
+
return _.omit(doc, options.isIncludeSecureFields ? [] : this._options.secureFields);
|
|
172
|
+
})
|
|
173
|
+
|
|
161
174
|
return created.length > 1 ? created : created[0];
|
|
162
175
|
}
|
|
163
176
|
|
|
@@ -184,7 +197,7 @@ class MongoService extends MongoQueryService {
|
|
|
184
197
|
if (this._options.addUpdatedOnField)
|
|
185
198
|
entity.updatedOn = new Date().toISOString();
|
|
186
199
|
entity = await updateFn(entity);
|
|
187
|
-
|
|
200
|
+
let updated = await this._validate(entity);
|
|
188
201
|
|
|
189
202
|
await this._collection.update(
|
|
190
203
|
{ ...query, _id: doc._id },
|
|
@@ -192,11 +205,14 @@ class MongoService extends MongoQueryService {
|
|
|
192
205
|
options
|
|
193
206
|
);
|
|
194
207
|
|
|
208
|
+
|
|
195
209
|
this._bus.emit(`${this._collection.name}:updated`, {
|
|
196
210
|
doc: updated,
|
|
197
211
|
prevDoc: doc,
|
|
198
212
|
});
|
|
199
213
|
|
|
214
|
+
updated = _.omit(updated, options.isIncludeSecureFields ? [] : this._options.secureFields);
|
|
215
|
+
|
|
200
216
|
return updated;
|
|
201
217
|
}
|
|
202
218
|
|
|
@@ -213,7 +229,7 @@ class MongoService extends MongoQueryService {
|
|
|
213
229
|
const { results: docs } = await this.find(query, findOptions);
|
|
214
230
|
if (docs.length === 0) return [];
|
|
215
231
|
|
|
216
|
-
|
|
232
|
+
let updated = await Promise.all(
|
|
217
233
|
docs.map(async (doc) => {
|
|
218
234
|
let entity = _.cloneDeep(doc);
|
|
219
235
|
|
|
@@ -243,6 +259,8 @@ class MongoService extends MongoQueryService {
|
|
|
243
259
|
});
|
|
244
260
|
});
|
|
245
261
|
|
|
262
|
+
updated = updated.map(doc => _.omit(doc, options.isIncludeSecureFields ? [] : this._options.secureFields));
|
|
263
|
+
|
|
246
264
|
return updated;
|
|
247
265
|
}
|
|
248
266
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
console.log('hello from handler');
|
|
@@ -96,6 +96,7 @@ export default async (app) => {
|
|
|
96
96
|
|
|
97
97
|
endpoints.forEach(({ endpoint, requestSchema, middlewares = [], handler }) => {
|
|
98
98
|
let targetRouter;
|
|
99
|
+
console.log('[routes] Register endpoint', resourceName, endpoint?.method || 'GET', endpoint?.url);
|
|
99
100
|
|
|
100
101
|
let url = endpoint.absoluteUrl || endpoint.url;
|
|
101
102
|
|