typeorm-dto-generator 1.0.1 → 1.0.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,156 @@
1
+ import * as ts_morph from 'ts-morph';
2
+ import { Project, SourceFile, ClassDeclaration, PropertyDeclaration } from 'ts-morph';
3
+ import * as typeorm from 'typeorm';
4
+ import { DataSource } from 'typeorm';
5
+
6
+ type Config = {
7
+ datasource: DataSource;
8
+ debug: boolean;
9
+ dtoOutputDir: string;
10
+ mapperOutputFile: string;
11
+ sharedDTOImportPath: string;
12
+ excludePropertyNames: string[];
13
+ customScalarTypeMap: Record<string, string>;
14
+ includeExtensionOnImports: boolean;
15
+ };
16
+ type InitConfig = Partial<Config> & {
17
+ test: any;
18
+ datasource: DataSource;
19
+ };
20
+ type EntityMeta = {
21
+ className: string;
22
+ dtoName: string;
23
+ moduleName: string;
24
+ dtoFileName: string;
25
+ mapperName: string;
26
+ filePath?: string;
27
+ columns: ColumnMeta[];
28
+ relations: RelationMeta[];
29
+ };
30
+ type ColumnMeta = {
31
+ name: string;
32
+ optional: boolean;
33
+ type: string;
34
+ isDate: boolean;
35
+ mapAsNumber?: boolean;
36
+ enumValues?: (string | number)[];
37
+ };
38
+ type RelationMeta = {
39
+ name: string;
40
+ targetClassName: string;
41
+ targetDTOName: string;
42
+ isArray: boolean;
43
+ nullable: boolean;
44
+ };
45
+ type GenerateResult = {
46
+ metas: EntityMeta[];
47
+ dtoOutputDir: string;
48
+ mapperOutputFile: string;
49
+ };
50
+
51
+ type RuntimeColumn = {
52
+ propertyName: string;
53
+ type: unknown;
54
+ enum?: (string | number)[];
55
+ enumName?: string;
56
+ isNullable: boolean;
57
+ isSelect: boolean;
58
+ relationMetadata?: unknown;
59
+ };
60
+ type RuntimeRelation = {
61
+ propertyName: string;
62
+ inverseEntityMetadata: RuntimeEntityMetadata;
63
+ isManyToMany: boolean;
64
+ isOneToMany: boolean;
65
+ isNullable: boolean;
66
+ };
67
+ type RuntimeEntityMetadata = {
68
+ target: Function | string;
69
+ name: string;
70
+ columns: RuntimeColumn[];
71
+ relations: RuntimeRelation[];
72
+ };
73
+ type EntitySourceInfo = {
74
+ filePath?: string;
75
+ enumTypeByProperty: Map<string, string>;
76
+ typeByProperty: Map<string, string>;
77
+ };
78
+ declare class TORMDTOGenerator {
79
+ readonly config: Config;
80
+ protected readonly project: Project;
81
+ protected readonly sourceInfoByClassName: Map<string, EntitySourceInfo>;
82
+ constructor(config: InitConfig);
83
+ run(): Promise<GenerateResult>;
84
+ protected log(message: string): void;
85
+ protected createConfig(config: InitConfig): Config;
86
+ protected writeFile(filePath: string, content: string): void;
87
+ protected toKebabCase(value: string): string;
88
+ protected unique<T>(items: T[]): T[];
89
+ protected createRelativeJSImportPath(fromFile: string, targetFile: string): string;
90
+ protected getCommonTypesFilePath(): string;
91
+ protected getCommonTypesImportSpecifier(dtoFilePath: string): string;
92
+ protected getDatasourceEntities(): (string | Function | typeorm.EntitySchema<any>)[];
93
+ protected normalizeEntityPattern(pattern: string): string;
94
+ protected isSourceFilePath(filePath: string): boolean;
95
+ protected isEntitySourceFilePath(filePath: string): boolean;
96
+ protected normalizeAbsolutePath(filePath: string): string;
97
+ protected isIgnoredSourcePath(filePath: string): boolean;
98
+ protected discoverProjectSourcePaths(): string[];
99
+ protected addEntityOptionSourceFiles(): SourceFile[];
100
+ protected addDiscoveredSourceFiles(): SourceFile[];
101
+ protected loadEntitySourceInfo(): void;
102
+ protected isEntityClass(classDeclaration: ClassDeclaration): boolean;
103
+ protected getDecoratorObjectLiteral(property: PropertyDeclaration): ts_morph.ObjectLiteralExpression | null;
104
+ protected getColumnEnumInitializer(property: PropertyDeclaration): string | null;
105
+ protected getEnumTypeNameFromProperty(property: PropertyDeclaration): string | null;
106
+ protected collectEnumTypesByProperty(classDeclaration: ClassDeclaration): Map<string, string>;
107
+ protected cleanTypeText(value: string): string;
108
+ protected collectTypesByProperty(classDeclaration: ClassDeclaration): Map<string, string>;
109
+ protected collectProperties(classDeclaration: ClassDeclaration, visitedClassNames?: Set<string>): PropertyDeclaration[];
110
+ protected getClassName(target: Function | string, fallback: string): string;
111
+ protected ensureDatasourceMetadata(): Promise<void>;
112
+ protected isExcludedColumn(column: RuntimeColumn): boolean;
113
+ protected isDateColumn(column: RuntimeColumn): boolean;
114
+ protected isJsonColumn(column: RuntimeColumn): boolean;
115
+ protected resolveEnumTypeName(entityName: string, column: RuntimeColumn): string;
116
+ protected resolveSourcePropertyType(entityName: string, propertyName: string): string | null;
117
+ protected resolveColumnType(entityName: string, column: RuntimeColumn): string;
118
+ protected appendNullType(typeText: string): string;
119
+ protected columnToMeta(entityName: string, column: RuntimeColumn): ColumnMeta;
120
+ protected relationToMeta(relation: RuntimeRelation): RelationMeta;
121
+ protected createEntityMetas(): Promise<{
122
+ className: string;
123
+ dtoName: string;
124
+ moduleName: string;
125
+ dtoFileName: string;
126
+ mapperName: string;
127
+ filePath: string | undefined;
128
+ columns: ColumnMeta[];
129
+ relations: RelationMeta[];
130
+ }[]>;
131
+ protected findMetaByClassName(metas: EntityMeta[], className: string): EntityMeta | undefined;
132
+ protected getValidRelations(meta: EntityMeta, metas: EntityMeta[]): RelationMeta[];
133
+ protected createDTOContent(meta: EntityMeta, metas: EntityMeta[]): string;
134
+ protected collectExternalTypeImports(meta: EntityMeta, dtoFilePath: string, relationTypeNames: Set<string>): [string, string][];
135
+ protected collectExternalTypeSymbols(meta: EntityMeta, relationTypeNames: Set<string>): Set<string>;
136
+ protected collectAllExternalTypeSymbols(metas: EntityMeta[]): Set<string>;
137
+ protected findTypeDeclarationByName(typeName: string): ts_morph.InterfaceDeclaration | ts_morph.EnumDeclaration | ts_morph.TypeAliasDeclaration | null;
138
+ protected findEnumDeclarationByName(typeName: string): ts_morph.EnumDeclaration | null;
139
+ protected normalizeDeclarationToExportText(typeName: string, preferEnum?: boolean): string | null;
140
+ protected formatEnumKey(value: string | number): string;
141
+ protected formatEnumValue(value: string | number): string;
142
+ protected createCommonEnumDeclaration(typeName: string, values: (string | number)[]): string;
143
+ protected ensureCommonTypesFile(metas: EntityMeta[]): void;
144
+ protected resolveCommonTypesSourceContent(): string | null;
145
+ protected validateCommonTypeCoverage(metas: EntityMeta[]): void;
146
+ protected createDTOIndexContent(metas: EntityMeta[]): string;
147
+ protected getMetasWithFilePath(metas: EntityMeta[]): EntityMeta[];
148
+ protected createEntityImports(metas: EntityMeta[]): string;
149
+ protected getMapperEntityTypeName(meta: EntityMeta): string;
150
+ protected createStructuralEntityTypes(metas: EntityMeta[]): string;
151
+ protected createGeneratedMapperContent(metas: EntityMeta[]): string;
152
+ protected createMapperFunctionContent(meta: EntityMeta, metas: EntityMeta[]): string;
153
+ protected createRelationMapperLine(relation: RelationMeta, metas: EntityMeta[]): string;
154
+ }
155
+
156
+ export { TORMDTOGenerator };
package/dist/index.d.ts CHANGED
@@ -1,75 +1,156 @@
1
- import { ClassDeclaration, Decorator, GetAccessorDeclaration, ObjectLiteralExpression, Project, PropertyAssignment, PropertyDeclaration, SourceFile, Type } from 'ts-morph';
2
- import { ColumnMeta, EntityMeta, Config, GenerateResult, RelationMeta } from './types.ts';
3
- export declare class TORMDTOGenerator {
4
- protected readonly config: Config;
1
+ import * as ts_morph from 'ts-morph';
2
+ import { Project, SourceFile, ClassDeclaration, PropertyDeclaration } from 'ts-morph';
3
+ import * as typeorm from 'typeorm';
4
+ import { DataSource } from 'typeorm';
5
+
6
+ type Config = {
7
+ datasource: DataSource;
8
+ debug: boolean;
9
+ dtoOutputDir: string;
10
+ mapperOutputFile: string;
11
+ sharedDTOImportPath: string;
12
+ excludePropertyNames: string[];
13
+ customScalarTypeMap: Record<string, string>;
14
+ includeExtensionOnImports: boolean;
15
+ };
16
+ type InitConfig = Partial<Config> & {
17
+ test: any;
18
+ datasource: DataSource;
19
+ };
20
+ type EntityMeta = {
21
+ className: string;
22
+ dtoName: string;
23
+ moduleName: string;
24
+ dtoFileName: string;
25
+ mapperName: string;
26
+ filePath?: string;
27
+ columns: ColumnMeta[];
28
+ relations: RelationMeta[];
29
+ };
30
+ type ColumnMeta = {
31
+ name: string;
32
+ optional: boolean;
33
+ type: string;
34
+ isDate: boolean;
35
+ mapAsNumber?: boolean;
36
+ enumValues?: (string | number)[];
37
+ };
38
+ type RelationMeta = {
39
+ name: string;
40
+ targetClassName: string;
41
+ targetDTOName: string;
42
+ isArray: boolean;
43
+ nullable: boolean;
44
+ };
45
+ type GenerateResult = {
46
+ metas: EntityMeta[];
47
+ dtoOutputDir: string;
48
+ mapperOutputFile: string;
49
+ };
50
+
51
+ type RuntimeColumn = {
52
+ propertyName: string;
53
+ type: unknown;
54
+ enum?: (string | number)[];
55
+ enumName?: string;
56
+ isNullable: boolean;
57
+ isSelect: boolean;
58
+ relationMetadata?: unknown;
59
+ };
60
+ type RuntimeRelation = {
61
+ propertyName: string;
62
+ inverseEntityMetadata: RuntimeEntityMetadata;
63
+ isManyToMany: boolean;
64
+ isOneToMany: boolean;
65
+ isNullable: boolean;
66
+ };
67
+ type RuntimeEntityMetadata = {
68
+ target: Function | string;
69
+ name: string;
70
+ columns: RuntimeColumn[];
71
+ relations: RuntimeRelation[];
72
+ };
73
+ type EntitySourceInfo = {
74
+ filePath?: string;
75
+ enumTypeByProperty: Map<string, string>;
76
+ typeByProperty: Map<string, string>;
77
+ };
78
+ declare class TORMDTOGenerator {
79
+ readonly config: Config;
5
80
  protected readonly project: Project;
6
- constructor(config?: Partial<Config>);
7
- run(): GenerateResult;
8
- protected createConfig(config: Partial<Config>): Config;
81
+ protected readonly sourceInfoByClassName: Map<string, EntitySourceInfo>;
82
+ constructor(config: InitConfig);
83
+ run(): Promise<GenerateResult>;
84
+ protected log(message: string): void;
85
+ protected createConfig(config: InitConfig): Config;
9
86
  protected writeFile(filePath: string, content: string): void;
10
87
  protected toKebabCase(value: string): string;
11
88
  protected unique<T>(items: T[]): T[];
12
- protected cleanTypeText(value: string): string;
13
89
  protected createRelativeJSImportPath(fromFile: string, targetFile: string): string;
14
90
  protected getCommonTypesFilePath(): string;
15
91
  protected getCommonTypesImportSpecifier(dtoFilePath: string): string;
16
- protected getDecorator(property: PropertyDeclaration, names: string[]): Decorator | undefined;
17
- protected hasDecorator(property: PropertyDeclaration, names: string[]): boolean;
92
+ protected getDatasourceEntities(): (string | Function | typeorm.EntitySchema<any>)[];
93
+ protected normalizeEntityPattern(pattern: string): string;
94
+ protected isSourceFilePath(filePath: string): boolean;
95
+ protected isEntitySourceFilePath(filePath: string): boolean;
96
+ protected normalizeAbsolutePath(filePath: string): string;
97
+ protected isIgnoredSourcePath(filePath: string): boolean;
98
+ protected discoverProjectSourcePaths(): string[];
99
+ protected addEntityOptionSourceFiles(): SourceFile[];
100
+ protected addDiscoveredSourceFiles(): SourceFile[];
101
+ protected loadEntitySourceInfo(): void;
18
102
  protected isEntityClass(classDeclaration: ClassDeclaration): boolean;
19
- protected getDecoratorArgumentsText(decorator: Decorator | undefined): string[];
20
- protected getDecoratorObjectLiteral(decorator: Decorator | undefined): ObjectLiteralExpression | null;
21
- protected getObjectLiteralProperty(objectLiteral: ObjectLiteralExpression | null, propertyName: string): PropertyAssignment | null;
22
- protected getPropertyAssignmentInitializerText(property: PropertyAssignment | null): string | null;
23
- protected hasDecoratorOption(property: PropertyDeclaration, decoratorNames: string[], optionName: string, optionValue: string): boolean;
24
- protected hasSelectFalse(property: PropertyDeclaration): boolean;
25
- protected isExcludedProperty(property: PropertyDeclaration): boolean;
26
- protected isColumnProperty(property: PropertyDeclaration): boolean;
27
- protected isRelationProperty(property: PropertyDeclaration): boolean;
103
+ protected getDecoratorObjectLiteral(property: PropertyDeclaration): ts_morph.ObjectLiteralExpression | null;
104
+ protected getColumnEnumInitializer(property: PropertyDeclaration): string | null;
105
+ protected getEnumTypeNameFromProperty(property: PropertyDeclaration): string | null;
106
+ protected collectEnumTypesByProperty(classDeclaration: ClassDeclaration): Map<string, string>;
107
+ protected cleanTypeText(value: string): string;
108
+ protected collectTypesByProperty(classDeclaration: ClassDeclaration): Map<string, string>;
28
109
  protected collectProperties(classDeclaration: ClassDeclaration, visitedClassNames?: Set<string>): PropertyDeclaration[];
29
- protected getBaseClass(classDeclaration: ClassDeclaration): ClassDeclaration | null;
30
- protected findClassDeclarationByName(className: string): ClassDeclaration | null;
31
- protected isNullishTypeText(typeText: string): typeText is "null" | "undefined";
32
- protected isNullableTypeText(typeText: string): boolean;
33
- protected getPropertyTypeText(property: PropertyDeclaration): string;
34
- protected getAccessorTypeText(accessor: GetAccessorDeclaration): string;
35
- protected isDateColumn(property: PropertyDeclaration): boolean;
36
- protected isNullableColumn(property: PropertyDeclaration): boolean;
37
- protected resolveEnumType(type: Type): string | null;
38
- protected resolveEnumTypeFromProperty(property: PropertyDeclaration): string | null;
39
- protected findEnumDeclarationByName(enumName: string): import("ts-morph").EnumDeclaration | null;
40
- protected unwrapNullishType(typeText: string): string;
41
- protected resolveSingleColumnType(type: Type, fallback: string): string;
42
- protected resolveColumnType(property: PropertyDeclaration): string;
43
- protected resolveAccessorType(accessor: GetAccessorDeclaration): string;
110
+ protected getClassName(target: Function | string, fallback: string): string;
111
+ protected ensureDatasourceMetadata(): Promise<void>;
112
+ protected isExcludedColumn(column: RuntimeColumn): boolean;
113
+ protected isDateColumn(column: RuntimeColumn): boolean;
114
+ protected isJsonColumn(column: RuntimeColumn): boolean;
115
+ protected resolveEnumTypeName(entityName: string, column: RuntimeColumn): string;
116
+ protected resolveSourcePropertyType(entityName: string, propertyName: string): string | null;
117
+ protected resolveColumnType(entityName: string, column: RuntimeColumn): string;
44
118
  protected appendNullType(typeText: string): string;
45
- protected propertyToColumnMeta(property: PropertyDeclaration): ColumnMeta;
46
- protected accessorToColumnMeta(accessor: GetAccessorDeclaration): ColumnMeta;
47
- protected isExcludedGetter(accessor: GetAccessorDeclaration): boolean;
48
- protected collectGetAccessors(classDeclaration: ClassDeclaration, visitedClassNames?: Set<string>): GetAccessorDeclaration[];
49
- protected unwrapRelationType(typeText: string): string;
50
- protected getRelationTargetClassName(property: PropertyDeclaration): string;
51
- protected isArrayRelation(property: PropertyDeclaration): boolean;
52
- protected isNullableRelation(property: PropertyDeclaration): boolean;
53
- protected propertyToRelationMeta(property: PropertyDeclaration): RelationMeta;
54
- protected createEntityMetas(entitySourceFiles: SourceFile[]): EntityMeta[];
119
+ protected columnToMeta(entityName: string, column: RuntimeColumn): ColumnMeta;
120
+ protected relationToMeta(relation: RuntimeRelation): RelationMeta;
121
+ protected createEntityMetas(): Promise<{
122
+ className: string;
123
+ dtoName: string;
124
+ moduleName: string;
125
+ dtoFileName: string;
126
+ mapperName: string;
127
+ filePath: string | undefined;
128
+ columns: ColumnMeta[];
129
+ relations: RelationMeta[];
130
+ }[]>;
55
131
  protected findMetaByClassName(metas: EntityMeta[], className: string): EntityMeta | undefined;
56
132
  protected getValidRelations(meta: EntityMeta, metas: EntityMeta[]): RelationMeta[];
57
133
  protected createDTOContent(meta: EntityMeta, metas: EntityMeta[]): string;
58
134
  protected collectExternalTypeImports(meta: EntityMeta, dtoFilePath: string, relationTypeNames: Set<string>): [string, string][];
59
135
  protected collectExternalTypeSymbols(meta: EntityMeta, relationTypeNames: Set<string>): Set<string>;
60
136
  protected collectAllExternalTypeSymbols(metas: EntityMeta[]): Set<string>;
61
- protected findTypeDeclarationByName(typeName: string): import("ts-morph").InterfaceDeclaration | import("ts-morph").EnumDeclaration | import("ts-morph").TypeAliasDeclaration | null;
62
- protected getCommonTypeReExportModuleSpecifier(typeName: string, commonTypesFilePath: string): {
63
- moduleSpecifier: string;
64
- isTypeOnly: boolean;
65
- } | null;
66
- protected normalizeDeclarationToExportText(typeName: string): string | null;
137
+ protected findTypeDeclarationByName(typeName: string): ts_morph.InterfaceDeclaration | ts_morph.EnumDeclaration | ts_morph.TypeAliasDeclaration | null;
138
+ protected findEnumDeclarationByName(typeName: string): ts_morph.EnumDeclaration | null;
139
+ protected normalizeDeclarationToExportText(typeName: string, preferEnum?: boolean): string | null;
140
+ protected formatEnumKey(value: string | number): string;
141
+ protected formatEnumValue(value: string | number): string;
142
+ protected createCommonEnumDeclaration(typeName: string, values: (string | number)[]): string;
67
143
  protected ensureCommonTypesFile(metas: EntityMeta[]): void;
68
- protected resolveCommonTypesSourceFile(): SourceFile | null;
144
+ protected resolveCommonTypesSourceContent(): string | null;
69
145
  protected validateCommonTypeCoverage(metas: EntityMeta[]): void;
70
146
  protected createDTOIndexContent(metas: EntityMeta[]): string;
147
+ protected getMetasWithFilePath(metas: EntityMeta[]): EntityMeta[];
148
+ protected createEntityImports(metas: EntityMeta[]): string;
149
+ protected getMapperEntityTypeName(meta: EntityMeta): string;
150
+ protected createStructuralEntityTypes(metas: EntityMeta[]): string;
71
151
  protected createGeneratedMapperContent(metas: EntityMeta[]): string;
72
152
  protected createMapperFunctionContent(meta: EntityMeta, metas: EntityMeta[]): string;
73
153
  protected createRelationMapperLine(relation: RelationMeta, metas: EntityMeta[]): string;
74
154
  }
75
- //# sourceMappingURL=index.d.ts.map
155
+
156
+ export { TORMDTOGenerator };