UNPKG

1.95 kBJavaScriptView Raw
1// share-node framework global functions
2const path = require('path');
3const logger = require('./utils/logger');
4
5/**
6 * get a logger
7 * @param category
8 */
9global.getLogger = function (category = '') {
10 const channel = category.replace(share.APP_PATH, '').replace('.js', '').trim();
11 return logger.getLogger(channel.split(path.sep).filter(item => !!item).join('.'));
12};
13
14/**
15 * get a controller
16 * @param name
17 */
18global.getController = function (name) {
19 // if name is empty, require base controller
20 const controllerPath = name || `${share.FRAMEWORK_PATH}lib${path.sep}base${path.sep}controller`;
21 return require(controllerPath) || null;
22};
23
24/**
25 * get a websocket handler
26 * @param name
27 */
28global.getWebsocketHandler = function (name) {
29 // if name is empty, require base websocket handler
30 const websocketPath = name || `${share.FRAMEWORK_PATH}lib${path.sep}base${path.sep}websocket`;
31 return require(websocketPath) || null;
32};
33
34/**
35 * get a validate
36 * @param name
37 */
38global.getValidate = function (name) {
39 // if name is empty, require base validate
40 const validatePath = name || `${share.FRAMEWORK_PATH}lib${path.sep}base${path.sep}validate`;
41 return require(validatePath) || null;
42};
43
44/**
45 * get a service
46 * @param name
47 */
48global.getService = function (name) {
49 if (name) {
50 const modelPath = `${share.APP_PATH}app${path.sep}service${path.sep}${name}`;
51 const Service = require(modelPath);
52 return new Service(name);
53 }
54 return require(`${share.FRAMEWORK_PATH}lib${path.sep}base${path.sep}service`) || null;
55};
56
57/**
58 * get a model
59 * @param name model name
60 * @param anotherName alias model name
61 */
62global.getModel = function (name, anotherName) {
63 if (name) {
64 const modelPath = `${share.APP_PATH}app${path.sep}model${path.sep}${name}`;
65 const Model = require(modelPath);
66 return new Model(name, anotherName);
67 }
68 return require(`${share.FRAMEWORK_PATH}lib${path.sep}base${path.sep}model`) || null;
69};