cgserver 9.1.0 → 9.1.2
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/README.md
CHANGED
|
@@ -53,21 +53,35 @@ class MongoManager {
|
|
|
53
53
|
async init(cfgs) {
|
|
54
54
|
for (let i = 0; i < cfgs.length; ++i) {
|
|
55
55
|
let cfg = cfgs[i];
|
|
56
|
-
|
|
57
|
-
Log_1.GLog.error("数据库配置得database不能相同!database=" + cfg.database);
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
let mongoext = new MongoExt();
|
|
61
|
-
let ret = await mongoext.init(cfg);
|
|
56
|
+
let ret = await this.addMongo(cfg);
|
|
62
57
|
if (!ret) {
|
|
63
|
-
Log_1.GLog.error("数据库初始化失败!cfg=" + JSON.stringify(cfg));
|
|
64
58
|
return false;
|
|
65
59
|
}
|
|
66
|
-
this._dbs[cfg.database] = mongoext;
|
|
67
|
-
this._defdbname = cfg.database;
|
|
68
60
|
}
|
|
69
61
|
return true;
|
|
70
62
|
}
|
|
63
|
+
async addMongo(cfg) {
|
|
64
|
+
if (this._dbs[cfg.database]) {
|
|
65
|
+
Log_1.GLog.error("数据库配置得database不能相同!database=" + cfg.database);
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
let mongoext = new MongoExt();
|
|
69
|
+
let ret = await mongoext.init(cfg);
|
|
70
|
+
if (!ret) {
|
|
71
|
+
Log_1.GLog.error("数据库初始化失败!cfg=" + JSON.stringify(cfg));
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
this._dbs[cfg.database] = mongoext;
|
|
75
|
+
this._defdbname = cfg.database;
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
async removeMongo(dbname, force = false) {
|
|
79
|
+
let mongo = this.getMongo(dbname);
|
|
80
|
+
if (!mongo) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
mongo.close(force);
|
|
84
|
+
}
|
|
71
85
|
getMongo(dbname = "") {
|
|
72
86
|
if (!dbname) {
|
|
73
87
|
dbname = this._defdbname;
|
|
@@ -108,15 +122,18 @@ class MongoExt {
|
|
|
108
122
|
this._mongocfg = cfg;
|
|
109
123
|
this._inited = true;
|
|
110
124
|
Log_1.GLog.info("mongo config=" + JSON.stringify(this._mongocfg));
|
|
111
|
-
|
|
112
|
-
await Core_1.core.safeCall(
|
|
125
|
+
this._mongoClient = new mongo.MongoClient("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
|
|
126
|
+
await Core_1.core.safeCall(this._mongoClient.connect, this._mongoClient);
|
|
113
127
|
this.onConnect();
|
|
114
|
-
this._mongoDb =
|
|
128
|
+
this._mongoDb = this._mongoClient.db(this._mongocfg.database);
|
|
115
129
|
for (let i = 0; i < this._init_cbs.length; ++i) {
|
|
116
130
|
this._init_cbs[i]();
|
|
117
131
|
}
|
|
118
132
|
return true;
|
|
119
133
|
}
|
|
134
|
+
close(force = false) {
|
|
135
|
+
this._mongoClient.close(force);
|
|
136
|
+
}
|
|
120
137
|
registerInitCb(cb) {
|
|
121
138
|
this._init_cbs.push(cb);
|
|
122
139
|
}
|
|
@@ -124,17 +141,6 @@ class MongoExt {
|
|
|
124
141
|
this._mongo_init_succ = true;
|
|
125
142
|
Log_1.GLog.info("mongo has connected!");
|
|
126
143
|
}
|
|
127
|
-
onEnd() {
|
|
128
|
-
this._mongo_init_succ = false;
|
|
129
|
-
Log_1.GLog.error("mongo has ended!");
|
|
130
|
-
Log_1.GLog.info("mongo try reconnect");
|
|
131
|
-
this.init(this._mongocfg);
|
|
132
|
-
}
|
|
133
|
-
onError(err) {
|
|
134
|
-
Log_1.GLog.error("Error " + err);
|
|
135
|
-
Log_1.GLog.info("mongo try reconnect");
|
|
136
|
-
this.init(this._mongocfg);
|
|
137
|
-
}
|
|
138
144
|
/**
|
|
139
145
|
* 获取自增长id
|
|
140
146
|
* @param key
|
|
@@ -5,6 +5,7 @@ const request = require("request");
|
|
|
5
5
|
const qs = require("querystring");
|
|
6
6
|
const Log_1 = require("./Log");
|
|
7
7
|
const Core_1 = require("../Core/Core");
|
|
8
|
+
const lossless_json_1 = require("lossless-json");
|
|
8
9
|
exports.GHttpTool = null;
|
|
9
10
|
class HttpTool {
|
|
10
11
|
get(options_url) {
|
|
@@ -24,7 +25,7 @@ class HttpTool {
|
|
|
24
25
|
}
|
|
25
26
|
try {
|
|
26
27
|
if (Core_1.core.isString(body)) {
|
|
27
|
-
body =
|
|
28
|
+
body = (0, lossless_json_1.parse)(body);
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
31
|
catch (e) {
|
|
@@ -56,7 +57,7 @@ class HttpTool {
|
|
|
56
57
|
}
|
|
57
58
|
try {
|
|
58
59
|
if (Core_1.core.isString(body)) {
|
|
59
|
-
body =
|
|
60
|
+
body = (0, lossless_json_1.parse)(body);
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
catch (e) {
|
|
@@ -42,6 +42,8 @@ export declare class MongoManager {
|
|
|
42
42
|
protected _defdbname: string;
|
|
43
43
|
get defdbname(): string;
|
|
44
44
|
init(cfgs: MongoConfig[]): Promise<boolean>;
|
|
45
|
+
addMongo(cfg: MongoConfig): Promise<boolean>;
|
|
46
|
+
removeMongo(dbname: string, force?: boolean): Promise<boolean>;
|
|
45
47
|
getMongo(dbname?: string): MongoExt;
|
|
46
48
|
}
|
|
47
49
|
export declare let GMongoMgr: MongoManager;
|
|
@@ -57,10 +59,9 @@ export declare class MongoExt {
|
|
|
57
59
|
get isValid(): boolean;
|
|
58
60
|
constructor();
|
|
59
61
|
init(cfg: MongoConfig): Promise<boolean>;
|
|
62
|
+
close(force?: boolean): void;
|
|
60
63
|
registerInitCb(cb: Function): void;
|
|
61
64
|
onConnect(): void;
|
|
62
|
-
onEnd(): void;
|
|
63
|
-
onError(err: any): void;
|
|
64
65
|
/**
|
|
65
66
|
* 获取自增长id
|
|
66
67
|
* @param key
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cgserver",
|
|
3
|
-
"version": "9.1.
|
|
3
|
+
"version": "9.1.2",
|
|
4
4
|
"author": "trojan",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"description": "free for all.Websocket or Http",
|
|
@@ -62,6 +62,7 @@
|
|
|
62
62
|
"https": "^1.0.0",
|
|
63
63
|
"jsonc": "^2.0.0",
|
|
64
64
|
"log4js": "^6.9.1",
|
|
65
|
+
"lossless-json": "^4.0.1",
|
|
65
66
|
"mime": "^3.0.0",
|
|
66
67
|
"mongodb": "^6.7.0",
|
|
67
68
|
"mssql": "^11.0.0",
|