cgserver 12.3.6 → 13.0.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.
Files changed (39) hide show
  1. package/dist/lib/Framework/Config/_error_.js +33 -24
  2. package/dist/lib/Framework/Database/Mongo/MongoBaseService.js +151 -128
  3. package/dist/lib/Framework/Database/Mongo/MongoManager.js +18 -561
  4. package/dist/lib/Framework/Decorator/JsonAuthorityValidate.js +1 -5
  5. package/dist/lib/Framework/Logic/Log.js +20 -5
  6. package/dist/lib/Framework/Server/WebServer/Controller/MongoBaseUserController.js +11 -25
  7. package/dist/lib/Framework/Server/WebServer/Controller/MysqlBaseUserController.js +0 -26
  8. package/dist/lib/Framework/Service/MongoAccountService.js +53 -116
  9. package/dist/lib/Framework/Service/MongoCacheService.js +20 -19
  10. package/dist/lib/Framework/Service/MongoUserService.js +39 -77
  11. package/dist/lib/Framework/Service/MysqlUserService.js +3 -12
  12. package/dist/lib/Framework/Service/ini.js +4 -10
  13. package/dist/lib/Framework/global.js +0 -2
  14. package/dist/lib/Framework/index_export_.js +2 -21
  15. package/dist/types/Framework/Config/_error_.d.ts +28 -92
  16. package/dist/types/Framework/Database/Mongo/MongoBaseService.d.ts +35 -113
  17. package/dist/types/Framework/Database/Mongo/MongoManager.d.ts +4 -145
  18. package/dist/types/Framework/Decorator/JsonAuthorityValidate.d.ts +1 -2
  19. package/dist/types/Framework/Logic/Log.d.ts +5 -5
  20. package/dist/types/Framework/Server/WebServer/Controller/MongoBaseUserController.d.ts +4 -8
  21. package/dist/types/Framework/Server/WebServer/Controller/MysqlBaseUserController.d.ts +0 -4
  22. package/dist/types/Framework/Service/MongoAccountService.d.ts +15 -17
  23. package/dist/types/Framework/Service/MongoCacheService.d.ts +5 -4
  24. package/dist/types/Framework/Service/MongoUserService.d.ts +14 -26
  25. package/dist/types/Framework/Service/MysqlUserService.d.ts +2 -4
  26. package/dist/types/Framework/Service/ini.d.ts +3 -8
  27. package/dist/types/Framework/global.d.ts +0 -1
  28. package/dist/types/Framework/index_export_.d.ts +5 -12
  29. package/package.json +2 -1
  30. package/dist/lib/Framework/Database/Mongo/MongoServiceManager.js +0 -52
  31. package/dist/lib/Framework/Decorator/AdminValidate.js +0 -18
  32. package/dist/lib/Framework/Decorator/AuthorityValidate.js +0 -22
  33. package/dist/lib/Framework/Decorator/CreatorValidate.js +0 -18
  34. package/dist/lib/Framework/Decorator/JsonCreatorValidate.js +0 -18
  35. package/dist/types/Framework/Database/Mongo/MongoServiceManager.d.ts +0 -13
  36. package/dist/types/Framework/Decorator/AdminValidate.d.ts +0 -1
  37. package/dist/types/Framework/Decorator/AuthorityValidate.d.ts +0 -2
  38. package/dist/types/Framework/Decorator/CreatorValidate.d.ts +0 -1
  39. package/dist/types/Framework/Decorator/JsonCreatorValidate.d.ts +0 -1
@@ -1,34 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FrameworkErrorCode = exports.EErrorCode = void 0;
3
+ exports.FrameworkErrorCode = exports.Errcode = exports.EErrorCode = void 0;
4
4
  exports.EErrorCode = null;
5
+ class Errcode {
6
+ id;
7
+ des;
8
+ constructor(id, des) {
9
+ this.id = id;
10
+ this.des = des;
11
+ }
12
+ }
13
+ exports.Errcode = Errcode;
5
14
  /**
6
15
  * 1-100
7
16
  */
