mm_sql 1.4.1 → 1.4.3

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.
@@ -0,0 +1,171 @@
1
+ const { mysqlAdmin } = require('../mm_mysql');
2
+
3
+ /**
4
+ * 测试MySQL语法错误
5
+ * @param {Object} mysql - MySQL实例
6
+ */
7
+ async function testMysqlSyntax(mysql) {
8
+ console.log('\n测试1: 语法错误的SQL...');
9
+ try {
10
+ await mysql.run('SELECT * FROM'); // 语法错误
11
+ console.log('❌ MySQL应该对语法错误SQL抛出错误,但没有抛出');
12
+ } catch (error) {
13
+ console.log('✅ MySQL正确抛出语法错误:', error.message);
14
+ }
15
+ }
16
+
17
+ /**
18
+ * 测试MySQL不存在的表
19
+ * @param {Object} mysql - MySQL实例
20
+ */
21
+ async function testMysqlTable(mysql) {
22
+ console.log('\n测试2: 不存在的表...');
23
+ try {
24
+ const result = await mysql.run('SELECT * FROM non_existent_table_xyz');
25
+ console.log('MySQL返回结果:', result);
26
+ console.log('结果类型:', typeof result);
27
+ console.log('结果长度:', Array.isArray(result) ? result.length : 'N/A');
28
+
29
+ if (Array.isArray(result) && result.length === 0) {
30
+ console.log('⚠️ MySQL对不存在的表返回空数组而不是抛出错误');
31
+ } else {
32
+ console.log('❌ MySQL应该对不存在的表抛出错误,但没有抛出');
33
+ }
34
+ } catch (error) {
35
+ console.log('✅ MySQL正确抛出表不存在错误:', error.message);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 测试MySQL不存在的数据库
41
+ * @param {Object} mysql - MySQL实例
42
+ */
43
+ async function testMysqlDb(mysql) {
44
+ console.log('\n测试3: 不存在的数据库...');
45
+ try {
46
+ await mysql.run('SELECT * FROM non_existent_db.non_existent_table');
47
+ console.log('❌ MySQL应该对不存在的数据库抛出错误,但没有抛出');
48
+ } catch (error) {
49
+ console.log('✅ MySQL正确抛出数据库不存在错误:', error.message);
50
+ }
51
+ }
52
+
53
+ /**
54
+ * 测试MySQL权限错误
55
+ * @param {Object} mysql - MySQL实例
56
+ */
57
+ async function testMysqlPerm(mysql) {
58
+ console.log('\n测试4: 权限错误...');
59
+ try {
60
+ await mysql.run('DROP TABLE IF EXISTS important_system_table');
61
+ console.log('❌ MySQL应该对权限错误抛出错误,但没有抛出');
62
+ } catch (error) {
63
+ console.log('✅ MySQL正确抛出权限错误:', error.message);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * 详细测试MySQL错误处理
69
+ */
70
+ async function mysqlErrorTest() {
71
+ console.log('=== 详细测试MySQL错误处理 ===');
72
+
73
+ try {
74
+ // 创建mysqlAdmin实例
75
+ const mysql = mysqlAdmin('detailed_test', {
76
+ host: 'localhost',
77
+ port: 3306,
78
+ user: 'root',
79
+ password: 'Asd159357',
80
+ database: 'mm'
81
+ });
82
+
83
+ console.log('✅ MySQL实例创建成功');
84
+
85
+ await testMysqlSyntax(mysql);
86
+ await testMysqlTable(mysql);
87
+ await testMysqlDb(mysql);
88
+ await testMysqlPerm(mysql);
89
+
90
+ } catch (error) {
91
+ console.log('❌ MySQL实例创建失败:', error.message);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * 测试SQLite语法错误
97
+ * @param {Object} sqlite - SQLite实例
98
+ */
99
+ async function testSqliteSyntax(sqlite) {
100
+ console.log('\n测试1: 语法错误的SQL...');
101
+ try {
102
+ await sqlite.run('SELECT * FROM'); // 语法错误
103
+ console.log('❌ SQLite应该对语法错误SQL抛出错误,但没有抛出');
104
+ } catch (error) {
105
+ console.log('✅ SQLite正确抛出语法错误:', error.message);
106
+ }
107
+ }
108
+
109
+ /**
110
+ * 测试SQLite不存在的表
111
+ * @param {Object} sqlite - SQLite实例
112
+ */
113
+ async function testSqliteTable(sqlite) {
114
+ console.log('\n测试2: 不存在的表...');
115
+ try {
116
+ const result = await sqlite.run('SELECT * FROM non_existent_table_xyz');
117
+ console.log('SQLite返回结果:', result);
118
+ console.log('结果类型:', typeof result);
119
+ console.log('结果长度:', Array.isArray(result) ? result.length : 'N/A');
120
+
121
+ if (Array.isArray(result) && result.length === 0) {
122
+ console.log('⚠️ SQLite对不存在的表返回空数组而不是抛出错误');
123
+ } else {
124
+ console.log('❌ SQLite应该对不存在的表抛出错误,但没有抛出');
125
+ }
126
+ } catch (error) {
127
+ console.log('✅ SQLite正确抛出表不存在错误:', error.message);
128
+ }
129
+ }
130
+
131
+ /**
132
+ * 测试SQLite错误处理
133
+ */
134
+ async function sqliteErrorTest() {
135
+ console.log('\n=== 详细测试SQLite错误处理 ===');
136
+
137
+ try {
138
+ const { sqliteAdmin } = require('../mm_sqlite');
139
+
140
+ // 创建sqliteAdmin实例
141
+ const sqlite = sqliteAdmin('detailed_test', {
142
+ dir: './db',
143
+ database: 'mm'
144
+ });
145
+
146
+ console.log('✅ SQLite实例创建成功');
147
+
148
+ await testSqliteSyntax(sqlite);
149
+ await testSqliteTable(sqlite);
150
+
151
+ } catch (error) {
152
+ console.log('❌ SQLite实例创建失败:', error.message);
153
+ }
154
+ }
155
+
156
+ /**
157
+ * 主测试函数
158
+ */
159
+ async function main() {
160
+ console.log('开始详细测试数据库错误处理...\n');
161
+
162
+ await detailedMysqlErrorTest();
163
+ await detailedSqliteErrorTest();
164
+
165
+ console.log('\n详细测试完成');
166
+ }
167
+
168
+ // 运行测试
169
+ if (require.main === module) {
170
+ main().catch(console.error);
171
+ }
@@ -0,0 +1,235 @@
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
+ 'naming-convention/prototype-method': 'error',
40
+
41
+ // 禁用与命名规范插件冲突的默认规则
42
+ camelcase: 'off',
43
+ 'id-match': 'off',
44
+ 'new-cap': 'off',
45
+ 'id-length': 'off',
46
+ 'id-denylist': 'off',
47
+ 'id-blacklist': 'off',
48
+ },
49
+ },
50
+
51
+ {
52
+ // 代码风格规则
53
+ rules: {
54
+ // 最大行长度100字符
55
+ 'max-len': [
56
+ 'error',
57
+ {
58
+ code: 100,
59
+ ignoreComments: true,
60
+ ignoreUrls: true,
61
+ ignoreStrings: true,
62
+ ignoreTemplateLiterals: true,
63
+ },
64
+ ],
65
+
66
+ // 缩进2空格
67
+ indent: [
68
+ 'error',
69
+ 2,
70
+ {
71
+ SwitchCase: 1,
72
+ VariableDeclarator: 1,
73
+ outerIIFEBody: 1,
74
+ MemberExpression: 1,
75
+ FunctionDeclaration: { parameters: 1, body: 1 },
76
+ FunctionExpression: { parameters: 1, body: 1 },
77
+ CallExpression: { arguments: 1 },
78
+ ArrayExpression: 1,
79
+ ObjectExpression: 1,
80
+ ImportDeclaration: 1,
81
+ flatTernaryExpressions: false,
82
+ ignoreComments: false,
83
+ },
84
+ ],
85
+
86
+ // 单引号
87
+ quotes: [
88
+ 'error',
89
+ 'single',
90
+ {
91
+ avoidEscape: true,
92
+ allowTemplateLiterals: true,
93
+ },
94
+ ],
95
+
96
+ // 禁止制表符
97
+ 'no-tabs': 'error',
98
+
99
+ // 行尾分号
100
+ semi: ['error', 'always'],
101
+
102
+ // 逗号风格
103
+ 'comma-style': ['error', 'last'],
104
+
105
+ // 逗号间距
106
+ 'comma-spacing': [
107
+ 'error',
108
+ {
109
+ before: false,
110
+ after: true,
111
+ },
112
+ ],
113
+ },
114
+ },
115
+
116
+ {
117
+ // 错误处理规则
118
+ rules: {
119
+ // 必须进行参数校验
120
+ 'no-unused-vars': [
121
+ 'error',
122
+ {
123
+ args: 'all',
124
+ caughtErrors: 'all',
125
+ ignoreRestSiblings: true,
126
+ },
127
+ ],
128
+
129
+ // 建议使用try-catch
130
+ 'no-unsafe-finally': 'warn',
131
+
132
+ // 禁止直接抛出字符串
133
+ 'no-throw-literal': 'error',
134
+ },
135
+ },
136
+
137
+ {
138
+ // 最佳实践规则
139
+ rules: {
140
+ // 优先使用async/await
141
+ 'prefer-promise-reject-errors': 'error',
142
+
143
+ // 避免副作用
144
+ 'no-param-reassign': [
145
+ 'error',
146
+ {
147
+ props: true,
148
+ ignorePropertyModificationsFor: [
149
+ 'acc', // for reduce accumulators
150
+ 'accumulator', // for reduce accumulators
151
+ 'e', // for e.returnvalue
152
+ 'ctx', // for Koa routing
153
+ 'context', // for Koa routing
154
+ 'req', // for Express requests
155
+ 'request', // for Express requests
156
+ 'res', // for Express responses
157
+ 'response', // for Express responses
158
+ '$scope', // for Angular 1 scopes
159
+ 'staticContext', // for ReactRouter context
160
+ ],
161
+ },
162
+ ],
163
+
164
+ // 优先链式编程
165
+ 'prefer-object-spread': 'error',
166
+
167
+ // 优先函数式编程
168
+ 'prefer-arrow-callback': 'error',
169
+
170
+ // 方法长度限制
171
+ 'max-lines-per-function': [
172
+ 'warn',
173
+ {
174
+ max: 40,
175
+ skipBlankLines: true,
176
+ skipComments: true,
177
+ },
178
+ ],
179
+
180
+ // 复杂度限制
181
+ complexity: ['warn', 10],
182
+ },
183
+ },
184
+
185
+ {
186
+ // JSDoc规则
187
+ rules: {
188
+ // 要求公开方法有JSDoc注释
189
+ 'jsdoc/require-jsdoc': [
190
+ 'warn',
191
+ {
192
+ publicOnly: true,
193
+ require: {
194
+ FunctionDeclaration: true,
195
+ MethodDefinition: true,
196
+ ClassDeclaration: true,
197
+ ArrowFunctionExpression: false,
198
+ FunctionExpression: false,
199
+ },
200
+ },
201
+ ],
202
+
203
+ // 检查JSDoc语法
204
+ 'jsdoc/check-alignment': 'warn',
205
+ 'jsdoc/check-indentation': 'warn',
206
+ 'jsdoc/check-param-names': 'warn',
207
+ 'jsdoc/check-syntax': 'warn',
208
+ 'jsdoc/check-tag-names': 'warn',
209
+ 'jsdoc/check-types': 'warn',
210
+ 'jsdoc/no-undefined-types': 'warn',
211
+ 'jsdoc/require-description': 'warn',
212
+ 'jsdoc/require-param': 'warn',
213
+ 'jsdoc/require-param-description': 'warn',
214
+ 'jsdoc/require-param-name': 'warn',
215
+ 'jsdoc/require-param-type': 'warn',
216
+ 'jsdoc/require-returns': 'warn',
217
+ 'jsdoc/require-returns-check': 'warn',
218
+ 'jsdoc/require-returns-description': 'warn',
219
+ 'jsdoc/require-returns-type': 'warn',
220
+ 'jsdoc/valid-types': 'warn',
221
+ },
222
+ },
223
+
224
+ {
225
+ // 忽略规则
226
+ ignores: [
227
+ 'node_modules/**',
228
+ 'dist/**',
229
+ 'build/**',
230
+ 'coverage/**',
231
+ '*.min.js',
232
+ 'eslint.config.js'
233
+ ],
234
+ },
235
+ ];