wok-server 0.1.0
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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/cache/cache.js +94 -0
- package/dist/cache/config.js +19 -0
- package/dist/cache/index.js +27 -0
- package/dist/cache/purge-task.js +56 -0
- package/dist/cache/stat.js +47 -0
- package/dist/config/convert.js +36 -0
- package/dist/config/exception.js +14 -0
- package/dist/config/index.js +67 -0
- package/dist/http-client/index.js +132 -0
- package/dist/i18n/ar.js +17 -0
- package/dist/i18n/de.js +17 -0
- package/dist/i18n/en-us.js +17 -0
- package/dist/i18n/es.js +17 -0
- package/dist/i18n/fr.js +17 -0
- package/dist/i18n/i18n.js +231 -0
- package/dist/i18n/index.js +52 -0
- package/dist/i18n/ja.js +17 -0
- package/dist/i18n/ko.js +17 -0
- package/dist/i18n/msg.js +2 -0
- package/dist/i18n/pt.js +17 -0
- package/dist/i18n/ru.js +17 -0
- package/dist/i18n/tag.js +18 -0
- package/dist/i18n/zh-HK.js +17 -0
- package/dist/i18n/zh-TW.js +17 -0
- package/dist/i18n/zh-cn.js +17 -0
- package/dist/index.js +13 -0
- package/dist/log/config.js +28 -0
- package/dist/log/date.js +21 -0
- package/dist/log/file.js +79 -0
- package/dist/log/index.js +109 -0
- package/dist/log/level.js +39 -0
- package/dist/log/store.js +16 -0
- package/dist/mongodb/collection.js +2 -0
- package/dist/mongodb/config.js +34 -0
- package/dist/mongodb/doc.js +2 -0
- package/dist/mongodb/exception.js +14 -0
- package/dist/mongodb/index.js +58 -0
- package/dist/mongodb/manager/base.js +563 -0
- package/dist/mongodb/manager/index.js +63 -0
- package/dist/mongodb/manager/tx-strict.js +84 -0
- package/dist/mongodb/manager/tx.js +30 -0
- package/dist/mongodb/migration.js +52 -0
- package/dist/mvc/access-log.js +31 -0
- package/dist/mvc/config.js +20 -0
- package/dist/mvc/exchange.js +113 -0
- package/dist/mvc/handler/index.js +6 -0
- package/dist/mvc/handler/json.js +33 -0
- package/dist/mvc/handler/restful.js +35 -0
- package/dist/mvc/handler/upload.js +33 -0
- package/dist/mvc/index.js +316 -0
- package/dist/mvc/interceptor.js +2 -0
- package/dist/mvc/query.js +43 -0
- package/dist/mvc/render/file.js +177 -0
- package/dist/mvc/render/html/html.js +90 -0
- package/dist/mvc/render/html/index.js +18 -0
- package/dist/mvc/render/html/style.js +2 -0
- package/dist/mvc/render/index.js +7 -0
- package/dist/mvc/render/json.js +26 -0
- package/dist/mvc/render/text.js +16 -0
- package/dist/mvc/router.js +2 -0
- package/dist/mysql/config.js +49 -0
- package/dist/mysql/exception.js +14 -0
- package/dist/mysql/index.js +85 -0
- package/dist/mysql/manager/base.js +233 -0
- package/dist/mysql/manager/index.js +107 -0
- package/dist/mysql/manager/ops/count.js +20 -0
- package/dist/mysql/manager/ops/criteria.js +326 -0
- package/dist/mysql/manager/ops/delete.js +65 -0
- package/dist/mysql/manager/ops/exist.js +26 -0
- package/dist/mysql/manager/ops/find.js +111 -0
- package/dist/mysql/manager/ops/index.js +14 -0
- package/dist/mysql/manager/ops/insert.js +101 -0
- package/dist/mysql/manager/ops/modify.js +10 -0
- package/dist/mysql/manager/ops/paginate.js +23 -0
- package/dist/mysql/manager/ops/query.js +9 -0
- package/dist/mysql/manager/ops/update.js +201 -0
- package/dist/mysql/manager/tx-strict.js +98 -0
- package/dist/mysql/manager/tx.js +30 -0
- package/dist/mysql/manager/utils.js +56 -0
- package/dist/mysql/migration.js +136 -0
- package/dist/mysql/table-info.js +8 -0
- package/dist/task/daily.js +58 -0
- package/dist/task/fixed-delay.js +33 -0
- package/dist/task/fixed-rate.js +37 -0
- package/dist/task/index.js +9 -0
- package/dist/task/task.js +39 -0
- package/dist/validation/exception.js +44 -0
- package/dist/validation/index.js +29 -0
- package/dist/validation/validator/array.js +38 -0
- package/dist/validation/validator/enum.js +28 -0
- package/dist/validation/validator/index.js +14 -0
- package/dist/validation/validator/length.js +40 -0
- package/dist/validation/validator/max-length.js +35 -0
- package/dist/validation/validator/max.js +29 -0
- package/dist/validation/validator/min-length.js +33 -0
- package/dist/validation/validator/min.js +29 -0
- package/dist/validation/validator/not-blank.js +33 -0
- package/dist/validation/validator/not-null.js +21 -0
- package/dist/validation/validator/plain-obj.js +32 -0
- package/dist/validation/validator/regexp.js +30 -0
- package/documentation/en/index.md +1 -0
- package/documentation/zh-cn/cache.md +59 -0
- package/documentation/zh-cn/config.md +68 -0
- package/documentation/zh-cn/http-client.md +33 -0
- package/documentation/zh-cn/i18n.md +154 -0
- package/documentation/zh-cn/index.md +25 -0
- package/documentation/zh-cn/log.md +40 -0
- package/documentation/zh-cn/mongodb.md +262 -0
- package/documentation/zh-cn/mvc.md +430 -0
- package/documentation/zh-cn/mysql.md +389 -0
- package/documentation/zh-cn/task.md +50 -0
- package/documentation/zh-cn/test.md +57 -0
- package/documentation/zh-cn/validate.md +125 -0
- package/package.json +46 -0
- package/types/cache/cache.d.ts +52 -0
- package/types/cache/config.d.ts +32 -0
- package/types/cache/index.d.ts +2 -0
- package/types/cache/purge-task.d.ts +11 -0
- package/types/cache/stat.d.ts +26 -0
- package/types/config/convert.d.ts +6 -0
- package/types/config/exception.d.ts +7 -0
- package/types/config/index.d.ts +15 -0
- package/types/http-client/index.d.ts +71 -0
- package/types/i18n/ar.d.ts +2 -0
- package/types/i18n/de.d.ts +2 -0
- package/types/i18n/en-us.d.ts +2 -0
- package/types/i18n/es.d.ts +2 -0
- package/types/i18n/fr.d.ts +2 -0
- package/types/i18n/i18n.d.ts +102 -0
- package/types/i18n/index.d.ts +9 -0
- package/types/i18n/ja.d.ts +2 -0
- package/types/i18n/ko.d.ts +2 -0
- package/types/i18n/msg.d.ts +50 -0
- package/types/i18n/pt.d.ts +2 -0
- package/types/i18n/ru.d.ts +2 -0
- package/types/i18n/tag.d.ts +11 -0
- package/types/i18n/zh-HK.d.ts +2 -0
- package/types/i18n/zh-TW.d.ts +2 -0
- package/types/i18n/zh-cn.d.ts +2 -0
- package/types/index.d.ts +10 -0
- package/types/log/config.d.ts +27 -0
- package/types/log/date.d.ts +2 -0
- package/types/log/file.d.ts +5 -0
- package/types/log/index.d.ts +34 -0
- package/types/log/level.d.ts +15 -0
- package/types/log/store.d.ts +12 -0
- package/types/mongodb/collection.d.ts +25 -0
- package/types/mongodb/config.d.ts +45 -0
- package/types/mongodb/doc.d.ts +11 -0
- package/types/mongodb/exception.d.ts +7 -0
- package/types/mongodb/index.d.ts +29 -0
- package/types/mongodb/manager/base.d.ts +188 -0
- package/types/mongodb/manager/index.d.ts +38 -0
- package/types/mongodb/manager/tx-strict.d.ts +41 -0
- package/types/mongodb/manager/tx.d.ts +21 -0
- package/types/mongodb/migration.d.ts +12 -0
- package/types/mvc/access-log.d.ts +7 -0
- package/types/mvc/config.d.ts +30 -0
- package/types/mvc/exchange.d.ts +72 -0
- package/types/mvc/handler/index.d.ts +3 -0
- package/types/mvc/handler/json.d.ts +23 -0
- package/types/mvc/handler/restful.d.ts +11 -0
- package/types/mvc/handler/upload.d.ts +40 -0
- package/types/mvc/index.d.ts +49 -0
- package/types/mvc/interceptor.d.ts +11 -0
- package/types/mvc/query.d.ts +13 -0
- package/types/mvc/render/file.d.ts +10 -0
- package/types/mvc/render/html/html.d.ts +98 -0
- package/types/mvc/render/html/index.d.ts +11 -0
- package/types/mvc/render/html/style.d.ts +1201 -0
- package/types/mvc/render/index.d.ts +4 -0
- package/types/mvc/render/json.d.ts +17 -0
- package/types/mvc/render/text.d.ts +10 -0
- package/types/mvc/router.d.ts +11 -0
- package/types/mysql/config.d.ts +86 -0
- package/types/mysql/exception.d.ts +7 -0
- package/types/mysql/index.d.ts +16 -0
- package/types/mysql/manager/base.d.ts +158 -0
- package/types/mysql/manager/index.d.ts +36 -0
- package/types/mysql/manager/ops/count.d.ts +13 -0
- package/types/mysql/manager/ops/criteria.d.ts +120 -0
- package/types/mysql/manager/ops/delete.d.ts +46 -0
- package/types/mysql/manager/ops/exist.d.ts +6 -0
- package/types/mysql/manager/ops/find.d.ts +66 -0
- package/types/mysql/manager/ops/index.d.ts +10 -0
- package/types/mysql/manager/ops/insert.d.ts +18 -0
- package/types/mysql/manager/ops/modify.d.ts +3 -0
- package/types/mysql/manager/ops/paginate.d.ts +36 -0
- package/types/mysql/manager/ops/query.d.ts +3 -0
- package/types/mysql/manager/ops/update.d.ts +70 -0
- package/types/mysql/manager/tx-strict.d.ts +34 -0
- package/types/mysql/manager/tx.d.ts +15 -0
- package/types/mysql/manager/utils.d.ts +17 -0
- package/types/mysql/migration.d.ts +8 -0
- package/types/mysql/table-info.d.ts +36 -0
- package/types/task/daily.d.ts +15 -0
- package/types/task/fixed-delay.d.ts +8 -0
- package/types/task/fixed-rate.d.ts +8 -0
- package/types/task/index.d.ts +4 -0
- package/types/task/task.d.ts +33 -0
- package/types/validation/exception.d.ts +43 -0
- package/types/validation/index.d.ts +32 -0
- package/types/validation/validator/array.d.ts +5 -0
- package/types/validation/validator/enum.d.ts +8 -0
- package/types/validation/validator/index.d.ts +11 -0
- package/types/validation/validator/length.d.ts +10 -0
- package/types/validation/validator/max-length.d.ts +8 -0
- package/types/validation/validator/max.d.ts +7 -0
- package/types/validation/validator/min-length.d.ts +6 -0
- package/types/validation/validator/min.d.ts +7 -0
- package/types/validation/validator/not-blank.d.ts +7 -0
- package/types/validation/validator/not-null.d.ts +6 -0
- package/types/validation/validator/plain-obj.d.ts +7 -0
- package/types/validation/validator/regexp.d.ts +8 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.setLogStore = exports.getLogger = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const os_1 = require("os");
|
|
6
|
+
const config_1 = require("./config");
|
|
7
|
+
const date_1 = require("./date");
|
|
8
|
+
const level_1 = require("./level");
|
|
9
|
+
const store_1 = require("./store");
|
|
10
|
+
const file_1 = require("./file");
|
|
11
|
+
/**
|
|
12
|
+
* 日志级别的值
|
|
13
|
+
*/
|
|
14
|
+
const LOG_LEVEL_VAL = level_1.LogLevelVals[config_1.config.level];
|
|
15
|
+
/**
|
|
16
|
+
* 文件存储
|
|
17
|
+
*/
|
|
18
|
+
if (config_1.config.file) {
|
|
19
|
+
(0, store_1.setLogStore)(file_1.fileStore);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 输出日志
|
|
23
|
+
* @param level
|
|
24
|
+
* @param message
|
|
25
|
+
* @param error
|
|
26
|
+
*/
|
|
27
|
+
function log(level, message, error) {
|
|
28
|
+
const levelVal = level_1.LogLevelVals[level];
|
|
29
|
+
if (levelVal >= LOG_LEVEL_VAL) {
|
|
30
|
+
const date = new Date();
|
|
31
|
+
// 控制台输出日志
|
|
32
|
+
let msg = `[${(0, date_1.formatDateTime)(date)}][${level}]${message}`;
|
|
33
|
+
console.log(msg);
|
|
34
|
+
if (error) {
|
|
35
|
+
console.log(error);
|
|
36
|
+
}
|
|
37
|
+
// 存储中输出日志
|
|
38
|
+
const store = (0, store_1.getLogStore)();
|
|
39
|
+
if (store) {
|
|
40
|
+
if (error) {
|
|
41
|
+
if (error.stack) {
|
|
42
|
+
msg += os_1.EOL + error.stack;
|
|
43
|
+
}
|
|
44
|
+
else if (error.message) {
|
|
45
|
+
msg += os_1.EOL + error.message;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
msg += os_1.EOL + error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
store(msg);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const logger = Object.freeze({
|
|
56
|
+
/**
|
|
57
|
+
* debug 日志
|
|
58
|
+
*/
|
|
59
|
+
debug(message) {
|
|
60
|
+
log(level_1.LogLevel.DEBUG, message);
|
|
61
|
+
},
|
|
62
|
+
isDebugEnabled() {
|
|
63
|
+
return level_1.LogLevelVals[level_1.LogLevel.DEBUG] >= LOG_LEVEL_VAL;
|
|
64
|
+
},
|
|
65
|
+
/**
|
|
66
|
+
* info 日志
|
|
67
|
+
* @param message
|
|
68
|
+
*/
|
|
69
|
+
info(message) {
|
|
70
|
+
log(level_1.LogLevel.INFO, message);
|
|
71
|
+
},
|
|
72
|
+
isInfoEnabled() {
|
|
73
|
+
return level_1.LogLevelVals[level_1.LogLevel.INFO] >= LOG_LEVEL_VAL;
|
|
74
|
+
},
|
|
75
|
+
/**
|
|
76
|
+
* 警告日志
|
|
77
|
+
* @param message
|
|
78
|
+
* @param error
|
|
79
|
+
*/
|
|
80
|
+
warn(message, error) {
|
|
81
|
+
log(level_1.LogLevel.WARN, message, error);
|
|
82
|
+
},
|
|
83
|
+
isWarnEnabled() {
|
|
84
|
+
return level_1.LogLevelVals[level_1.LogLevel.WARN] >= LOG_LEVEL_VAL;
|
|
85
|
+
},
|
|
86
|
+
/**
|
|
87
|
+
* 错误日志
|
|
88
|
+
* @param message
|
|
89
|
+
* @param error
|
|
90
|
+
*/
|
|
91
|
+
error(message, error) {
|
|
92
|
+
log(level_1.LogLevel.ERROR, message, error);
|
|
93
|
+
},
|
|
94
|
+
isErrorEnabled() {
|
|
95
|
+
return level_1.LogLevelVals[level_1.LogLevel.ERROR] >= LOG_LEVEL_VAL;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
/**
|
|
99
|
+
* 获取日志对象.
|
|
100
|
+
* @returns
|
|
101
|
+
*/
|
|
102
|
+
function getLogger() {
|
|
103
|
+
return logger;
|
|
104
|
+
}
|
|
105
|
+
exports.getLogger = getLogger;
|
|
106
|
+
tslib_1.__exportStar(require("./config"), exports);
|
|
107
|
+
tslib_1.__exportStar(require("./level"), exports);
|
|
108
|
+
var store_2 = require("./store");
|
|
109
|
+
Object.defineProperty(exports, "setLogStore", { enumerable: true, get: function () { return store_2.setLogStore; } });
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseLogLevel = exports.LogLevelVals = exports.LogLevel = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* 日志级别.
|
|
6
|
+
*/
|
|
7
|
+
var LogLevel;
|
|
8
|
+
(function (LogLevel) {
|
|
9
|
+
LogLevel["DEBUG"] = "DEBUG";
|
|
10
|
+
LogLevel["INFO"] = "INFO";
|
|
11
|
+
LogLevel["WARN"] = "WARN";
|
|
12
|
+
LogLevel["ERROR"] = "ERROR";
|
|
13
|
+
})(LogLevel = exports.LogLevel || (exports.LogLevel = {}));
|
|
14
|
+
exports.LogLevelVals = Object.freeze({
|
|
15
|
+
[LogLevel.DEBUG]: 1,
|
|
16
|
+
[LogLevel.INFO]: 2,
|
|
17
|
+
[LogLevel.WARN]: 3,
|
|
18
|
+
[LogLevel.ERROR]: 4
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* 解析日志级别
|
|
22
|
+
* @param level
|
|
23
|
+
*/
|
|
24
|
+
function parseLogLevel(level) {
|
|
25
|
+
const iLevel = level.toUpperCase();
|
|
26
|
+
switch (iLevel) {
|
|
27
|
+
case 'DEBUG':
|
|
28
|
+
return LogLevel.DEBUG;
|
|
29
|
+
case 'INFO':
|
|
30
|
+
return LogLevel.INFO;
|
|
31
|
+
case 'WARN':
|
|
32
|
+
return LogLevel.WARN;
|
|
33
|
+
case 'ERROR':
|
|
34
|
+
return LogLevel.ERROR;
|
|
35
|
+
default:
|
|
36
|
+
throw new Error(`无法解析日志级别:${level}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.parseLogLevel = parseLogLevel;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLogStore = exports.setLogStore = void 0;
|
|
4
|
+
let STORE;
|
|
5
|
+
/**
|
|
6
|
+
* 设置日志存储.
|
|
7
|
+
* @param store
|
|
8
|
+
*/
|
|
9
|
+
function setLogStore(store) {
|
|
10
|
+
STORE = store;
|
|
11
|
+
}
|
|
12
|
+
exports.setLogStore = setLogStore;
|
|
13
|
+
function getLogStore() {
|
|
14
|
+
return STORE;
|
|
15
|
+
}
|
|
16
|
+
exports.getLogStore = getLogStore;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configValidation = exports.defaultConfig = void 0;
|
|
4
|
+
const validation_1 = require("../validation");
|
|
5
|
+
/**
|
|
6
|
+
* 默认配置.
|
|
7
|
+
*/
|
|
8
|
+
exports.defaultConfig = {
|
|
9
|
+
uri: 'mongodb://test:123456@localhost/test',
|
|
10
|
+
maxPoolSize: 10,
|
|
11
|
+
minPoolSize: 1,
|
|
12
|
+
maxConnecting: 10,
|
|
13
|
+
maxIdleTimeMS: 60000,
|
|
14
|
+
waitQueueTimeoutMS: 60000,
|
|
15
|
+
slowQueryWarn: true,
|
|
16
|
+
slowQueryMs: 200,
|
|
17
|
+
transactionTimeout: 5000,
|
|
18
|
+
transactionStrict: true
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 配置校验
|
|
22
|
+
*/
|
|
23
|
+
exports.configValidation = {
|
|
24
|
+
uri: [(0, validation_1.notBlank)()],
|
|
25
|
+
maxPoolSize: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(1000)],
|
|
26
|
+
minPoolSize: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(1000)],
|
|
27
|
+
maxConnecting: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(1000)],
|
|
28
|
+
maxIdleTimeMS: [(0, validation_1.notNull)(), (0, validation_1.min)(1)],
|
|
29
|
+
waitQueueTimeoutMS: [(0, validation_1.notNull)(), (0, validation_1.min)(1)],
|
|
30
|
+
slowQueryWarn: [(0, validation_1.notNull)()],
|
|
31
|
+
slowQueryMs: [(0, validation_1.notNull)(), (0, validation_1.min)(1), (0, validation_1.max)(3600000)],
|
|
32
|
+
transactionTimeout: [(0, validation_1.notNull)(), (0, validation_1.min)(0), (0, validation_1.max)(3600000)],
|
|
33
|
+
transactionStrict: [(0, validation_1.notNull)()]
|
|
34
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MongoDBException = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* MongoDB 异常.
|
|
6
|
+
*/
|
|
7
|
+
class MongoDBException extends Error {
|
|
8
|
+
message;
|
|
9
|
+
constructor(message) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.message = message;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
exports.MongoDBException = MongoDBException;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMongoDBManager = exports.enableMongoDB = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const mongodb_1 = require("mongodb");
|
|
6
|
+
const config_1 = require("../config");
|
|
7
|
+
const config_2 = require("./config");
|
|
8
|
+
const manager_1 = require("./manager");
|
|
9
|
+
const migration_1 = require("./migration");
|
|
10
|
+
const managerMap = new Map();
|
|
11
|
+
const DEFAULT_NAME = 'MONGO';
|
|
12
|
+
/**
|
|
13
|
+
* 启用 MongoDB
|
|
14
|
+
* @param opts 选项
|
|
15
|
+
*/
|
|
16
|
+
async function enableMongoDB(opts) {
|
|
17
|
+
const finalOpts = opts || {};
|
|
18
|
+
// 校验名称
|
|
19
|
+
if (finalOpts.name) {
|
|
20
|
+
if (!/^[a-zA-Z][a-zA-Z0-9_]{0,31}$/.test(finalOpts.name)) {
|
|
21
|
+
throw new Error(`The mongodb configuration name must start with an english letter, digit or an underscore (_), and cannot exceed 32 characters:${finalOpts.name}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const finalName = finalOpts.name || DEFAULT_NAME;
|
|
25
|
+
// 配置信息
|
|
26
|
+
const mongoConfig = (0, config_1.registerConfig)(config_2.defaultConfig, finalName, config_2.configValidation);
|
|
27
|
+
const client = new mongodb_1.MongoClient(mongoConfig.uri, {
|
|
28
|
+
maxPoolSize: mongoConfig.maxPoolSize,
|
|
29
|
+
minPoolSize: mongoConfig.minPoolSize,
|
|
30
|
+
maxConnecting: mongoConfig.maxConnecting,
|
|
31
|
+
maxIdleTimeMS: mongoConfig.maxIdleTimeMS,
|
|
32
|
+
waitQueueTimeoutMS: mongoConfig.waitQueueTimeoutMS
|
|
33
|
+
});
|
|
34
|
+
const db = client.db();
|
|
35
|
+
// 版本控制
|
|
36
|
+
if (finalOpts.migration) {
|
|
37
|
+
await (0, migration_1.migrate)(db, finalOpts.migration.versionList);
|
|
38
|
+
}
|
|
39
|
+
process.addListener('beforeExit', () => client.close());
|
|
40
|
+
managerMap.set(finalName, new manager_1.MongoDBManager(mongoConfig, db, client));
|
|
41
|
+
}
|
|
42
|
+
exports.enableMongoDB = enableMongoDB;
|
|
43
|
+
/**
|
|
44
|
+
* 获取管理器实例
|
|
45
|
+
* @param name
|
|
46
|
+
* @returns
|
|
47
|
+
*/
|
|
48
|
+
function getMongoDBManager(name) {
|
|
49
|
+
const manager = managerMap.get(name || DEFAULT_NAME);
|
|
50
|
+
if (!manager) {
|
|
51
|
+
throw new Error(`找不到想要的 mongodb 配置 ${name ? ': ' + name : ''},请先调用 enableMongoDB 启用`);
|
|
52
|
+
}
|
|
53
|
+
return manager;
|
|
54
|
+
}
|
|
55
|
+
exports.getMongoDBManager = getMongoDBManager;
|
|
56
|
+
tslib_1.__exportStar(require("./exception"), exports);
|
|
57
|
+
tslib_1.__exportStar(require("./collection"), exports);
|
|
58
|
+
tslib_1.__exportStar(require("./doc"), exports);
|