cgserver 13.1.7 → 13.2.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.
@@ -11,6 +11,14 @@ class MongoBaseService {
11
11
  _model;
12
12
  _schema;
13
13
  _collection_name;
14
+ _dbname = "";
15
+ _mongo = null;
16
+ get mongo() {
17
+ if (!this._mongo) {
18
+ this._mongo = MongoManager_1.gMongoMgr.getMongo(this._dbname);
19
+ }
20
+ return this._mongo;
21
+ }
14
22
  get model() {
15
23
  if (this._model) {
16
24
  return this._model;
@@ -27,68 +35,69 @@ class MongoBaseService {
27
35
  this._model = mongoose_1.default.model(this._collection_name, this._schema);
28
36
  return this._model;
29
37
  }
30
- constructor(collection_name, schema) {
38
+ constructor(collection_name, schema, dbname) {
31
39
  this._collection_name = collection_name;
32
40
  schema.set('collection', collection_name);
33
41
  this._schema = schema;
34
42
  this._schema.set('minimize', false);
43
+ this._dbname = dbname;
35
44
  }
36
45
  async findOne(filter, projection, options) {
37
- let ret = await MongoManager_1.gMongoMgr.getMongo().findOne(this.model, filter, projection, options);
46
+ let ret = await this.mongo.findOne(this.model, filter, projection, options);
38
47
  return ret;
39
48
  }
40
49
  async find(filter, projection, options) {
41
- let ret = await MongoManager_1.gMongoMgr.getMongo().find(this.model, filter, projection, options);
50
+ let ret = await this.mongo.find(this.model, filter, projection, options);
42
51
  return ret;
43
52
  }
44
53
  async findById(id, projection, options) {
45
- let ret = await MongoManager_1.gMongoMgr.getMongo().findById(this.model, id, projection, options);
54
+ let ret = await this.mongo.findById(this.model, id, projection, options);
46
55
  return ret;
47
56
  }
48
57
  async create(doc) {
49
- let ret = await MongoManager_1.gMongoMgr.getMongo().create(this.model, doc);
58
+ let ret = await this.mongo.create(this.model, doc);
50
59
  return ret;
51
60
  }
52
61
  async insert(doc) {
53
- let ret = await MongoManager_1.gMongoMgr.getMongo().insert(this.model, doc);
62
+ let ret = await this.mongo.insert(this.model, doc);
54
63
  return ret;
55
64
  }
56
65
  async updateOne(filter, update, options) {
57
- let ret = await MongoManager_1.gMongoMgr.getMongo().updateOne(this.model, filter, update, options);
66
+ let ret = await this.mongo.updateOne(this.model, filter, update, options);
58
67
  return ret;
59
68
  }
60
69
  async updateMany(filter, update, options) {
61
- let ret = await MongoManager_1.gMongoMgr.getMongo().updateMany(this.model, filter, update, options);
70
+ let ret = await this.mongo.updateMany(this.model, filter, update, options);
62
71
  return ret;
63
72
  }
64
73
  async deleteOne(filter) {
65
- let ret = await MongoManager_1.gMongoMgr.getMongo().deleteOne(this.model, filter);
74
+ let ret = await this.mongo.deleteOne(this.model, filter);
66
75
  return ret;
67
76
  }
68
77
  async deleteMany(filter) {
69
- let ret = await MongoManager_1.gMongoMgr.getMongo().deleteMany(this.model, filter);
78
+ let ret = await this.mongo.deleteMany(this.model, filter);
70
79
  return ret;
71
80
  }
72
81
  async exists(filter) {
73
- let ret = await MongoManager_1.gMongoMgr.getMongo().exists(this.model, filter);
82
+ let ret = await this.mongo.exists(this.model, filter);
74
83
  return ret;
75
84
  }
76
85
  // 用于聚合查询
77
86
  aggregate(pipeline) {
78
- let ret = MongoManager_1.gMongoMgr.getMongo().aggregate(this.model, pipeline);
87
+ let ret = this.mongo.aggregate(this.model, pipeline);
79
88
  return ret;
80
89
  }
81
90
  // findOneAndUpdate method for MongoDB operations
82
91
  async findOneAndUpdate(filter, update, options) {
83
- let ret = await MongoManager_1.gMongoMgr.getMongo().findOneAndUpdate(this.model, filter, update, options);
92
+ let ret = await this.mongo.findOneAndUpdate(this.model, filter, update, options);
84
93
  return ret;
85
94
  }
86
95
  async countDocuments(filter) {
87
- let ret = await MongoManager_1.gMongoMgr.getMongo().countDocuments(this.model, filter);
96
+ let ret = await this.mongo.countDocuments(this.model, filter);
88
97
  return ret;
89
98
  }
90
99
  async getAutoIds() {
91
- let id = await MongoManager_1.gMongoMgr.getMongo().getAutoIds(this.model.collection.name);
100
+ let id = await this.mongo.getAutoIds(this.model.collection.name);
92
101
  return id;
93
102
  }
94
103
  }
@@ -96,6 +96,9 @@ class MongoManager {
96
96
  if (!dbname) {
97
97
  dbname = this._defdbname;
98
98
  }
99
+ if (!dbname) {
100
+ return null;
101
+ }
99
102
  return this._dbs[dbname];
100
103
  }
101
104
  }
@@ -115,6 +118,10 @@ class MongoExt {
115
118
  get debug() {
116
119
  return this._mongocfg.debug;
117
120
  }
121
+ _connection = null;
122
+ get connection() {
123
+ return this._connection;
124
+ }
118
125
  constructor() {
119
126
  }
120
127
  async init(cfg) {
@@ -127,14 +134,14 @@ class MongoExt {
127
134
  this._mongocfg = cfg;
128
135
  this._inited = true;
129
136
  Log_1.gLog.info("mongo config=" + JSON.stringify(this._mongocfg));
130
- mongoose_2.default.connection.on("open", this.onOpen.bind(this));
131
- mongoose_2.default.connection.on("close", this.onClose.bind(this));
132
137
  this._mongocfg.options.dbName = this._mongocfg.database;
133
- let mg = await mongoose_2.default.connect("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
134
- mongoose_2.default.connection.useDb(this._mongocfg.database);
135
- mongoose_2.default.connection.getClient().on("connectionCreated", this.onConnect.bind(this));
136
- mongoose_2.default.connection.getClient().on("connectionClosed", this.onDisconnect.bind(this));
137
- console.log("mongo connect success! db=" + this._mongocfg.database);
138
+ this._connection = await mongoose_2.default.createConnection("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
139
+ this._connection.on("open", this.onOpen.bind(this));
140
+ this._connection.on("close", this.onClose.bind(this));
141
+ this._connection.useDb(this._mongocfg.database);
142
+ this._connection.on("connectionCreated", this.onConnect.bind(this));
143
+ this._connection.on("connectionClosed", this.onDisconnect.bind(this));
144
+ console.log("mongo connect success! db=" + this._connection.db.databaseName);
138
145
  return true;
139
146
  }
140
147
  onConnect() {
@@ -336,6 +343,9 @@ class MongoExt {
336
343
  }
337
344
  }
338
345
  exports.MongoExt = MongoExt;
346
+ __decorate([
347
+ (0, MongoActionCheck_1.MongoActionCheck)(false)
348
+ ], MongoExt.prototype, "init", null);
339
349
  __decorate([
340
350
  (0, MongoActionCheck_1.MongoActionCheck)(-1)
341
351
  ], MongoExt.prototype, "getAutoIds", null);
@@ -1,11 +1,14 @@
1
1
  import mongoose, { FilterQuery, UpdateQuery, Types, MongooseQueryOptions } from 'mongoose';
2
- import { IMongoBaseModel } from './MongoManager';
2
+ import { IMongoBaseModel, MongoExt } from './MongoManager';
3
3
  export declare class MongoBaseService<T extends IMongoBaseModel> {
4
4
  protected _model: mongoose.Model<T>;
5
5
  protected _schema: mongoose.Schema<T>;
6
6
  protected _collection_name: string;
7
+ protected _dbname: string;
8
+ protected _mongo: MongoExt;
9
+ get mongo(): MongoExt;
7
10
  get model(): mongoose.Model<T, {}, {}, {}, mongoose.IfAny<T, any, mongoose.Document<unknown, {}, T, {}> & mongoose.Default__v<mongoose.Require_id<T>>>, any>;
8
- constructor(collection_name: string, schema: mongoose.Schema<T>);
11
+ constructor(collection_name: string, schema: mongoose.Schema<T>, dbname?: string);
9
12
  findOne(filter?: FilterQuery<T>, projection?: any, options?: any): Promise<T | null>;
10
13
  find(filter?: FilterQuery<T>, projection?: mongoose.ProjectionType<T>, options?: MongooseQueryOptions): Promise<T[]>;
11
14
  findById(id: string | Types.ObjectId, projection?: mongoose.ProjectionType<T>, options?: MongooseQueryOptions): Promise<T>;
@@ -47,7 +47,7 @@ export declare class MongoManager {
47
47
  init(cfgs: MongoConfig[]): Promise<boolean>;
48
48
  addMongo(cfg: MongoConfig): Promise<boolean>;
49
49
  removeMongo(dbname: string, force?: boolean): Promise<boolean>;
50
- getMongo(dbname?: string): MongoExt;
50
+ getMongo(dbname?: string): MongoExt | null;
51
51
  }
52
52
  export declare class MongoExt {
53
53
  protected _mongocfg: MongoConfig;
@@ -58,6 +58,8 @@ export declare class MongoExt {
58
58
  get curConnectingCount(): number;
59
59
  get isValid(): boolean;
60
60
  get debug(): boolean;
61
+ protected _connection: mongoose.Connection;
62
+ get connection(): mongoose.Connection;
61
63
  constructor();
62
64
  init(cfg: MongoConfig): Promise<boolean>;
63
65
  onConnect(): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "13.1.7",
3
+ "version": "13.2.0",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",