@mikro-orm/entity-generator 7.0.2-dev.9 → 7.0.2

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.
@@ -1,9 +1,9 @@
1
1
  import { type Dictionary, type EntityProperty } from '@mikro-orm/core';
2
2
  import { SourceFile } from './SourceFile.js';
3
3
  export declare class EntitySchemaSourceFile extends SourceFile {
4
- generate(): string;
5
- protected generateClassDefinition(): string;
6
- protected getPropertyOptions(prop: EntityProperty, quote?: boolean): Dictionary;
7
- protected getPropertyIndexesOptions(prop: EntityProperty, options: Dictionary): void;
8
- protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty, quote?: boolean): void;
4
+ generate(): string;
5
+ protected generateClassDefinition(): string;
6
+ protected getPropertyOptions(prop: EntityProperty, quote?: boolean): Dictionary;
7
+ protected getPropertyIndexesOptions(prop: EntityProperty, options: Dictionary): void;
8
+ protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty, quote?: boolean): void;
9
9
  }
@@ -1,151 +1,147 @@
1
- import { Config, ReferenceKind, } from '@mikro-orm/core';
1
+ import { Config, ReferenceKind } from '@mikro-orm/core';
2
2
  import { SourceFile } from './SourceFile.js';
3
3
  export class EntitySchemaSourceFile extends SourceFile {
4
- generate() {
5
- const classDefinition = this.generateClassDefinition();
6
- const enumDefinitions = [];
7
- for (const prop of Object.values(this.meta.properties)) {
8
- if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR)) {
9
- const def = this.getEnumClassDefinition(prop, 2);
10
- if (def.length) {
11
- enumDefinitions.push(def);
12
- }
13
- }
14
- }
15
- let ret = classDefinition;
16
- if (enumDefinitions.length) {
17
- ret += '\n' + enumDefinitions.join('\n');
18
- }
19
- ret += `\n`;
20
- const entitySchemaOptions = {
21
- class: this.meta.className,
22
- ...(this.meta.embeddable
23
- ? this.getEmbeddableDeclOptions()
24
- : this.meta.collection
25
- ? this.getEntityDeclOptions()
26
- : {}),
27
- };
28
- const declLine = `export const ${this.meta.className}Schema = new ${this.referenceCoreImport('EntitySchema')}(`;
29
- ret += declLine;
30
- if (this.meta.indexes.length > 0) {
31
- entitySchemaOptions.indexes = this.meta.indexes.map(index => this.getIndexOptions(index));
32
- }
33
- if (this.meta.uniques.length > 0) {
34
- entitySchemaOptions.uniques = this.meta.uniques.map(index => this.getUniqueOptions(index));
35
- }
36
- entitySchemaOptions.properties = Object.fromEntries(Object.entries(this.meta.properties).map(([name, prop]) => [name, this.getPropertyOptions(prop)]));
37
- // Force top level and properties to be indented, regardless of line length
38
- entitySchemaOptions[Config] = true;
39
- entitySchemaOptions.properties[Config] = true;
40
- ret += this.serializeObject(entitySchemaOptions, declLine.length > 80 ? undefined : 80 - declLine.length, 0);
41
- ret += ');\n';
42
- ret = `${this.generateImports()}\n\n${ret}`;
43
- return ret;
4
+ generate() {
5
+ const classDefinition = this.generateClassDefinition();
6
+ const enumDefinitions = [];
7
+ for (const prop of Object.values(this.meta.properties)) {
8
+ if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR)) {
9
+ const def = this.getEnumClassDefinition(prop, 2);
10
+ if (def.length) {
11
+ enumDefinitions.push(def);
12
+ }
13
+ }
44
14
  }