8
17
  class FrameworkErrorCode {
9
- Wrong_Params = { id: 1, des: "参数错误" };
10
- Verify_failed = { id: 2, des: "验证失败" };
11
- Account_Create_Failed = { id: 3, des: "账号创建失败" };
12
- User_Create_Failed = { id: 4, des: "角色创建失败" };
13
- Server_Error = { id: 5, des: "服务器内部错误" };
14
- No_Account = { id: 6, des: "账户不存在" };
15
- Mysql_Error = { id: 7, des: "数据库异常" };
16
- No_Mysql = { id: 8, des: "未开通Mysql数据库" };
17
- No_Mssql = { id: 8, des: "未开通Mssql数据库" };
18
- No_User = { id: 9, des: "用户不存在" };
19
- Account_Phone_Exist = { id: 10, des: "电话已存在" };
20
- Account_Email_Exist = { id: 11, des: "邮箱已存在" };
21
- Account_Name_Exist = { id: 12, des: "账户名已存在" };
22
- Account_Type_Error = { id: 13, des: "账户类型错误" };
23
- Email_Send_Failed = { id: 14, des: "邮件发送失败" };
24
- Login_Failed = { id: 15, des: "登陆验证失败" };
25
- Password_Too_Short = { id: 16, des: "密码太短" };
26
- Wrong_Phone = { id: 17, des: "电话号码不正确" };
27
- PhoneCode_Too_Quick = { id: 18, des: "验证码获取过快,请稍后再试" };
28
- PhoneCode_GetFailed = { id: 19, des: "验证码获取失败" };
29
- Wrong_Phone_Code = { id: 20, des: "验证码不正确" };
30
- No_Mongo = { id: 21, des: "未开通Mongo数据库" };
31
- Mongo_Error = { id: 22, des: "Mongo数据库异常" };
18
+ Wrong_Params = new Errcode(1, "参数错误");
19
+ Verify_failed = new Errcode(2, "验证失败");
20
+ Account_Create_Failed = new Errcode(3, "账号创建失败");
21
+ User_Create_Failed = new Errcode(4, "角色创建失败");
22
+ Server_Error = new Errcode(5, "服务器内部错误");
23
+ No_Account = new Errcode(6, "账户不存在");
24
+ Mysql_Error = new Errcode(7, "数据库异常");
25
+ No_Mysql = new Errcode(8, "未开通Mysql数据库");
26
+ No_Mssql = new Errcode(8, "未开通Mssql数据库");
27
+ No_User = new Errcode(9, "用户不存在");
28
+ Account_Phone_Exist = new Errcode(10, "电话已存在");
29
+ Account_Email_Exist = new Errcode(11, "邮箱已存在");
30
+ Account_Name_Exist = new Errcode(12, "账户名已存在");
31
+ Account_Type_Error = new Errcode(13, "账户类型错误");
32
+ Email_Send_Failed = new Errcode(14, "邮件发送失败");
33
+ Login_Failed = new Errcode(15, "登陆验证失败");
34
+ Password_Too_Short = new Errcode(16, "密码太短");
35
+ Wrong_Phone = new Errcode(17, "电话号码不正确");
36
+ PhoneCode_Too_Quick = new Errcode(18, "验证码获取过快,请稍后再试");
37
+ PhoneCode_GetFailed = new Errcode(19, "验证码获取失败");
38
+ Wrong_Phone_Code = new Errcode(20, "验证码不正确");
39
+ No_Mongo = new Errcode(21, "未开通Mongo数据库");
40
+ Mongo_Error = new Errcode(22, "Mongo数据库异常");
32
41
  constructor() { }
33
42
  }
34
43
  exports.FrameworkErrorCode = FrameworkErrorCode;
@@ -1,145 +1,168 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.MongoBaseService = void 0;
7
+ const mongoose_1 = __importDefault(require("mongoose"));
8
+ const _error_1 = require("../../Config/_error_");
9
+ const Log_1 = require("../../Logic/Log");
4
10
  const MongoManager_1 = require("./MongoManager");
