@mikro-orm/entity-generator 7.0.4 → 7.0.5-dev.0

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,10 +1 @@
1
- export declare const POSSIBLE_TYPE_IMPORTS: readonly [
2
- 'DefineConfig',
3
- 'Hidden',
4
- 'IType',
5
- 'Opt',
6
- 'Ref',
7
- 'EntityRef',
8
- 'ScalarRef',
9
- 'Rel',
10
- ];
1
+ export declare const POSSIBLE_TYPE_IMPORTS: readonly ["DefineConfig", "Hidden", "IType", "Opt", "Ref", "EntityRef", "ScalarRef", "Rel"];
@@ -1 +1,10 @@
1
- export const POSSIBLE_TYPE_IMPORTS = ['DefineConfig', 'Hidden', 'IType', 'Opt', 'Ref', 'EntityRef', 'ScalarRef', 'Rel'];
1
+ export const POSSIBLE_TYPE_IMPORTS = [
2
+ 'DefineConfig',
3
+ 'Hidden',
4
+ 'IType',
5
+ 'Opt',
6
+ 'Ref',
7
+ 'EntityRef',
8
+ 'ScalarRef',
9
+ 'Rel',
10
+ ];
@@ -1,5 +1,5 @@
1
1
  import { EntitySchemaSourceFile } from './EntitySchemaSourceFile.js';
2
2
  export declare class DefineEntitySourceFile extends EntitySchemaSourceFile {
3
- generate(): string;
4
- private getPropertyBuilder;
3
+ generate(): string;
4
+ private getPropertyBuilder;
5
5
  }
@@ -1,147 +1,146 @@
1
- import { Config, ReferenceKind, types } from '@mikro-orm/core';
1
+ import { Config, ReferenceKind, types, } from '@mikro-orm/core';
2
2
  import { EntitySchemaSourceFile } from './EntitySchemaSourceFile.js';