45
- generateClassDefinition() {
46
- let classBody = '';
47
- if (!this.options.customBaseEntityName || this.meta.className === this.options.customBaseEntityName) {
48
- const defineConfigTypeSettings = {};
49
- defineConfigTypeSettings.forceObject = this.platform.getConfig().get('serialization').forceObject ?? false;
50
- if (defineConfigTypeSettings.forceObject) {
51
- classBody += `${' '.repeat(2)}[${this.referenceCoreImport('Config')}]?: ${this.referenceCoreImport('DefineConfig')}<${this.serializeObject(defineConfigTypeSettings)}>;\n`;
52
- }
53
- }
54
- const eagerProperties = [];
55
- const primaryProps = [];
56
- const props = [];
57
- for (const prop of Object.values(this.meta.properties)) {
58
- props.push(this.getPropertyDefinition(prop, 2));
59
- if (prop.eager) {
60
- eagerProperties.push(prop);
61
- }
62
- if (prop.primary && (!['id', '_id', 'uuid'].includes(prop.name) || this.meta.compositePK)) {
63
- primaryProps.push(prop);
64
- }
65
- }
66
- if (primaryProps.length > 0) {
67
- const primaryPropNames = primaryProps.map(prop => `'${prop.name}'`);
68
- if (primaryProps.length > 1) {
69
- classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: [${primaryPropNames.join(', ')}];\n`;
70
- }
71
- else {
72
- classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: ${primaryPropNames[0]};\n`;
73
- }
74
- }
75
- if (eagerProperties.length > 0) {
76
- const eagerPropertyNames = eagerProperties.map(prop => `'${prop.name}'`).sort();
77
- classBody += `${' '.repeat(2)}[${this.referenceCoreImport('EagerProps')}]?: ${eagerPropertyNames.join(' | ')};\n`;
78
- }
79
- classBody += props.join('');
80
- return this.getEntityClass(classBody);
15
+ let ret = classDefinition;
16
+ if (enumDefinitions.length) {
17
+ ret += '\n' + enumDefinitions.join('\n');
81
18
  }
82
- getPropertyOptions(prop, quote = true) {
83
- const options = {};
84
- if (prop.primary) {
85
- options.primary = true;
86
- }
87
- if (typeof prop.kind !== 'undefined' && prop.kind !== ReferenceKind.SCALAR) {
88
- options.kind = this.quote(prop.kind);
89
- }
90
- if (prop.kind === ReferenceKind.MANY_TO_MANY) {
91
- this.getManyToManyDecoratorOptions(options, prop);
92
- }
93
- else if (prop.kind === ReferenceKind.ONE_TO_MANY) {
94
- this.getOneToManyDecoratorOptions(options, prop);
95
- }
96
- else if (prop.kind === ReferenceKind.SCALAR || typeof prop.kind === 'undefined') {
97
- this.getScalarPropertyDecoratorOptions(options, prop, quote);
98
- }
99
- else if (prop.kind === ReferenceKind.EMBEDDED) {
100
- this.getEmbeddedPropertyDeclarationOptions(options, prop);
101
- }
102
- else {
103
- this.getForeignKeyDecoratorOptions(options, prop);
104
- }
105
- if (prop.formula) {
106
- options.formula = `${prop.formula}`;
107
- }
108
- this.getCommonDecoratorOptions(options, prop);
109
- this.getPropertyIndexesOptions(prop, options);
110
- return options;
19
+ ret += `\n`;
20
+ const entitySchemaOptions = {
21
+ class: this.meta.className,
22
+ ...(this.meta.embeddable
23
+ ? this.getEmbeddableDeclOptions()
24
+ : this.meta.collection
25
+ ? this.getEntityDeclOptions()
26
+ : {}),
27
+ };
28
+ const declLine = `export const ${this.meta.className}Schema = new ${this.referenceCoreImport('EntitySchema')}(`;
29
+ ret += declLine;
30
+ if (this.meta.indexes.length > 0) {
31
+ entitySchemaOptions.indexes = this.meta.indexes.map(index => this.getIndexOptions(index));
111
32
  }
112
- getPropertyIndexesOptions(prop, options) {
113
- if (prop.kind === ReferenceKind.SCALAR) {
114
- if (prop.index) {
115
- options.index = this.quote(prop.index);
116
- }
117
- if (prop.unique) {
118
- options.unique = this.quote(prop.unique);
119
- }
120
- return;
121
- }
122
- const processIndex = (type) => {
123
- const propType = prop[type];
124
- if (!propType) {
125
- return;
126
- }
127
- const defaultName = this.platform.getIndexName(this.meta.collection, prop.fieldNames, type);
128
- /* v8 ignore next */
129
- options[type] = propType === true || defaultName === propType ? 'true' : this.quote(propType);
130
- const expected = {
131
- index: this.platform.indexForeignKeys(),
132
- unique: prop.kind === ReferenceKind.ONE_TO_ONE,
133
- };
134
- if (expected[type] && options[type] === 'true') {
135
- delete options[type];
136
- }
137
- };
138
- processIndex('index');
139
- processIndex('unique');
33
+ if (this.meta.uniques.length > 0) {
34
+ entitySchemaOptions.uniques = this.meta.uniques.map(index => this.getUniqueOptions(index));
140
35
  }
141
- getScalarPropertyDecoratorOptions(options, prop, quote = true) {
142
- if (prop.enum) {
143
- options.enum = true;
144
- options.items = `() => ${prop.runtimeType}`;
145
- }
146
- else {
147
- options.type = quote ? this.quote(prop.type) : prop.type;
148
- }
149
- super.getScalarPropertyDecoratorOptions(options, prop, quote);
36
+ entitySchemaOptions.properties = Object.fromEntries(
37
+ Object.entries(this.meta.properties).map(([name, prop]) => [name, this.getPropertyOptions(prop)]),
38
+ );
39
+ // Force top level and properties to be indented, regardless of line length
40
+ entitySchemaOptions[Config] = true;
41
+ entitySchemaOptions.properties[Config] = true;
42
+ ret += this.serializeObject(entitySchemaOptions, declLine.length > 80 ? undefined : 80 - declLine.length, 0);
43
+ ret += ');\n';
44
+ ret = `${this.generateImports()}\n\n${ret}`;
45
+ return ret;
46
+ }
47
+ generateClassDefinition() {
48
+ let classBody = '';
49
+ if (!this.options.customBaseEntityName || this.meta.className === this.options.customBaseEntityName) {
50
+ const defineConfigTypeSettings = {};
51
+ defineConfigTypeSettings.forceObject = this.platform.getConfig().get('serialization').forceObject ?? false;
52
+ if (defineConfigTypeSettings.forceObject) {
53
+ classBody += `${' '.repeat(2)}[${this.referenceCoreImport('Config')}]?: ${this.referenceCoreImport('DefineConfig')}<${this.serializeObject(defineConfigTypeSettings)}>;\n`;
54
+ }
55
+ }
56
+ const eagerProperties = [];
57
+ const primaryProps = [];
58
+ const props = [];
59
+ for (const prop of Object.values(this.meta.properties)) {
60
+ props.push(this.getPropertyDefinition(prop, 2));
61
+ if (prop.eager) {
62
+ eagerProperties.push(prop);
63
+ }
64
+ if (prop.primary && (!['id', '_id', 'uuid'].includes(prop.name) || this.meta.compositePK)) {
65
+ primaryProps.push(prop);
66
+ }
67
+ }
68
+ if (primaryProps.length > 0) {
69
+ const primaryPropNames = primaryProps.map(prop => `'${prop.name}'`);
70
+ if (primaryProps.length > 1) {
71
+ classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: [${primaryPropNames.join(', ')}];\n`;
72
+ } else {
73
+ classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: ${primaryPropNames[0]};\n`;
74
+ }
75
+ }
76
+ if (eagerProperties.length > 0) {
77
+ const eagerPropertyNames = eagerProperties.map(prop => `'${prop.name}'`).sort();
78
+ classBody += `${' '.repeat(2)}[${this.referenceCoreImport('EagerProps')}]?: ${eagerPropertyNames.join(' | ')};\n`;
79
+ }
80
+ classBody += props.join('');
81
+ return this.getEntityClass(classBody);
82
+ }
83
+ getPropertyOptions(prop, quote = true) {
84
+ const options = {};
85
+ if (prop.primary) {
86
+ options.primary = true;
87
+ }
88
+ if (typeof prop.kind !== 'undefined' && prop.kind !== ReferenceKind.SCALAR) {
89
+ options.kind = this.quote(prop.kind);
90
+ }
91
+ if (prop.kind === ReferenceKind.MANY_TO_MANY) {
92
+ this.getManyToManyDecoratorOptions(options, prop);
93
+ } else if (prop.kind === ReferenceKind.ONE_TO_MANY) {
94
+ this.getOneToManyDecoratorOptions(options, prop);
95
+ } else if (prop.kind === ReferenceKind.SCALAR || typeof prop.kind === 'undefined') {
96
+ this.getScalarPropertyDecoratorOptions(options, prop, quote);
97
+ } else if (prop.kind === ReferenceKind.EMBEDDED) {
98
+ this.getEmbeddedPropertyDeclarationOptions(options, prop);
99
+ } else {
100
+ this.getForeignKeyDecoratorOptions(options, prop);
101
+ }
102
+ if (prop.formula) {
103
+ options.formula = `${prop.formula}`;
104
+ }
105
+ this.getCommonDecoratorOptions(options, prop);
106
+ this.getPropertyIndexesOptions(prop, options);
107
+ return options;
108
+ }
109
+ getPropertyIndexesOptions(prop, options) {
110
+ if (prop.kind === ReferenceKind.SCALAR) {
111
+ if (prop.index) {
112
+ options.index = this.quote(prop.index);
113
+ }
114
+ if (prop.unique) {
115
+ options.unique = this.quote(prop.unique);
116
+ }
117
+ return;
118
+ }
119
+ const processIndex = type => {
120
+ const propType = prop[type];
121
+ if (!propType) {
122
+ return;
123
+ }
124
+ const defaultName = this.platform.getIndexName(this.meta.collection, prop.fieldNames, type);
125
+ /* v8 ignore next */
126
+ options[type] = propType === true || defaultName === propType ? 'true' : this.quote(propType);
127
+ const expected = {
128
+ index: this.platform.indexForeignKeys(),
129
+ unique: prop.kind === ReferenceKind.ONE_TO_ONE,
130
+ };
131
+ if (expected[type] && options[type] === 'true') {
132
+ delete options[type];
133
+ }
134
+ };
135
+ processIndex('index');
136
+ processIndex('unique');
137
+ }
138
+ getScalarPropertyDecoratorOptions(options, prop, quote = true) {
139
+ if (prop.enum) {
140
+ options.enum = true;
141
+ options.items = `() => ${prop.runtimeType}`;
142
+ } else {
143
+ options.type = quote ? this.quote(prop.type) : prop.type;
150
144
  }
145
+ super.getScalarPropertyDecoratorOptions(options, prop, quote);
146
+ }
151
147
  }
@@ -1,12 +1,18 @@
1
1
  import type { EntityMetadata, GenerateOptions, NamingStrategy, Platform } from '@mikro-orm/core';
2
2
  import { SourceFile } from './SourceFile.js';
3
3
  export declare class NativeEnumSourceFile extends SourceFile {
4
- private readonly nativeEnum;
5
- constructor(meta: EntityMetadata, namingStrategy: NamingStrategy, platform: Platform, options: GenerateOptions, nativeEnum: {
6
- name: string;
7
- schema?: string;
8
- items: string[];
9
- });
10
- generate(): string;
11
- getBaseName(extension?: string): string;
4
+ private readonly nativeEnum;
5
+ constructor(
6
+ meta: EntityMetadata,
7
+ namingStrategy: NamingStrategy,
8
+ platform: Platform,
9
+ options: GenerateOptions,
10
+ nativeEnum: {
11
+ name: string;
12
+ schema?: string;
13
+ items: string[];
14
+ },
15
+ );
16
+ generate(): string;
17
+ getBaseName(extension?: string): string;
12
18
  }
@@ -1,47 +1,49 @@
1
1
  import { identifierRegex, SourceFile } from './SourceFile.js';
2
2
  export class NativeEnumSourceFile extends SourceFile {
3
- nativeEnum;
4
- constructor(meta, namingStrategy, platform, options, nativeEnum) {
5
- super(meta, namingStrategy, platform, options);
6
- this.nativeEnum = nativeEnum;
3
+ nativeEnum;
4
+ constructor(meta, namingStrategy, platform, options, nativeEnum) {
5
+ super(meta, namingStrategy, platform, options);
6
+ this.nativeEnum = nativeEnum;
7
+ }
8
+ generate() {
9
+ const enumClassName = this.namingStrategy.getEnumClassName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
10
+ const enumTypeName = this.namingStrategy.getEnumTypeName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
11
+ const padding = ' ';
12
+ const enumMode = this.options.enumMode;
13
+ const enumValues = this.nativeEnum.items;
14
+ if (enumMode === 'union-type') {
15
+ return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
7
16
  }
8
- generate() {
9
- const enumClassName = this.namingStrategy.getEnumClassName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
10
- const enumTypeName = this.namingStrategy.getEnumTypeName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
11
- const padding = ' ';
12
- const enumMode = this.options.enumMode;
13
- const enumValues = this.nativeEnum.items;
14
- if (enumMode === 'union-type') {
15
- return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
16
- }
17
- let ret = '';
18
- if (enumMode === 'dictionary') {
19
- ret += `export const ${enumClassName} = {\n`;
20
- }
21
- else {
22
- ret += `export enum ${enumClassName} {\n`;
23
- }
24
- for (const enumValue of enumValues) {
25
- const enumName = this.namingStrategy.enumValueToEnumProperty(enumValue, this.nativeEnum.name, '', this.nativeEnum.schema);
26
- if (enumMode === 'dictionary') {
27
- ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
28
- }
29
- else {
30
- ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
31
- }
32
- }
33
- if (enumMode === 'dictionary') {
34
- ret += '} as const;\n';
35
- }
36
- else {
37
- ret += '}\n';
38
- }
39
- if (enumMode === 'dictionary') {
40
- ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
41
- }
42
- return ret;
17
+ let ret = '';
18
+ if (enumMode === 'dictionary') {
19
+ ret += `export const ${enumClassName} = {\n`;
20
+ } else {
21
+ ret += `export enum ${enumClassName} {\n`;
43
22
  }
44
- getBaseName(extension = '.ts') {
45
- return `${this.options.fileName(this.nativeEnum.name)}${extension}`;
23
+ for (const enumValue of enumValues) {
24
+ const enumName = this.namingStrategy.enumValueToEnumProperty(
25
+ enumValue,
26
+ this.nativeEnum.name,
27
+ '',
28
+ this.nativeEnum.schema,
29
+ );
30
+ if (enumMode === 'dictionary') {
31
+ ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
32
+ } else {
33
+ ret += `${padding}${identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
34
+ }
46
35
  }
36
+ if (enumMode === 'dictionary') {
37
+ ret += '} as const;\n';
38
+ } else {
39
+ ret += '}\n';
40
+ }
41
+ if (enumMode === 'dictionary') {
42
+ ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
43
+ }
44
+ return ret;
45
+ }
46
+ getBaseName(extension = '.ts') {
47
+ return `${this.options.fileName(this.nativeEnum.name)}${extension}`;
48
+ }
47
49
  }