cgserver 8.4.1 → 8.6.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,8 +11,8 @@ class MongoBaseService {
11
11
  get isInited() {
12
12
  return this._inited;
13
13
  }
14
- get mongo() {
15
- return MongoManager_1.GMongoMgr.mongo;
14
+ get mongoDb() {
15
+ return MongoManager_1.GMongoMgr.mongoDb;
16
16
  }
17
17
  _t_type = null;
18
18
  constructor(table, type) {
@@ -47,9 +47,13 @@ exports.GMongoMgr = null;
47
47
  class MongoManager {
48
48
  _mongocfg = null;
49
49
  _init_cbs = [];
50
- _mongo = null;
51
- get mongo() {
52
- return this._mongo;
50
+ _mongoDb = null;
51
+ get mongoDb() {
52
+ return this._mongoDb;
53
+ }
54
+ _mongoClient = null;
55
+ get mongoClient() {
56
+ return this._mongoClient;
53
57
  }
54
58
  _mongo_init_succ = false;
55
59
  _inited = false;
@@ -65,7 +69,7 @@ class MongoManager {
65
69
  if (this._inited) {
66
70
  return false;
67
71
  }
68
- if (this._mongo) {
72
+ if (this._mongoDb) {
69
73
  return true;
70
74
  }
71
75
  this._mongocfg = cfg;
@@ -74,7 +78,7 @@ class MongoManager {
74
78
  let client = new mongo.MongoClient("mongodb://" + this._mongocfg.host + ":" + this._mongocfg.port, this._mongocfg.options);
75
79
  await Core_1.core.safeCall(client.connect, client);
76
80
  this.onConnect();
77
- this._mongo = client.db(this._mongocfg.database);
81
+ this._mongoDb = client.db(this._mongocfg.database);
78
82
  for (let i = 0; i < this._init_cbs.length; ++i) {
79
83
  this._init_cbs[i]();
80
84
  }
@@ -104,11 +108,11 @@ class MongoManager {
104
108
  * @returns 小于等于0为异常
105
109
  */
106
110
  async getAutoIds(key) {
107
- if (!this._mongo) {
111
+ if (!this._mongoDb) {
108
112
  return -1;
109
113
  }
110
114
  let collection = "auto_ids";
111
- let col = this._mongo.collection(collection);
115
+ let col = this._mongoDb.collection(collection);
112
116
  try {
113
117
  let where = this._convertWhere({ _id: key });
114
118
  let rs = await Core_1.core.safeCall(col.findOneAndUpdate, col, where, { $inc: { id: 1 } }, { upsert: true });
@@ -147,13 +151,13 @@ class MongoManager {
147
151
  async findOne(collection, property = {}, where = {}) {
148
152
  this._convertWhere(where);
149
153
  let rs = { errcode: null, one: null };
150
- if (!this._mongo) {
154
+ if (!this._mongoDb) {
151
155
  rs.errcode = _error_1.EErrorCode.No_Mongo;
152
156
  return rs;
153
157
  }
154
158
  let one = null;
155
159
  try {
156
- let col = this._mongo.collection(collection);
160
+ let col = this._mongoDb.collection(collection);
157
161
  one = await col.findOne(where, { projection: property });
158
162
  }
159
163
  catch (e) {
@@ -167,13 +171,13 @@ class MongoManager {
167
171
  async findMany(collection, property = {}, where = {}, sort, skip = 0, limit = 0) {
168
172
  this._convertWhere(where);
169
173
  let rs = { errcode: null, list: null };
170
- if (!this._mongo) {
174
+ if (!this._mongoDb) {
171
175
  rs.errcode = _error_1.EErrorCode.No_Mongo;
172
176
  return rs;
173
177
  }
174
178
  let list = [];
175
179
  try {
176
- let col = this._mongo.collection(collection);
180
+ let col = this._mongoDb.collection(collection);
177
181
  let cursor = col.find(where, property);
178
182
  if (sort) {
179
183
  cursor = cursor.sort(sort);
@@ -197,13 +201,13 @@ class MongoManager {
197
201
  async countDocuments(collection, where, options) {
198
202
  this._convertWhere(where);
199
203
  let rs = { errcode: null, count: -1 };
200
- if (!this._mongo) {
204
+ if (!this._mongoDb) {
201
205
  rs.errcode = _error_1.EErrorCode.No_Mongo;
202
206
  return rs;
203
207
  }
204
208
  let count = -1;
205
209
  try {
206
- let col = this._mongo.collection(collection);
210
+ let col = this._mongoDb.collection(collection);
207
211
  count = await col.countDocuments(where || {}, options);
208
212
  }
209
213
  catch (e) {
@@ -217,13 +221,13 @@ class MongoManager {
217
221
  async deleteOne(collection, where) {
218
222
  this._convertWhere(where);
219
223
  let rs = { errcode: null, count: -1 };
220
- if (!this._mongo) {
224
+ if (!this._mongoDb) {
221
225
  rs.errcode = _error_1.EErrorCode.No_Mongo;
222
226
  return rs;
223
227
  }
224
228
  let del_rs = null;
225
229
  try {
226
- let col = this._mongo.collection(collection);
230
+ let col = this._mongoDb.collection(collection);
227
231
  del_rs = await col.deleteOne(where || {});
228
232
  }
229
233
  catch (e) {
@@ -239,13 +243,13 @@ class MongoManager {
239
243
  async deleteMany(collection, where) {
240
244
  this._convertWhere(where);
241
245
  let rs = { errcode: null, count: -1 };
242
- if (!this._mongo) {
246
+ if (!this._mongoDb) {
243
247
  rs.errcode = _error_1.EErrorCode.No_Mongo;
244
248
  return rs;
245
249
  }
246
250
  let del_rs = null;
247
251
  try {
248
- let col = this._mongo.collection(collection);
252
+ let col = this._mongoDb.collection(collection);
249
253
  del_rs = await col.deleteMany(where || {});
250
254
  }
251
255
  catch (e) {
@@ -265,13 +269,13 @@ class MongoManager {
265
269
  */
266
270
  async insertOne(collection, data) {
267
271
  let rs = { errcode: null, rs: null };
268
- if (!this._mongo) {
272
+ if (!this._mongoDb) {
269
273
  rs.errcode = _error_1.EErrorCode.No_Mongo;
270
274
  return rs;
271
275
  }
272
276
  let in_rs = null;
273
277
  try {
274
- let col = this._mongo.collection(collection);
278
+ let col = this._mongoDb.collection(collection);
275
279
  in_rs = await col.insertOne(data);
276
280
  }
277
281
  catch (e) {
@@ -284,13 +288,13 @@ class MongoManager {
284
288
  }
285
289
  async insertManay(collection, data) {
286
290
  let rs = { errcode: null, rs: null };
287
- if (!this._mongo) {
291
+ if (!this._mongoDb) {
288
292
  rs.errcode = _error_1.EErrorCode.No_Mongo;
289
293
  return rs;
290
294
  }
291
295
  let in_rs = null;
292
296
  try {
293
- let col = this._mongo.collection(collection);
297
+ let col = this._mongoDb.collection(collection);
294
298
  in_rs = await col.insertMany(data);
295
299
  }
296
300
  catch (e) {
@@ -309,7 +313,7 @@ class MongoManager {
309
313
  }
310
314
  this._convertWhere(where);
311
315
  let rs = { errcode: null, rs: null };
312
- if (!this._mongo) {
316
+ if (!this._mongoDb) {
313
317
  if (_id) {
314
318
  model["_id"] = _id;
315
319
  }
@@ -331,7 +335,7 @@ class MongoManager {
331
335
  else {
332
336
  updatemodel = model;
333
337
  }
334
- let col = this._mongo.collection(collection);
338
+ let col = this._mongoDb.collection(collection);
335
339
  up_rs = await col.updateOne(where, updatemodel, { upsert: upsert });
336
340
  }
337
341
  catch (e) {
@@ -348,7 +352,7 @@ class MongoManager {
348
352
  async updateMany(collection, model, where, upsert = false) {
349
353
  this._convertWhere(where);
350
354
  let rs = { errcode: null, rs: null };
351
- if (!this._mongo) {
355
+ if (!this._mongoDb) {
352
356
  rs.errcode = _error_1.EErrorCode.No_Mongo;
353
357
  return rs;
354
358
  }
@@ -362,7 +366,7 @@ class MongoManager {
362
366
  else {
363
367
  updateModel = model;
364
368
  }
365
- let col = this._mongo.collection(collection);
369
+ let col = this._mongoDb.collection(collection);
366
370
  up_rs = await col.updateMany(where, updateModel, { upsert: upsert });
367
371
  }
368
372
  catch (e) {
@@ -375,13 +379,13 @@ class MongoManager {
375
379
  }
376
380
  async createIndex(collection, index, options) {
377
381
  let rs = { errcode: null, rs: null };
378
- if (!this._mongo) {
382
+ if (!this._mongoDb) {
379
383
  rs.errcode = _error_1.EErrorCode.No_Mongo;
380
384
  return rs;
381
385
  }
382
386
  let i_rs = null;
383
387
  try {
384
- let col = this._mongo.collection(collection);
388
+ let col = this._mongoDb.collection(collection);
385
389
  i_rs = await col.createIndex(index, options);
386
390
  }
387
391
  catch (e) {
@@ -395,13 +399,13 @@ class MongoManager {
395
399
  async simpleAggregate(collection, property = {}, where = {}, size, random_size) {
396
400
  this._convertWhere(where);
397
401
  let rs = { errcode: null, list: null };
398
- if (!this._mongo) {
402
+ if (!this._mongoDb) {
399
403
  rs.errcode = _error_1.EErrorCode.No_Mongo;
400
404
  return rs;
401
405
  }
402
406
  let list = [];
403
407
  try {
404
- let col = this._mongo.collection(collection);
408
+ let col = this._mongoDb.collection(collection);
405
409
  let params = [];
406
410
  params.push({ '$match': where || {} });
407
411
  params.push({ '$project': property || {} });
@@ -424,12 +428,36 @@ class MongoManager {
424
428
  return rs;
425
429
  }
426
430
  aggregate(collection, pipeline, options) {
427
- if (!this._mongo) {
431
+ if (!this._mongoDb) {
428
432
  return;
429
433
  }
430
- let col = this._mongo.collection(collection);
434
+ let col = this._mongoDb.collection(collection);
431
435
  let agg = col.aggregate(pipeline, options);
432
436
  return agg;
433
437
  }
438
+ /**
439
+ * 快速事务
440
+ * @param collection
441
+ * @param cb
442
+ */
443
+ async quickTransaction(cb) {
444
+ if (!this._mongoDb) {
445
+ return false;
446
+ }
447
+ let session = this._mongoClient.startSession();
448
+ session.startTransaction();
449
+ try {
450
+ let rs = await cb(session);
451
+ await session.commitTransaction();
452
+ session.endSession();
453
+ return rs;
454
+ }
455
+ catch (e) {
456
+ await session.abortTransaction();
457
+ session.endSession();
458
+ Log_1.GLog.error(e.stack);
459
+ }
460
+ return false;
461
+ }
434
462
  }
435
463
  exports.GMongoMgr = new MongoManager();
@@ -38,8 +38,8 @@ exports.MongoUserModel = MongoUserModel;
38
38
  //暂时不实例化,方便重写
39
39
  exports.GUserSer = null;
40
40
  class UserService extends MongoBaseService_1.MongoBaseService {
41
- constructor(type) {
42
- super("user", type);
41
+ constructor(table, type) {
42
+ super(table, type);
43
43
  exports.GUserSer = this;
44
44
  }
45
45
  async _createNewUser(account_id, nickname, sex, logo, group) {
@@ -4,7 +4,7 @@ export declare class MongoBaseService<T> {
4
4
  get table(): string;
5
5
  protected _inited: boolean;
6
6
  get isInited(): boolean;
7
- get mongo(): mongo.Db;
7
+ get mongoDb(): mongo.Db;
8
8
  protected _t_type: {
9
9
  new (): T;
10
10
  };
@@ -39,8 +39,10 @@ export declare let GMongoMgr: MongoManager;
39
39
  declare class MongoManager {
40
40
  protected _mongocfg: MongoConfig;
41
41
  protected _init_cbs: any[];
42
- protected _mongo: mongo.Db;
43
- get mongo(): mongo.Db;
42
+ protected _mongoDb: mongo.Db;
43
+ get mongoDb(): mongo.Db;
44
+ protected _mongoClient: mongo.MongoClient;
45
+ get mongoClient(): mongo.MongoClient;
44
46
  protected _mongo_init_succ: boolean;
45
47
  protected _inited: boolean;
46
48
  get isValid(): boolean;
@@ -144,5 +146,11 @@ declare class MongoManager {
144
146
  list: any[];
145
147
  }>;
146
148
  aggregate(collection: string, pipeline?: Document[], options?: mongo.AggregateOptions): mongo.AggregationCursor<mongo.BSON.Document>;
149
+ /**
150
+ * 快速事务
151
+ * @param collection
152
+ * @param cb
153
+ */
154
+ quickTransaction(cb: Function): Promise<false | any>;
147
155
  }
148
156
  export {};
@@ -25,7 +25,7 @@ export declare class MongoUserModel extends MongoBaseModel {
25
25
  }
26
26
  export declare let GUserSer: UserService<MongoUserModel>;
27
27
  export declare class UserService<T extends MongoUserModel> extends MongoBaseService<T> {
28
- constructor(type: {
28
+ constructor(table: string, type: {
29
29
  new (): T;
30
30
  });
31
31
  protected _createNewUser(account_id: number, nickname: string, sex: number, logo: string, group?: ERoleGroup): Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "8.4.1",
3
+ "version": "8.6.0",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",