cgserver 13.2.2 → 13.2.4

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,6 @@
1
1
  # 版本更新日志
2
+ ## v13.2.3
3
+ - Improve MongoDB connection validation and error handling
2
4
  ## v13.2.2
3
5
  - Fix MongoDB connection initialization logic
4
6
  ## v13.2.1
@@ -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) {
@@ -137,11 +140,18 @@ class MongoExt {
137
140
  this._mongocfg.options.dbName = this._mongocfg.database;
138
141
  this._connection = mongoose_2.default.createConnection("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
139
142
  this._connection = this._connection.useDb(this._mongocfg.database);
140
- this._connection.on("open", this.onOpen.bind(this));
143
+ this._connection.once("open", this.onOpen.bind(this));
141
144
  this._connection.on("close", this.onClose.bind(this));
142
145
  this._connection.on("connectionCreated", this.onConnect.bind(this));
143
146
  this._connection.on("connectionClosed", this.onDisconnect.bind(this));
144
147
  await this._connection.asPromise();
148
+ while (this._connection.readyState != mongoose_1.ConnectionStates.connected) {
149
+ if (this._connection.readyState != mongoose_1.ConnectionStates.connecting) {
150
+ Log_1.gLog.error("MongoDB connection is not ready, please check the connection settings. Current state: " + this._connection.readyState);
151
+ return false;
152
+ }
153
+ await Core_1.core.sleep(100);
154
+ }
145
155
  console.log("mongo connect success! db=" + this._connection.name);
146
156
  return true;
147
157
  }
@@ -152,7 +162,10 @@ class MongoExt {
152
162
  this._cur_connecting_count--;
153
163
  }
154
164
  close(force = false) {
155
- mongoose_2.default.connection.close(force);
165
+ if (!this._connection) {
166
+ return;
167
+ }
168
+ this._connection.close(force);
156
169
  }
157
170
  registerInitCb(cb) {
158
171
  this._init_cbs.push(cb);
@@ -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.2",
3
+ "version": "13.2.4",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",