3
3
  export class DefineEntitySourceFile extends EntitySchemaSourceFile {
4
- generate() {
5
- const enumDefinitions = [];
6
- for (const prop of Object.values(this.meta.properties)) {
7
- if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR)) {
8
- const def = this.getEnumClassDefinition(prop, 2);
9
- if (def.length) {
10
- enumDefinitions.push(def);
4
+ generate() {
5
+ const enumDefinitions = [];
6
+ for (const prop of Object.values(this.meta.properties)) {
7
+ if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === ReferenceKind.SCALAR)) {
8
+ const def = this.getEnumClassDefinition(prop, 2);
9
+ if (def.length) {
10
+ enumDefinitions.push(def);
11
+ }
12
+ }
11
13
  }
12
- }
13
- }
14
- let ret = '';
15
- if (!this.options.inferEntityType) {
16
- ret += this.generateClassDefinition() + '\n';
17
- }
18
- if (enumDefinitions.length) {
19
- ret += enumDefinitions.join('\n') + '\n';
20
- }
21
- const entitySchemaOptions = {};
22
- if (this.options.inferEntityType) {
23
- entitySchemaOptions.name = this.quote(this.meta.className);
24
- if (this.meta.compositePK) {
25
- entitySchemaOptions.primaryKeys = this.meta.getPrimaryProps().map(p => this.quote(p.name));
26
- }
27
- } else {
28
- entitySchemaOptions.class = this.meta.className;
29
- }
30
- Object.assign(
31
- entitySchemaOptions,
32
- this.meta.embeddable ? this.getEmbeddableDeclOptions() : this.meta.collection ? this.getEntityDeclOptions() : {},
33
- );
34
- const nameSuffix = this.options.inferEntityType ? '' : 'Schema';
35
- const declLine = `export const ${this.meta.className + nameSuffix} = ${this.referenceCoreImport('defineEntity')}(`;
36
- ret += declLine;
37
- if (this.meta.indexes.length > 0) {
38
- entitySchemaOptions.indexes = this.meta.indexes.map(index => this.getIndexOptions(index));
39
- }
40
- if (this.meta.uniques.length > 0) {
41
- entitySchemaOptions.uniques = this.meta.uniques.map(index => this.getUniqueOptions(index));
42
- }
43
- entitySchemaOptions.properties = Object.fromEntries(
44
- Object.entries(this.meta.properties).map(([name, prop]) => [name, this.getPropertyBuilder(prop)]),
45
- );
46
- // Force top level and properties to be indented, regardless of line length
47
- entitySchemaOptions[Config] = true;
48
- entitySchemaOptions.properties[Config] = true;
49
- ret += this.serializeObject(entitySchemaOptions, declLine.length > 80 ? undefined : 80 - declLine.length, 0);
50
- ret += ');\n';
51
- if (this.options.inferEntityType) {
52
- ret += `\nexport interface I${this.meta.className} extends ${this.referenceCoreImport('InferEntity')}<typeof ${this.meta.className}> {}\n`;
53
- }
54
- ret = `${this.generateImports()}\n\n${ret}`;
55
- return ret;
56
- }
57
- getPropertyBuilder(prop) {
58
- const options = this.getPropertyOptions(prop, false);
59
- const p = this.referenceCoreImport('p');
60
- let builder = '';
61
- switch (prop.kind) {
62
- case ReferenceKind.ONE_TO_ONE:
63
- builder += `() => ${p}.oneToOne(${prop.type})`;
64
- break;
65
- case ReferenceKind.ONE_TO_MANY:
66
- builder += `() => ${p}.oneToMany(${prop.type})`;
67
- break;
68
- case ReferenceKind.MANY_TO_ONE:
69
- builder += `() => ${p}.manyToOne(${prop.type})`;
70
- break;
71
- case ReferenceKind.MANY_TO_MANY:
72
- builder += `() => ${p}.manyToMany(${prop.type})`;
73
- break;
74
- case ReferenceKind.EMBEDDED:
75
- builder += `() => ${p}.embedded(${prop.type})`;
76
- break;
77
- case ReferenceKind.SCALAR:
78
- default: {
79
- if (options.type && !(options.type in types)) {
80
- builder += `${p}.type(${options.type})`;
81
- } else {
82
- builder += options.type ? `${p}.${options.type}()` : p;
14
+ let ret = '';
15
+ if (!this.options.inferEntityType) {
16
+ ret += this.generateClassDefinition() + '\n';
17
+ }
18
+ if (enumDefinitions.length) {
19
+ ret += enumDefinitions.join('\n') + '\n';
83
20
  }
84
- }
21
+ const entitySchemaOptions = {};
22
+ if (this.options.inferEntityType) {
23
+ entitySchemaOptions.name = this.quote(this.meta.className);
24
+ if (this.meta.compositePK) {
25
+ entitySchemaOptions.primaryKeys = this.meta.getPrimaryProps().map(p => this.quote(p.name));
26
+ }
27
+ }
28
+ else {
29
+ entitySchemaOptions.class = this.meta.className;
30
+ }
31
+ Object.assign(entitySchemaOptions, this.meta.embeddable ? this.getEmbeddableDeclOptions() : this.meta.collection ? this.getEntityDeclOptions() : {});
32
+ const nameSuffix = this.options.inferEntityType ? '' : 'Schema';
33
+ const declLine = `export const ${this.meta.className + nameSuffix} = ${this.referenceCoreImport('defineEntity')}(`;
34
+ ret += declLine;
35
+ if (this.meta.indexes.length > 0) {
36
+ entitySchemaOptions.indexes = this.meta.indexes.map(index => this.getIndexOptions(index));
37
+ }
38
+ if (this.meta.uniques.length > 0) {
39
+ entitySchemaOptions.uniques = this.meta.uniques.map(index => this.getUniqueOptions(index));
40
+ }
41
+ entitySchemaOptions.properties = Object.fromEntries(Object.entries(this.meta.properties).map(([name, prop]) => [name, this.getPropertyBuilder(prop)]));
42
+ // Force top level and properties to be indented, regardless of line length
43
+ entitySchemaOptions[Config] = true;
44
+ entitySchemaOptions.properties[Config] = true;
45
+ ret += this.serializeObject(entitySchemaOptions, declLine.length > 80 ? undefined : 80 - declLine.length, 0);
46
+ ret += ');\n';
47
+ if (this.options.inferEntityType) {
48
+ ret += `\nexport interface I${this.meta.className} extends ${this.referenceCoreImport('InferEntity')}<typeof ${this.meta.className}> {}\n`;
49
+ }
50
+ ret = `${this.generateImports()}\n\n${ret}`;
51
+ return ret;
85
52
  }
86
- const simpleOptions = new Set([
87
- 'primary',
88
- 'ref',
89
- 'nullable',
90
- 'array',
91
- 'object',
92
- 'mapToPk',
93
- 'hidden',
94
- 'concurrencyCheck',
95
- 'lazy',
96
- 'eager',
97
- 'orphanRemoval',
98
- 'version',
99
- 'unsigned',
100
- 'returning',
101
- 'createForeignKeyConstraint',
102
- 'fixedOrder',
103
- 'owner',
104
- 'getter',
105
- 'setter',
106
- 'unique',
107
- 'index',
108
- 'hydrate',
109
- 'persist',
110
- 'autoincrement',
111
- ]);
112
- const skipOptions = new Set(['entity', 'kind', 'type', 'items']);
113
- const spreadOptions = new Set([
114
- 'fieldNames',
115
- 'joinColumns',
116
- 'inverseJoinColumns',
117
- 'referencedColumnNames',
118
- 'ownColumns',
119
- 'columnTypes',
120
- 'cascade',
121
- 'ignoreSchemaChanges',
122
- 'customOrder',
123
- 'groups',
124
- 'where',
125
- 'orderBy',
126
- ]);
127
- const rename = {
128
- fieldName: 'name',
129
- };
130
- for (const key of Object.keys(options)) {
131
- if (typeof options[key] === 'undefined' || skipOptions.has(key)) {
132
- continue;
133
- }
134
- const method = rename[key] ?? key;
135
- const params = simpleOptions.has(key) && options[key] === true ? '' : options[key];
136
- builder += `.${method}`;
137
- if (key === 'enum') {
138
- builder += `(${options.items})`;
139
- } else if (spreadOptions.has(key) && typeof params === 'string' && params.startsWith('[')) {
140
- builder += `(${params.slice(1, -1)})`;
141
- } else {
142
- builder += `(${params})`;
143
- }
53
+ getPropertyBuilder(prop) {
54
+ const options = this.getPropertyOptions(prop, false);
55
+ const p = this.referenceCoreImport('p');
56
+ let builder = '';
57
+ switch (prop.kind) {
58
+ case ReferenceKind.ONE_TO_ONE:
59
+ builder += `() => ${p}.oneToOne(${prop.type})`;
60
+ break;
61
+ case ReferenceKind.ONE_TO_MANY:
62
+ builder += `() => ${p}.oneToMany(${prop.type})`;
63
+ break;
64
+ case ReferenceKind.MANY_TO_ONE:
65
+ builder += `() => ${p}.manyToOne(${prop.type})`;
66
+ break;
67
+ case ReferenceKind.MANY_TO_MANY:
68
+ builder += `() => ${p}.manyToMany(${prop.type})`;
69
+ break;
70
+ case ReferenceKind.EMBEDDED:
71
+ builder += `() => ${p}.embedded(${prop.type})`;
72
+ break;
73
+ case ReferenceKind.SCALAR:
74
+ default: {
75
+ if (options.type && !(options.type in types)) {
76
+ builder += `${p}.type(${options.type})`;
77
+ }
78
+ else {
79
+ builder += options.type ? `${p}.${options.type}()` : p;
80
+ }
81
+ }
82
+ }
83
+ const simpleOptions = new Set([
84
+ 'primary',
85
+ 'ref',
86
+ 'nullable',
87
+ 'array',
88
+ 'object',
89
+ 'mapToPk',
90
+ 'hidden',
91
+ 'concurrencyCheck',
92
+ 'lazy',
93
+ 'eager',
94
+ 'orphanRemoval',
95
+ 'version',
96
+ 'unsigned',
97
+ 'returning',
98
+ 'createForeignKeyConstraint',
99
+ 'fixedOrder',
100
+ 'owner',
101
+ 'getter',
102
+ 'setter',
103
+ 'unique',
104
+ 'index',
105
+ 'hydrate',
106
+ 'persist',
107
+ 'autoincrement',
108
+ ]);
109
+ const skipOptions = new Set(['entity', 'kind', 'type', 'items']);
110
+ const spreadOptions = new Set([
111
+ 'fieldNames',
112
+ 'joinColumns',
113
+ 'inverseJoinColumns',
114
+ 'referencedColumnNames',
115
+ 'ownColumns',
116
+ 'columnTypes',
117
+ 'cascade',
118
+ 'ignoreSchemaChanges',
119
+ 'customOrder',
120
+ 'groups',
121
+ 'where',
122
+ 'orderBy',
123
+ ]);
124
+ const rename = {
125
+ fieldName: 'name',
126
+ };
127
+ for (const key of Object.keys(options)) {
128
+ if (typeof options[key] === 'undefined' || skipOptions.has(key)) {
129
+ continue;
130
+ }
131
+ const method = rename[key] ?? key;
132
+ const params = simpleOptions.has(key) && options[key] === true ? '' : options[key];
133
+ builder += `.${method}`;
134
+ if (key === 'enum') {
135
+ builder += `(${options.items})`;
136
+ }
137
+ else if (spreadOptions.has(key) && typeof params === 'string' && params.startsWith('[')) {
138
+ builder += `(${params.slice(1, -1)})`;
139
+ }
140
+ else {
141
+ builder += `(${params})`;
142
+ }
143
+ }
144
+ return builder;
144
145
  }
145
- return builder;
146
- }
147
146
  }
@@ -2,16 +2,16 @@ import { type GenerateOptions, type MikroORM } from '@mikro-orm/core';
2
2
  import { type EntityManager } from '@mikro-orm/sql';
3
3
  /** Generates entity source files by introspecting an existing database schema. */
4
4
  export declare class EntityGenerator {
5
- #private;
6
- constructor(em: EntityManager);
7
- static register(orm: MikroORM): void;
8
- generate(options?: GenerateOptions): Promise<string[]>;
9
- private getEntityMetadata;
10
- private cleanUpReferentialIntegrityRules;
11
- private matchName;
12
- private detectManyToManyRelations;
13
- private generateBidirectionalRelations;
14
- private generateIdentifiedReferences;
15
- private generateAndAttachCustomBaseEntity;
16
- private castNullDefaultsToUndefined;
5
+ #private;
6
+ constructor(em: EntityManager);
7
+ static register(orm: MikroORM): void;
8
+ generate(options?: GenerateOptions): Promise<string[]>;
9
+ private getEntityMetadata;
10
+ private cleanUpReferentialIntegrityRules;
11
+ private matchName;
12
+ private detectManyToManyRelations;
13
+ private generateBidirectionalRelations;
14
+ private generateIdentifiedReferences;
15
+ private generateAndAttachCustomBaseEntity;
16
+ private castNullDefaultsToUndefined;
17
17
  }