cgserver 13.2.1 → 13.2.3

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
@@ -1,4 +1,8 @@
1
1
  # 版本更新日志
2
+ ## v13.2.3
3
+ - Improve MongoDB connection validation and error handling
4
+ ## v13.2.2
5
+ - Fix MongoDB connection initialization logic
2
6
  ## v13.2.1
3
7
  - Fix MongoManager to use correct connection for stats
4
8
  ## v13.2.0
@@ -23,11 +23,15 @@ class MongoBaseService {
23
23
  if (this._model) {
24
24
  return this._model;
25
25
  }
26
- if (mongoose_1.default.connection.readyState !== 1) {
26
+ if (!this.mongo) {
27
+ Log_1.gLog.error("MongoDB connection is not established, please check the connection settings.");
28
+ throw new Error("MongoDB connection is not established");
29
+ }
30
+ if (!this.mongo.connected) {
27
31
  Log_1.gLog.error("MongoDB connection is not ready, please check the connection settings. Current state: " + mongoose_1.default.connection.readyState);
28
32
  throw new Error("MongoDB connection is not ready");
29
33
  }
30
- let db = mongoose_1.default.connection.db;
34
+ let db = this.mongo.connection.db;
31
35
  if (!db || db.databaseName == "test") {
32
36
  Log_1.gLog.error("MongoDB connection is not valid, please check the connection settings.");
33
37
  throw new Error("MongoDB connection is not valid");
@@ -122,6 +122,9 @@ class MongoExt {
122
122
  get connection() {
123
123
  return this._connection;
124
124
  }
125
+ get connected() {
126
+ return this._connection && this._connection.readyState === 1;
127
+ }
125
128
  constructor() {
126
129
  }
127
130
  async init(cfg) {
@@ -135,13 +138,14 @@ class MongoExt {
135
138
  this._inited = true;
136
139
  Log_1.gLog.info("mongo config=" + JSON.stringify(this._mongocfg));
137
140
  this._mongocfg.options.dbName = this._mongocfg.database;
138
- this._connection = await mongoose_2.default.createConnection("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
141
+ this._connection = mongoose_2.default.createConnection("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
142
+ this._connection = this._connection.useDb(this._mongocfg.database);
139
143
  this._connection.on("open", this.onOpen.bind(this));
140
144
  this._connection.on("close", this.onClose.bind(this));
141
- this._connection.useDb(this._mongocfg.database);
142
145
  this._connection.on("connectionCreated", this.onConnect.bind(this));
143
146
  this._connection.on("connectionClosed", this.onDisconnect.bind(this));
144
- console.log("mongo connect success! db=" + this._connection.db.databaseName);
147
+ await this._connection.asPromise();
148
+ console.log("mongo connect success! db=" + this._connection.name);
145
149
  return true;
146
150
  }
147
151
  onConnect() {
@@ -60,6 +60,7 @@ export declare class MongoExt {
60
60
  get debug(): boolean;
61
61
  protected _connection: mongoose.Connection;
62
62
  get connection(): mongoose.Connection;
63
+ get connected(): boolean;
63
64
  constructor();
64
65
  init(cfg: MongoConfig): Promise<boolean>;
65
66
  onConnect(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "13.2.1",
3
+ "version": "13.2.3",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",