5
- const MongoServiceManager_1 = require("./MongoServiceManager");
6
11
  class MongoBaseService {
7
- _table = "";
8
- get table() {
9
- return this._table;
12
+ _model;
13
+ _schema;
14
+ _collection_name;
15
+ get model() {
16
+ if (this._model) {
17
+ return this._model;
18
+ }
19
+ if (mongoose_1.default.connection.readyState !== 1) {
20
+ Log_1.gLog.error("MongoDB connection is not ready, please check the connection settings. Current state: " + mongoose_1.default.connection.readyState);
21
+ throw new Error("MongoDB connection is not ready");
22
+ }
23
+ let db = mongoose_1.default.connection.db;
24
+ if (!db || db.databaseName == "test") {
25
+ Log_1.gLog.error("MongoDB connection is not valid, please check the connection settings.");
26
+ throw new Error("MongoDB connection is not valid");
27
+ }
28
+ this._model = mongoose_1.default.model(this._collection_name, this._schema);
29
+ return this._model;
30
+ }
31
+ constructor(collection_name, schema) {
32
+ this._collection_name = collection_name;
33
+ schema.set('collection', collection_name);
34
+ this._schema = schema;
10
35
  }
11
- _inited = false;
12
- get isInited() {
13
- return this._inited;
36
+ async findOne(filter, projection, options) {
37
+ if (!this.model) {
38
+ throw new Error("Model is not defined");
39
+ }
40
+ return await this.model.findOne(filter, projection, options);
41
+ }
42
+ async find(filter, projection, options) {
43
+ if (!this.model) {
44
+ throw new Error("Model is not defined");
45
+ }
46
+ return await this.model.find(filter, projection, options);
14
47
  }
15
- get mongoDb() {
16
- return MongoManager_1.gMongoMgr.getMongo(this._dbname);
48
+ async findMany(filter, projection, options, skip, limit) {
49
+ if (!this.model) {
50
+ throw new Error("Model is not defined");
51
+ }
52
+ let query = this.model.find(filter, projection, options);
53
+ if (skip !== undefined) {
54
+ query = query.skip(skip);
55
+ }
56
+ if (limit !== undefined) {
57
+ query = query.limit(limit);
58
+ }
59
+ return await query.exec();
17
60
  }
18
- _dbname = "";
19
- get dbname() {
20
- if (!this._dbname) {
21
- this._dbname = MongoManager_1.gMongoMgr.defdbname;
61
+ async findById(id, projection, options) {
62
+ if (!this.model) {
63
+ throw new Error("Model is not defined");
22
64
  }
23
- return this._dbname;
65
+ return await this.model.findById(id, projection, options);
24
66
  }
25
- set dbname(db) {
26
- this._dbname = db;
67
+ async create(doc) {
68
+ if (!this.model) {
69
+ throw new Error("Model is not defined");
70
+ }
71
+ const newDoc = new this.model(doc);
72
+ return await newDoc.save();
73
+ }
74
+ async insert(doc) {
75
+ try {
76
+ if (!this.model) {
77
+ throw new Error("Model is not defined");
78
+ }
79
+ const newDoc = await this.model.insertOne(doc);
80
+ return { errcode: null, model: newDoc };
81
+ }
82
+ catch (error) {
83
+ Log_1.gLog.error(arguments);
84
+ Log_1.gLog.error("MongoDB insert error:", error.stack);
85
+ return { errcode: _error_1.EErrorCode.Mongo_Error, model: null };
86
+ }
27
87
  }
28
- _t_type = null;
29
- constructor(table, type, dbname = "") {
30
- this._t_type = type;
31
- this._table = table;
32
- this._dbname = dbname;
33
- MongoServiceManager_1.gMongoServiceMgr.addService(this);
88
+ async updateOne(filter, update, options) {
89
+ try {
90
+ if (!this.model) {
91
+ throw new Error("Model is not defined");
92
+ }
93
+ let rs = await this.model.updateOne(filter, update, options);
94
+ return { errcode: null, rs: rs };
95
+ }
96
+ catch (error) {
97
+ return { errcode: _error_1.EErrorCode.Mongo_Error, rs: null };
98
+ }
34
99
  }
35
- async getNextId(key = "") {
36
- if (!key) {
37
- key = this._table;
100
+ async updateMany(filter, update, options) {
101
+ try {
102
+ if (!this.model) {
103
+ throw new Error("Model is not defined");
104
+ }
105
+ await this.model.updateMany(filter, update, options);
106
+ return { errcode: null };
38
107
  }
39
- let id = await this.mongoDb.getAutoIds(this._table);
40
- return id;
108
+ catch (error) {
109
+ return { errcode: error };
110
+ }
111
+ }
112
+ async deleteOne(filter) {
113
+ try {
114
+ if (!this.model) {
115
+ throw new Error("Model is not defined");
116
+ }
117
+ let rs = await this.model.deleteOne(filter);
118
+ return { errcode: null, rs: rs };
119
+ }
120
+ catch (error) {
121
+ return { errcode: _error_1.EErrorCode.Mongo_Error, rs: null };
122
+ }
123
+ }
124
+ async deleteMany(filter) {
125
+ try {
126
+ if (!this.model) {
127
+ throw new Error("Model is not defined");
128
+ }
129
+ await this.model.deleteMany(filter);
130
+ return { errcode: null };
131
+ }
132
+ catch (error) {
133
+ return { errcode: error };
134
+ }
135
+ }
136
+ async exists(filter) {
137
+ if (!this.model) {
138
+ throw new Error("Model is not defined");
139
+ }
140
+ const doc = await this.model.findOne(filter, { _id: 1 });
141
+ return !!doc;
142
+ }
143
+ // 用于聚合查询
144
+ aggregate(pipeline) {
145
+ if (!this.model) {
146
+ throw new Error("Model is not defined");
147
+ }
148
+ return this.model.aggregate(pipeline);
149
+ }
150
+ // findOneAndUpdate method for MongoDB operations
151
+ async findOneAndUpdate(filter, update, options) {
152
+ if (!this.model) {
153
+ throw new Error("Model is not defined");
154
+ }
155
+ return await this.model.findOneAndUpdate(filter, update, options);
156
+ }
157
+ async countDocuments(filter) {
158
+ if (!this.model) {
159
+ throw new Error("Model is not defined");
160
+ }
161
+ return await this.model.countDocuments(filter);
41
162
  }
42
- toObjectId(id) {
43
- return this.mongoDb.toObjectId(id);
44
- }
45
- /**
46
- * 没有id(非_id)的表不能使用该函数
47
- * @param id
48
- */
49
- async getById(id) {
50
- let rs = await this.mongoDb.findOne(this._table, { id: id });
51
- return rs.one;
52
- }
53
- async findOne(where = null, property = null) {
54
- let rs = await this.mongoDb.findOne(this._table, where, property);
55
- return rs.one;
56
- }
57
- async findOneAndUpdate(where = null, update, options) {
58
- let rs = await this.mongoDb.findOneAndUpdate(this._table, where, update, options);
59
- return rs;
60
- }
61
- async countDocuments(where = null, options) {
62
- let rs = await this.mongoDb.countDocuments(this._table, where);
63
- return rs.count;
64
- }
65
- async findMany(where = null, property = null, sort = null, skip = 0, limit = 0) {
66
- let rs = await this.mongoDb.findMany(this._table, where, property, sort, skip, limit);
67
- return rs.list;
68
- }
69
- async getRandoms(num, where, property = null) {
70
- let rs = await this.mongoDb.simpleAggregate(this._table, where, property, null, num);
71
- return rs.list;
72
- }
73
- async updateOne(where, model, options) {
74
- let rs = await this.mongoDb.updateOne(this._table, where, model, options);
75
- return rs;
76
- }
77
- async updateMany(where, model = null, options) {
78
- let rs = await this.mongoDb.updateMany(this._table, where, model, options);
79
- return rs;
80
- }
81
- async insert(model) {
82
- let rs = await this.mongoDb.insertOne(this._table, model);
83
- return rs;
84
- }
85
- async deleteOne(where) {
86
- let rs = await this.mongoDb.deleteOne(this._table, where);
87
- return rs;
88
- }
89
- async deleteMany(where) {
90
- let rs = await this.mongoDb.deleteMany(this._table, where);
91
- return rs;
92
- }
93
- async createIndex(index, options) {
94
- let rs = await this.mongoDb.createIndex(this._table, index, options);
95
- return rs;
96
- }
97
- aggregate(pipeline, options) {
98
- let ret = this.mongoDb.aggregate(this._table, pipeline, options);
99
- return ret;
100
- }
101
- /**
102
- * 仅仅支持一级
103
- * @param array 数据名称 比如 items
104
- * @param where 数组内赛选条件 比如 "items.id":1
105
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
106
- */
107
- async getsInArray(array, where, pre_match) {
108
- let agg = this.aggregate();
109
- if (pre_match) {
110
- agg = agg.match(pre_match);
111
- }
112
- agg = agg.unwind("$" + array);
113
- if (where) {
114
- agg = agg.match(where);
115
- }
116
- let all = await agg.toArray();
117
- let items = [];
118
- for (let i = 0; i < all.length; ++i) {
119
- items.push(all[i][array]);
120
- }
121
- return items;
122
- }
123
- /**
124
- * 仅仅支持一级
125
- * @param array 数据名称 比如 items
126
- * @param where 数组内赛选条件 比如 "items.id":1
127
- * @param pre_match 数组上一级赛选条件 比如 "user_id":1
128
- */
129
- async getInArray(array, where, pre_match) {
130
- let items = await this.getsInArray(array, where, pre_match);
131
- if (items.length <= 0) {
132
- return null;
133
- }
134
- return items[0];
135
- }
136
- async distinct(key, where = {}) {
137
- let rs = await this.mongoDb.distinct(this._table, key, where);
138
- return rs;
139
- }
140
- async bulkWrite(operations, options) {
141
- let bwr = await this.mongoDb.bulkWrite(this._table, operations, options);
142
- return bwr;
163
+ async getAutoIds() {
164
+ let id = await MongoManager_1.gMongoMgr.getMongo().getAutoIds(this.model.collection.name);
165
+ return id;
143
166
  }
144
167
  }
145
168
  exports.MongoBaseService = MongoBaseService;