mm_mongodb 1.5.4 → 1.5.6

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 (3) hide show
  1. package/eslint.config.js +210 -67
  2. package/index.js +24 -43
  3. package/package.json +1 -1
package/eslint.config.js CHANGED
@@ -1,68 +1,211 @@
1
- const jsdoc = require('eslint-plugin-jsdoc');
2
-
3
- module.exports = [
4
- {
5
- files: ['lib/**/*.js'],
6
- languageOptions: {
7
- ecmaVersion: 2020,
8
- sourceType: 'script'
9
- },
10
- plugins: {
11
- jsdoc: jsdoc
12
- },
13
- rules: {
14
- // JSDoc相关规则 - 设置为error级别强制要求
15
- 'jsdoc/require-jsdoc': ['error', {
16
- require: {
17
- FunctionDeclaration: true,
18
- MethodDefinition: true,
19
- ClassDeclaration: true,
20
- ArrowFunctionExpression: true
21
- }
22
- }],
23
- 'jsdoc/check-param-names': 'off',
24
- 'jsdoc/check-tag-names': 'off',
25
- 'jsdoc/check-types': 'off',
26
- 'jsdoc/require-param': 'off',
27
- 'jsdoc/require-param-description': 'off',
28
- 'jsdoc/require-returns': 'off',
29
- 'jsdoc/require-returns-description': 'off',
30
-
31
- // 基础语法规则
32
- 'no-unused-vars': ['error', { args: 'none', vars: 'all' }],
33
- 'no-console': 'warn',
34
-
35
- // 代码风格规则
36
- 'indent': ['error', 2],
37
- 'quotes': ['error', 'single'],
38
- 'semi': ['error', 'always'],
39
- 'comma-dangle': ['error', 'never'],
40
- 'no-trailing-spaces': 'error',
41
- 'eol-last': 'error',
42
- 'max-len': ['error', { code: 100 }],
43
- 'max-lines-per-function': ['error', { max: 40 }],
44
- 'complexity': ['error', 12],
45
- 'max-depth': ['error', 4],
46
- 'max-params': ['error', 4],
47
-
48
- // 命名规则
49
- 'id-length': ['error', {
50
- min: 1,
51
- max: 20,
52
- properties: 'always'
53
- }]
54
- }
55
- },
56
- {
57
- files: ['test/**/*.js'],
58
- languageOptions: {
59
- ecmaVersion: 2020,
60
- sourceType: 'script'
61
- },
62
- rules: {
63
- // 测试文件中允许使用console
64
- 'no-console': 'off',
65
- 'no-unused-vars': 'off'
66
- }
67
- }
1
+ 'use strict';
2
+
3
+ const namingConventionPlugin = require('mm_eslint');
4
+
5
+ /**
6
+ * 基于个人规则的ESLint配置
7
+ * 符合最新ESLint Flat Config格式
8
+ */
9
+
10
+ module.exports = [
11
+ {
12
+ files: ['**/*.js'],
13
+ plugins: {
14
+ 'naming-convention': namingConventionPlugin,
15
+ 'jsdoc': require('eslint-plugin-jsdoc')
16
+ },
17
+ languageOptions: {
18
+ ecmaVersion: 'latest',
19
+ sourceType: 'module',
20
+ parserOptions: {
21
+ ecmaVersion: 'latest',
22
+ sourceType: 'module'
23
+ }
24
+ },
25
+
26
+ // 命名规范规则 - 由自定义插件处理
27
+ rules: {
28
+ // 自定义命名规范插件规则(优先使用)
29
+ 'naming-convention/class-name': 'error',
30
+ 'naming-convention/function-name': 'error',
31
+ 'naming-convention/method-name': 'error',
32
+ 'naming-convention/variable-name': 'warn',
33
+ 'naming-convention/constant-name': 'error',
34
+ 'naming-convention/private-method-naming': 'warn',
35
+ 'naming-convention/private-variable-naming': 'warn',
36
+ 'naming-convention/param-name': 'warn',
37
+ 'naming-convention/property-name': 'warn',
38
+ 'naming-convention/instance-property': 'warn',
39
+
40
+ // 禁用与命名规范插件冲突的默认规则
41
+ 'camelcase': 'off',
42
+ 'id-match': 'off',
43
+ 'new-cap': 'off',
44
+ 'id-length': 'off',
45
+ 'id-denylist': 'off',
46
+ 'id-blacklist': 'off'
47
+ }
48
+ },
49
+
50
+ {
51
+ // 代码风格规则
52
+ rules: {
53
+ // 最大行长度100字符
54
+ 'max-len': ['error', {
55
+ code: 100,
56
+ ignoreComments: true,
57
+ ignoreUrls: true,
58
+ ignoreStrings: true,
59
+ ignoreTemplateLiterals: true
60
+ }],
61
+
62
+ // 缩进2空格
63
+ 'indent': ['error', 2, {
64
+ SwitchCase: 1,
65
+ VariableDeclarator: 1,
66
+ outerIIFEBody: 1,
67
+ MemberExpression: 1,
68
+ FunctionDeclaration: { parameters: 1, body: 1 },
69
+ FunctionExpression: { parameters: 1, body: 1 },
70
+ CallExpression: { arguments: 1 },
71
+ ArrayExpression: 1,
72
+ ObjectExpression: 1,
73
+ ImportDeclaration: 1,
74
+ flatTernaryExpressions: false,
75
+ ignoreComments: false
76
+ }],
77
+
78
+ // 单引号
79
+ 'quotes': ['error', 'single', {
80
+ avoidEscape: true,
81
+ allowTemplateLiterals: true
82
+ }],
83
+
84
+ // 禁止制表符
85
+ 'no-tabs': 'error',
86
+
87
+ // 行尾分号
88
+ 'semi': ['error', 'always'],
89
+
90
+ // 逗号风格
91
+ 'comma-style': ['error', 'last'],
92
+
93
+ // 逗号后必须有空格,逗号前不能有空格
94
+ 'comma-spacing': ['error', {
95
+ before: false,
96
+ after: true
97
+ }]
98
+ }
99
+ },
100
+
101
+ {
102
+ // 错误处理规则
103
+ rules: {
104
+ // 必须进行参数校验
105
+ 'no-unused-vars': ['error', {
106
+ args: 'all',
107
+ argsIgnorePattern: '^_',
108
+ caughtErrors: 'all',
109
+ caughtErrorsIgnorePattern: '^_',
110
+ destructuredArrayIgnorePattern: '^_',
111
+ varsIgnorePattern: '^_',
112
+ ignoreRestSiblings: true
113
+ }],
114
+
115
+ // 建议使用try-catch
116
+ 'no-unsafe-finally': 'warn',
117
+
118
+ // 禁止直接抛出字符串
119
+ 'no-throw-literal': 'error'
120
+ }
121
+ },
122
+
123
+ {
124
+ // 最佳实践规则
125
+ rules: {
126
+ // 优先使用async/await
127
+ 'prefer-promise-reject-errors': 'error',
128
+
129
+ // 避免副作用
130
+ 'no-param-reassign': ['error', {
131
+ props: true,
132
+ ignorePropertyModificationsFor: [
133
+ 'acc', // for reduce accumulators
134
+ 'accumulator', // for reduce accumulators
135
+ 'e', // for e.returnvalue
136
+ 'ctx', // for Koa routing
137
+ 'context', // for Koa routing
138
+ 'req', // for Express requests
139
+ 'request', // for Express requests
140
+ 'res', // for Express responses
141
+ 'response', // for Express responses
142
+ '$scope', // for Angular 1 scopes
143
+ 'staticContext' // for ReactRouter context
144
+ ]
145
+ }],
146
+
147
+ // 优先链式编程
148
+ 'prefer-object-spread': 'error',
149
+
150
+ // 优先函数式编程
151
+ 'prefer-arrow-callback': 'error',
152
+
153
+ // 方法长度限制
154
+ 'max-lines-per-function': ['warn', {
155
+ max: 40,
156
+ skipBlankLines: true,
157
+ skipComments: true
158
+ }],
159
+
160
+ // 复杂度限制
161
+ 'complexity': ['warn', 10]
162
+ }
163
+ },
164
+
165
+ {
166
+ // JSDoc规则
167
+ rules: {
168
+ // 要求公开方法有JSDoc注释
169
+ 'jsdoc/require-jsdoc': ['warn', {
170
+ publicOnly: true,
171
+ require: {
172
+ FunctionDeclaration: true,
173
+ MethodDefinition: true,
174
+ ClassDeclaration: true,
175
+ ArrowFunctionExpression: false,
176
+ FunctionExpression: false
177
+ }
178
+ }],
179
+
180
+ // 检查JSDoc语法
181
+ 'jsdoc/check-alignment': 'warn',
182
+ 'jsdoc/check-indentation': 'warn',
183
+ 'jsdoc/check-param-names': 'warn',
184
+ 'jsdoc/check-syntax': 'warn',
185
+ 'jsdoc/check-tag-names': 'warn',
186
+ 'jsdoc/check-types': 'warn',
187
+ 'jsdoc/no-undefined-types': 'warn',
188
+ 'jsdoc/require-description': 'warn',
189
+ 'jsdoc/require-param': 'warn',
190
+ 'jsdoc/require-param-description': 'warn',
191
+ 'jsdoc/require-param-name': 'warn',
192
+ 'jsdoc/require-param-type': 'warn',
193
+ 'jsdoc/require-returns': 'warn',
194
+ 'jsdoc/require-returns-check': 'warn',
195
+ 'jsdoc/require-returns-description': 'warn',
196
+ 'jsdoc/require-returns-type': 'warn',
197
+ 'jsdoc/valid-types': 'warn'
198
+ }
199
+ },
200
+
201
+ {
202
+ // 忽略规则
203
+ ignores: [
204
+ 'node_modules/**',
205
+ 'dist/**',
206
+ 'build/**',
207
+ 'coverage/**',
208
+ '*.min.js'
209
+ ]
210
+ }
68
211
  ];
package/index.js CHANGED
@@ -12,31 +12,32 @@ const MongoClient = require('mongodb').MongoClient;
12
12
  * @description MongoDB帮助类
13
13
  */
14
14
  class MongoDB extends Base {
15
+ static config = {
16
+ // 服务器地址
17
+ host: "localhost",
18
+ // 端口号
19
+ port: 27017,
20
+ // 数据库名
21
+ database: "mm",
22
+ // 用户名
23
+ user: "",
24
+ // 密码
25
+ password: "",
26
+ // 连接选项 (MongoDB驱动7.x版本支持的参数)
27
+ min_pool_size: 10,
28
+ max_pool_size: 100,
29
+ connect_timeout_ms: 10000,
30
+ server_selection_timeout_ms: 5000,
31
+ retry_writes: true
32
+ };
33
+
15
34
  /**
16
35
  * @description 创建MongoDB帮助类函数 (构造函数)
17
36
  * @param {Object} config 配置参数
18
37
  * @constructor
19
38
  */
20
39
  constructor(config) {
21
- super();
22
- this.config = {
23
- // 服务器地址
24
- host: "localhost",
25
- // 端口号
26
- port: 27017,
27
- // 数据库名
28
- database: "mm",
29
- // 用户名
30
- user: "",
31
- // 密码
32
- password: "",
33
- // 连接选项 (MongoDB驱动7.x版本支持的参数)
34
- min_pool_size: 10,
35
- max_pool_size: 100,
36
- connect_timeout_ms: 10000,
37
- server_selection_timeout_ms: 5000,
38
- retry_writes: true
39
- };
40
+ super(MongoDB.config);
40
41
 
41
42
  // 数据库连接器
42
43
  this.conn = null;
@@ -156,7 +157,6 @@ MongoDB.prototype._ensureConnected = async function () {
156
157
  await this._ensureIndexes();
157
158
  }
158
159
 
159
- this.log('info', `成功连接到数据库,表名: ${this.table}`);
160
160
  } else {
161
161
  this.log('error', `[_ensureConnected] 连接失败,db对象为空或不具有collection方法`);
162
162
  throw new Error('数据库连接失败,db对象为空');
@@ -2914,9 +2914,6 @@ MongoDB.prototype.open = async function () {
2914
2914
  this.connected = false;
2915
2915
  });
2916
2916
 
2917
- this.log('info',
2918
- `成功连接到 ${this.config.host}:${this.config.port}/${this.config.database}`
2919
- );
2920
2917
  return true;
2921
2918
  } catch (error) {
2922
2919
  this.log('error', 'MongoDB连接失败:', error);
@@ -2931,7 +2928,6 @@ MongoDB.prototype.open = async function () {
2931
2928
  */
2932
2929
  MongoDB.prototype.close = async function () {
2933
2930
  if (this.conn) {
2934
- this.log('info', `关闭连接`);
2935
2931
  await this.conn.close().catch(err => this.log('error', '关闭MongoDB连接时出错:', err));
2936
2932
  this.conn = null;
2937
2933
  this.db = null;
@@ -2977,10 +2973,6 @@ MongoDB.prototype.createIndex = async function (keys, options = {}) {
2977
2973
  throw new Error('数据库未连接或未指定表名');
2978
2974
  }
2979
2975
 
2980
- this.log('info',
2981
- `创建索引: ${this.table}, keys: ${JSON.stringify(keys)}, options: ${JSON.stringify(options)}`
2982
- );
2983
-
2984
2976
  return await this.db.collection(this.table).createIndex(keys, options);
2985
2977
  } catch (error) {
2986
2978
  this.log('error', `创建索引失败: ${error.message}`);
@@ -3014,9 +3006,6 @@ MongoDB.prototype.createTTLIndex = async function (keys, expireAfterSeconds) {
3014
3006
  expireAfterSeconds: expireAfterSeconds
3015
3007
  };
3016
3008
 
3017
- this.log('info',
3018
- `创建TTL索引: ${this.table}, keys: ${JSON.stringify(keys)}, 过期时间: ${expireAfterSeconds}秒`);
3019
-
3020
3009
  return await this.db.collection(this.table).createIndex(keys, options);
3021
3010
  } catch (error) {
3022
3011
  this.log('error', `创建TTL索引失败: ${error.message}`);
@@ -3035,7 +3024,6 @@ MongoDB.prototype.getIndexes = async function () {
3035
3024
  throw new Error('未指定表名');
3036
3025
  }
3037
3026
 
3038
- this.log('info', `获取索引: ${this.table}`);
3039
3027
  return await this.db.collection(this.table).indexes();
3040
3028
  } catch (error) {
3041
3029
  this.log('error', `[getIndexes] 获取索引失败: ${error.message}`);
@@ -3057,7 +3045,6 @@ MongoDB.prototype.dropIndex = async function (indexName) {
3057
3045
  throw new TypeError('索引名称不能为空');
3058
3046
  }
3059
3047
  try {
3060
- this.log('info', `[dropIndex] 删除索引: ${this.table}, 索引名: ${indexName}`);
3061
3048
  await this.db.collection(this.table).dropIndex(indexName);
3062
3049
  return true;
3063
3050
  } catch (error) {
@@ -3076,7 +3063,6 @@ MongoDB.prototype.dropTable = async function () {
3076
3063
  throw new Error('未指定表名');
3077
3064
  }
3078
3065
  try {
3079
- this.log('info', `[dropTable] 删除集合: ${this.table}`);
3080
3066
  await this.db.collection(this.table).drop();
3081
3067
  return true;
3082
3068
  } catch (error) {
@@ -3951,11 +3937,6 @@ MongoDB.prototype.rollbackTransaction = async function (session) {
3951
3937
  }
3952
3938
  };
3953
3939
 
3954
- /**
3955
- * 导出MongoDB类
3956
- */
3957
- exports.MongoDB = MongoDB;
3958
-
3959
3940
  /**
3960
3941
  * 确保连接池对象存在
3961
3942
  */
@@ -3991,7 +3972,7 @@ function mongodbAdmin(scope, config) {
3991
3972
  return obj;
3992
3973
  }
3993
3974
 
3994
- /**
3995
- * @module 导出MongoDB管理器
3996
- */
3997
- exports.mongodbAdmin = mongodbAdmin;
3975
+ module.exports = {
3976
+ MongoDB,
3977
+ mongodbAdmin
3978
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_mongodb",
3
- "version": "1.5.4",
3
+ "version": "1.5.6",
4
4
  "description": "MongoDB数据库操作模块,提供简洁易用的API、缓存功能以及完整的Redis兼容接口,支持事件系统、事务和连接池管理",
5
5
  "main": "index.js",
6
6
  "scripts": {