@shys/crud 1.0.1-beta.5 → 1.0.1-beta.7
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.
- package/dist/db.js +3 -2
- package/dist/generator.js +22 -12
- package/package.json +1 -1
- package/templates/entity.njk +19 -9
- package/templates/service.njk +2 -2
package/dist/db.js
CHANGED
|
@@ -52,7 +52,7 @@ export async function getTableFieldsFromDb(dbConfig, tableName) {
|
|
|
52
52
|
database: database || '',
|
|
53
53
|
});
|
|
54
54
|
try {
|
|
55
|
-
const [rows] = await connection.execute(`SELECT column_name, data_type, is_nullable, column_type, column_comment, column_default, character_maximum_length, extra
|
|
55
|
+
const [rows] = await connection.execute(`SELECT column_name, data_type, is_nullable, column_type, column_comment, column_default, character_maximum_length, extra, column_key
|
|
56
56
|
FROM information_schema.COLUMNS
|
|
57
57
|
WHERE table_schema = ? AND table_name = ?
|
|
58
58
|
ORDER BY ordinal_position`, [database, tableName]);
|
|
@@ -68,7 +68,8 @@ export async function getTableFieldsFromDb(dbConfig, tableName) {
|
|
|
68
68
|
const defaultVal = row.COLUMN_DEFAULT || row.column_default;
|
|
69
69
|
const length = row.CHARACTER_MAXIMUM_LENGTH || row.character_maximum_length;
|
|
70
70
|
const extra = row.EXTRA || row.extra || '';
|
|
71
|
-
const
|
|
71
|
+
const columnKey = row.COLUMN_KEY || row.column_key || '';
|
|
72
|
+
const isPk = columnKey === 'PRI';
|
|
72
73
|
const isAuto = extra.includes('auto_increment');
|
|
73
74
|
return {
|
|
74
75
|
name: columnName,
|
package/dist/generator.js
CHANGED
|
@@ -186,8 +186,10 @@ export function buildFiles(params) {
|
|
|
186
186
|
const desc = `${tableName} 表增删改查接口`;
|
|
187
187
|
const modRoot = resolve(outputPath || baseSrcPath, 'modules', moduleName);
|
|
188
188
|
const resultFiles = [];
|
|
189
|
-
const
|
|
190
|
-
const
|
|
189
|
+
const pkFields = fields.filter(f => f.pk);
|
|
190
|
+
const originalPkInfo = pkFields[0] || null;
|
|
191
|
+
const hasPk = pkFields.length > 0;
|
|
192
|
+
const autoPkFields = pkFields.filter(f => f.auto);
|
|
191
193
|
const entityFields = fields.filter(f => {
|
|
192
194
|
if (finalBaseFieldNames.includes(f.camel))
|
|
193
195
|
return false;
|
|
@@ -200,6 +202,7 @@ export function buildFiles(params) {
|
|
|
200
202
|
const hasDateField = entityFields.some(f => /datetime|date|timestamp/i.test(f.type));
|
|
201
203
|
const enhancedEntityFields = entityFields.map(f => enhanceField(f, { idx: idxFields.some(i => i.name === f.name) }));
|
|
202
204
|
const enhancedPkInfo = originalPkInfo ? enhanceField(originalPkInfo) : null;
|
|
205
|
+
const enhancedPkInfos = pkFields.map(f => enhanceField(f));
|
|
203
206
|
// Entity
|
|
204
207
|
resultFiles.push({
|
|
205
208
|
path: resolve(modRoot, `entity/${Camel}.entity.ts`),
|
|
@@ -211,6 +214,7 @@ export function buildFiles(params) {
|
|
|
211
214
|
Pascal,
|
|
212
215
|
Camel,
|
|
213
216
|
pkInfo: enhancedPkInfo,
|
|
217
|
+
pkInfos: enhancedPkInfos,
|
|
214
218
|
hasPk,
|
|
215
219
|
hasDateField,
|
|
216
220
|
idxCount: idxFields.length,
|
|
@@ -227,20 +231,23 @@ export function buildFiles(params) {
|
|
|
227
231
|
searchable: [],
|
|
228
232
|
};
|
|
229
233
|
}
|
|
230
|
-
const createFields =
|
|
234
|
+
const createFields = fields.filter(f => {
|
|
235
|
+
if (finalBaseFieldNames.includes(f.camel))
|
|
236
|
+
return false;
|
|
237
|
+
if (f.pk && f.auto)
|
|
238
|
+
return false;
|
|
239
|
+
return true;
|
|
240
|
+
});
|
|
231
241
|
const writable = createFields.filter(f => !excludeWritableRegex.test(f.name));
|
|
232
242
|
const isSensitive = (f) => /password|passwd|salt|token|secret|private_key/i.test(f.name + f.camel);
|
|
233
243
|
const pkCamel = originalPkInfo?.camel || '';
|
|
234
|
-
const
|
|
244
|
+
const pkCamels = pkFields.map(f => f.camel);
|
|
245
|
+
const sortableBase = [...entityFields, ...pkFields]
|
|
235
246
|
.filter(f => !isSensitive(f))
|
|
236
247
|
.map(f => f.camel);
|
|
237
|
-
const sortable =
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const filterableBase = entityFields.filter(f => !isSensitive(f));
|
|
241
|
-
const filterableArr = pkCamel && !filterableBase.some(f => f.camel === pkCamel)
|
|
242
|
-
? [originalPkInfo, ...filterableBase]
|
|
243
|
-
: filterableBase;
|
|
248
|
+
const sortable = [...new Set([...pkCamels, ...sortableBase])];
|
|
249
|
+
const filterableBase = [...entityFields, ...pkFields].filter(f => !isSensitive(f));
|
|
250
|
+
const filterableArr = [...new Map([...pkFields, ...filterableBase].map(f => [f.camel, f])).values()];
|
|
244
251
|
const filterable = filterableArr.map(f => f.camel);
|
|
245
252
|
const searchable = filterableArr
|
|
246
253
|
.filter(f => /char|varchar|text/i.test(f.type))
|
|
@@ -325,7 +332,7 @@ export function buildFiles(params) {
|
|
|
325
332
|
});
|
|
326
333
|
// Single VO
|
|
327
334
|
const allVoFields = [
|
|
328
|
-
...
|
|
335
|
+
...pkFields,
|
|
329
336
|
...entityFields,
|
|
330
337
|
].map(f => enhanceField(f));
|
|
331
338
|
const voHasTransform = allVoFields.some(f => f.isDate || f.isSensitive);
|
|
@@ -366,6 +373,7 @@ export function buildFiles(params) {
|
|
|
366
373
|
// Controller
|
|
367
374
|
const sArr = (arr) => arr.map(x => JSON.stringify(x)).join(', ');
|
|
368
375
|
const idField = pkCamel || 'id';
|
|
376
|
+
const idFields = pkCamels.length ? pkCamels : ['id'];
|
|
369
377
|
const defaultSort = searchable && searchable.length
|
|
370
378
|
? `[{ field: ${JSON.stringify(sortable[0] || idField)}, order: 'DESC' }]`
|
|
371
379
|
: `[{ field: 'id', order: 'DESC' }]`;
|
|
@@ -389,7 +397,9 @@ export function buildFiles(params) {
|
|
|
389
397
|
filterable: sArr(filterable),
|
|
390
398
|
searchable: sArr(searchable),
|
|
391
399
|
pkCamel,
|
|
400
|
+
pkCamels,
|
|
392
401
|
idField,
|
|
402
|
+
idFields,
|
|
393
403
|
defaultSort,
|
|
394
404
|
}),
|
|
395
405
|
});
|
package/package.json
CHANGED
package/templates/entity.njk
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// {{ orm }} Entity 模板占位(TypeORM 是当前唯一完整生成的 ORM)
|
|
3
3
|
export class {{ Pascal }} {}
|
|
4
4
|
{%- else -%}
|
|
5
|
-
|
|
5
|
+
{% set hasAutoPk = pkInfos | selectattr('auto') | list | length > 0 -%}
|
|
6
|
+
{% set hasNonAutoPk = pkInfos | rejectattr('auto') | list | length > 0 -%}
|
|
7
|
+
import { Entity, Column{% if hasPk and hasAutoPk %}, PrimaryGeneratedColumn{% endif %}{% if hasPk and hasNonAutoPk %}, PrimaryColumn{% endif %}{% if idxCount %}, Index{% endif %} } from 'typeorm';
|
|
6
8
|
import { Expose{% if hasDateField %}, Transform{% endif %} } from 'class-transformer';
|
|
7
9
|
import { ApiProperty, ApiPropertyOptional } from '@midwayjs/swagger';
|
|
8
10
|
import { Rule } from '@midwayjs/validation';
|
|
@@ -13,21 +15,29 @@ import { BaseEntity } from '../../../framework/entity/base.entity';
|
|
|
13
15
|
comment: {{ (tableName + ' 表') | json }},
|
|
14
16
|
})
|
|
15
17
|
export class {{ Pascal }} extends BaseEntity {
|
|
16
|
-
{%
|
|
18
|
+
{% for pk in pkInfos -%}
|
|
19
|
+
{% if pk.auto -%}
|
|
17
20
|
@PrimaryGeneratedColumn({
|
|
18
|
-
type: {{
|
|
19
|
-
name: {{
|
|
20
|
-
comment: {{ (
|
|
21
|
+
type: {{ pk.type | json }},
|
|
22
|
+
name: {{ pk.name | json }},
|
|
23
|
+
comment: {{ (pk.comment or '主键') | json }},
|
|
21
24
|
})
|
|
25
|
+
{% else -%}
|
|
26
|
+
@PrimaryColumn({
|
|
27
|
+
type: {{ pk.type | json }},
|
|
28
|
+
name: {{ pk.name | json }},
|
|
29
|
+
comment: {{ (pk.comment or '主键') | json }},
|
|
30
|
+
})
|
|
31
|
+
{% endif -%}
|
|
22
32
|
@ApiProperty({
|
|
23
33
|
type: 'integer',
|
|
24
|
-
description: {{ (
|
|
34
|
+
description: {{ (pk.comment or '主键ID') | json }},
|
|
25
35
|
})
|
|
26
36
|
@Expose()
|
|
27
|
-
@Rule(Joi.number().integer().label({{ (
|
|
28
|
-
{{
|
|
37
|
+
@Rule(Joi.number().integer().label({{ (pk.comment or '主键ID') | json }}).required())
|
|
38
|
+
{{ pk.camel }}: number;
|
|
29
39
|
|
|
30
|
-
{%
|
|
40
|
+
{% endfor -%}
|
|
31
41
|
{% for f in fields -%}
|
|
32
42
|
{% if f.idx -%} @Index()
|
|
33
43
|
{% endif -%} @Column({
|
package/templates/service.njk
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
|
3
3
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
4
4
|
import { Repository } from 'typeorm';
|
|
5
|
-
import {
|
|
5
|
+
import { BaseTypeOrmCrudService } from '@shys/core';
|
|
6
6
|
import { {{ Pascal }} } from '../entity/{{ Camel }}.entity';
|
|
7
7
|
|
|
8
8
|
@Provide()
|
|
9
9
|
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
|
10
|
-
export class {{ ServiceName }} extends
|
|
10
|
+
export class {{ ServiceName }} extends BaseTypeOrmCrudService<{{ Pascal }}> {
|
|
11
11
|
@InjectEntityModel({{ Pascal }})
|
|
12
12
|
repo: Repository<{{ Pascal }}>;
|
|
13
13
|
}
|