@mikro-orm/entity-generator 7.0.4-dev.9 → 7.0.4
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/CoreImportsHelper.d.ts +10 -1
- package/CoreImportsHelper.js +1 -10
- package/DefineEntitySourceFile.d.ts +2 -2
- package/DefineEntitySourceFile.js +140 -139
- package/EntityGenerator.d.ts +12 -12
- package/EntityGenerator.js +377 -343
- package/EntitySchemaSourceFile.d.ts +5 -5
- package/EntitySchemaSourceFile.js +139 -143
- package/NativeEnumSourceFile.d.ts +14 -8
- package/NativeEnumSourceFile.js +43 -41
- package/README.md +1 -1
- package/SourceFile.d.ts +59 -41
- package/SourceFile.js +987 -919
- package/package.json +4 -4
package/CoreImportsHelper.d.ts
CHANGED
|
@@ -1 +1,10 @@
|
|
|
1
|
-
export declare const POSSIBLE_TYPE_IMPORTS: readonly [
|
|
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
|
+
];
|
package/CoreImportsHelper.js
CHANGED
|
@@ -1,10 +1 @@
|
|
|
1
|
-
export const POSSIBLE_TYPE_IMPORTS = [
|
|
2
|
-
'DefineConfig',
|
|
3
|
-
'Hidden',
|
|
4
|
-
'IType',
|
|
5
|
-
'Opt',
|
|
6
|
-
'Ref',
|
|
7
|
-
'EntityRef',
|
|
8
|
-
'ScalarRef',
|
|
9
|
-
'Rel',
|
|
10
|
-
];
|
|
1
|
+
export const POSSIBLE_TYPE_IMPORTS = ['DefineConfig', 'Hidden', 'IType', 'Opt', 'Ref', 'EntityRef', 'ScalarRef', 'Rel'];
|
|
@@ -1,146 +1,147 @@
|
|
|
1
|
-
import { Config, ReferenceKind, types
|
|
1
|
+
import { Config, ReferenceKind, types } from '@mikro-orm/core';
|
|
2
2
|
import { EntitySchemaSourceFile } from './EntitySchemaSourceFile.js';
|
|
3
3
|
export class DefineEntitySourceFile extends EntitySchemaSourceFile {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
}
|
|
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);
|
|
13
11
|
}
|
|
14
|
-
|
|
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
|
-
}
|
|
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;
|
|
12
|
+
}
|
|
52
13
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
}
|
|
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;
|
|
143
83
|
}
|
|
144
|
-
|
|
84
|
+
}
|
|
85
|
+
}
|
|
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
|
+
}
|
|
145
144
|
}
|
|
145
|
+
return builder;
|
|
146
|
+
}
|
|
146
147
|
}
|
package/EntityGenerator.d.ts
CHANGED
|
@@ -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
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
}
|