@mikro-orm/entity-generator 6.5.10-dev.19 → 6.5.10-dev.20
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/DefineEntitySourceFile.js +4 -1
- package/EntityGenerator.js +4 -0
- package/EntitySchemaSourceFile.js +4 -1
- package/NativeEnumSourceFile.d.ts +16 -0
- package/NativeEnumSourceFile.js +51 -0
- package/SourceFile.d.ts +1 -0
- package/SourceFile.js +54 -16
- package/package.json +3 -3
|
@@ -8,7 +8,10 @@ class DefineEntitySourceFile extends EntitySchemaSourceFile_1.EntitySchemaSource
|
|
|
8
8
|
const enumDefinitions = [];
|
|
9
9
|
for (const prop of Object.values(this.meta.properties)) {
|
|
10
10
|
if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR)) {
|
|
11
|
-
|
|
11
|
+
const def = this.getEnumClassDefinition(prop, 2);
|
|
12
|
+
if (def.length) {
|
|
13
|
+
enumDefinitions.push(def);
|
|
14
|
+
}
|
|
12
15
|
}
|
|
13
16
|
}
|
|
14
17
|
let ret = '';
|
package/EntityGenerator.js
CHANGED
|
@@ -7,6 +7,7 @@ const node_path_1 = require("node:path");
|
|
|
7
7
|
const fs_extra_1 = require("fs-extra");
|
|
8
8
|
const DefineEntitySourceFile_1 = require("./DefineEntitySourceFile");
|
|
9
9
|
const EntitySchemaSourceFile_1 = require("./EntitySchemaSourceFile");
|
|
10
|
+
const NativeEnumSourceFile_1 = require("./NativeEnumSourceFile");
|
|
10
11
|
const SourceFile_1 = require("./SourceFile");
|
|
11
12
|
class EntityGenerator {
|
|
12
13
|
em;
|
|
@@ -54,6 +55,9 @@ class EntityGenerator {
|
|
|
54
55
|
}
|
|
55
56
|
this.sources.push(new map[options.entityDefinition](meta, this.namingStrategy, this.platform, options));
|
|
56
57
|
}
|
|
58
|
+
for (const nativeEnum of Object.values(schema.getNativeEnums())) {
|
|
59
|
+
this.sources.push(new NativeEnumSourceFile_1.NativeEnumSourceFile({}, this.namingStrategy, this.platform, options, nativeEnum));
|
|
60
|
+
}
|
|
57
61
|
const files = this.sources.map(file => [file.getBaseName(), file.generate()]);
|
|
58
62
|
if (options.save) {
|
|
59
63
|
await (0, fs_extra_1.ensureDir)(baseDir);
|
|
@@ -9,7 +9,10 @@ class EntitySchemaSourceFile extends SourceFile_1.SourceFile {
|
|
|
9
9
|
const enumDefinitions = [];
|
|
10
10
|
for (const prop of Object.values(this.meta.properties)) {
|
|
11
11
|
if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR)) {
|
|
12
|
-
|
|
12
|
+
const def = this.getEnumClassDefinition(prop, 2);
|
|
13
|
+
if (def.length) {
|
|
14
|
+
enumDefinitions.push(def);
|
|
15
|
+
}
|
|
13
16
|
}
|
|
14
17
|
}
|
|
15
18
|
let ret = classDefinition;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EntityMetadata, GenerateOptions, NamingStrategy, Platform } from '@mikro-orm/core';
|
|
2
|
+
import { SourceFile } from './SourceFile';
|
|
3
|
+
export declare class NativeEnumSourceFile extends SourceFile {
|
|
4
|
+
protected readonly nativeEnum: {
|
|
5
|
+
name: string;
|
|
6
|
+
schema?: string;
|
|
7
|
+
items: string[];
|
|
8
|
+
};
|
|
9
|
+
constructor(meta: EntityMetadata, namingStrategy: NamingStrategy, platform: Platform, options: GenerateOptions, nativeEnum: {
|
|
10
|
+
name: string;
|
|
11
|
+
schema?: string;
|
|
12
|
+
items: string[];
|
|
13
|
+
});
|
|
14
|
+
generate(): string;
|
|
15
|
+
getBaseName(extension?: string): string;
|
|
16
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NativeEnumSourceFile = void 0;
|
|
4
|
+
const SourceFile_1 = require("./SourceFile");
|
|
5
|
+
class NativeEnumSourceFile extends SourceFile_1.SourceFile {
|
|
6
|
+
nativeEnum;
|
|
7
|
+
constructor(meta, namingStrategy, platform, options, nativeEnum) {
|
|
8
|
+
super(meta, namingStrategy, platform, options);
|
|
9
|
+
this.nativeEnum = nativeEnum;
|
|
10
|
+
}
|
|
11
|
+
generate() {
|
|
12
|
+
const enumClassName = this.namingStrategy.getEnumClassName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
|
|
13
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(this.nativeEnum.name, undefined, this.nativeEnum.schema);
|
|
14
|
+
const padding = ' ';
|
|
15
|
+
const enumMode = this.options.enumMode;
|
|
16
|
+
const enumValues = this.nativeEnum.items;
|
|
17
|
+
if (enumMode === 'union-type') {
|
|
18
|
+
return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
|
|
19
|
+
}
|
|
20
|
+
let ret = '';
|
|
21
|
+
if (enumMode === 'dictionary') {
|
|
22
|
+
ret += `export const ${enumClassName} = {\n`;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
ret += `export enum ${enumClassName} {\n`;
|
|
26
|
+
}
|
|
27
|
+
for (const enumValue of enumValues) {
|
|
28
|
+
const enumName = this.namingStrategy.enumValueToEnumProperty(enumValue, this.nativeEnum.name, '', this.nativeEnum.schema);
|
|
29
|
+
if (enumMode === 'dictionary') {
|
|
30
|
+
ret += `${padding}${SourceFile_1.identifierRegex.test(enumName) ? enumName : this.quote(enumName)}: ${this.quote(enumValue)},\n`;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
ret += `${padding}${SourceFile_1.identifierRegex.test(enumName) ? enumName : this.quote(enumName)} = ${this.quote(enumValue)},\n`;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (enumMode === 'dictionary') {
|
|
37
|
+
ret += '} as const;\n';
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
ret += '}\n';
|
|
41
|
+
}
|
|
42
|
+
if (enumMode === 'dictionary') {
|
|
43
|
+
ret += `\nexport type ${enumTypeName} = (typeof ${enumClassName})[keyof typeof ${enumClassName}];\n`;
|
|
44
|
+
}
|
|
45
|
+
return ret;
|
|
46
|
+
}
|
|
47
|
+
getBaseName(extension = '.ts') {
|
|
48
|
+
return `${this.options.fileName(this.nativeEnum.name)}${extension}`;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.NativeEnumSourceFile = NativeEnumSourceFile;
|
package/SourceFile.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export declare class SourceFile {
|
|
|
10
10
|
protected readonly options: GenerateOptions;
|
|
11
11
|
protected readonly coreImports: Set<string>;
|
|
12
12
|
protected readonly entityImports: Set<string>;
|
|
13
|
+
protected readonly enumImports: Map<string, string[]>;
|
|
13
14
|
constructor(meta: EntityMetadata, namingStrategy: NamingStrategy, platform: Platform, options: GenerateOptions);
|
|
14
15
|
generate(): string;
|
|
15
16
|
protected getIndexOptions(index: EntityMetadata['indexes'][number], isAtEntityLevel?: boolean): IndexOptions<Dictionary, string>;
|
package/SourceFile.js
CHANGED
|
@@ -17,6 +17,7 @@ class SourceFile {
|
|
|
17
17
|
options;
|
|
18
18
|
coreImports = new Set();
|
|
19
19
|
entityImports = new Set();
|
|
20
|
+
enumImports = new Map();
|
|
20
21
|
constructor(meta, namingStrategy, platform, options) {
|
|
21
22
|
this.meta = meta;
|
|
22
23
|
this.namingStrategy = namingStrategy;
|
|
@@ -68,7 +69,10 @@ class SourceFile {
|
|
|
68
69
|
classBody += definition;
|
|
69
70
|
classBody += '\n';
|
|
70
71
|
if (prop.enum) {
|
|
71
|
-
|
|
72
|
+
const def = this.getEnumClassDefinition(prop, 2);
|
|
73
|
+
if (def.length) {
|
|
74
|
+
enumDefinitions.push(def);
|
|
75
|
+
}
|
|
72
76
|
}
|
|
73
77
|
if (prop.eager) {
|
|
74
78
|
eagerProperties.push(prop);
|
|
@@ -149,32 +153,45 @@ class SourceFile {
|
|
|
149
153
|
const basePath = (0, node_path_1.relative)(dir, this.options.path ?? '.') || '.';
|
|
150
154
|
(this.options.extraImports?.(basePath, base) ?? []).forEach(v => this.entityImports.add(v));
|
|
151
155
|
const entityImports = [...this.entityImports].filter(e => e !== this.meta.className);
|
|
152
|
-
|
|
156
|
+
const importMap = new Map();
|
|
157
|
+
for (const entity of entityImports) {
|
|
153
158
|
const file = this.options.onImport?.(entity, basePath, extension, base) ?? {
|
|
154
159
|
path: `${basePath}/${this.options.fileName(entity)}${extension}`,
|
|
155
160
|
name: entity,
|
|
156
161
|
};
|
|
157
162
|
if (file.path === '') {
|
|
158
163
|
if (file.name === '') {
|
|
159
|
-
|
|
164
|
+
continue;
|
|
160
165
|
}
|
|
161
|
-
|
|
162
|
-
|
|
166
|
+
importMap.set(file.path, `import ${this.quote(file.name)};`);
|
|
167
|
+
continue;
|
|
163
168
|
}
|
|
164
169
|
if (file.name === '') {
|
|
165
|
-
|
|
166
|
-
|
|
170
|
+
importMap.set(file.path, `import * as ${entity} from ${this.quote(file.path)};`);
|
|
171
|
+
continue;
|
|
167
172
|
}
|
|
168
173
|
if (file.name === 'default') {
|
|
169
|
-
|
|
170
|
-
|
|
174
|
+
importMap.set(file.path, `import ${entity} from ${this.quote(file.path)};`);
|
|
175
|
+
continue;
|
|
171
176
|
}
|
|
172
177
|
if (file.name === entity) {
|
|
173
|
-
|
|
174
|
-
|
|
178
|
+
importMap.set(file.path, `import { ${entity} } from ${this.quote(file.path)};`);
|
|
179
|
+
continue;
|
|
175
180
|
}
|
|
176
|
-
|
|
177
|
-
}
|
|
181
|
+
importMap.set(file.path, `import { ${exports.identifierRegex.test(file.name) ? file.name : this.quote(file.name)} as ${entity} } from ${this.quote(file.path)};`);
|
|
182
|
+
}
|
|
183
|
+
if (this.enumImports.size) {
|
|
184
|
+
for (const [name, exports] of this.enumImports.entries()) {
|
|
185
|
+
const file = this.options.onImport?.(name, basePath, extension, base) ?? {
|
|
186
|
+
path: `${basePath}/${this.options.fileName(name)}${extension}`,
|
|
187
|
+
name,
|
|
188
|
+
};
|
|
189
|
+
importMap.set(file.path, `import { ${exports.join(', ')} } from ${this.quote(file.path)};`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
for (const key of [...importMap.keys()].sort()) {
|
|
193
|
+
imports.add(importMap.get(key));
|
|
194
|
+
}
|
|
178
195
|
return Array.from(imports.values()).join('\n');
|
|
179
196
|
}
|
|
180
197
|
getEntityClass(classBody) {
|
|
@@ -223,6 +240,9 @@ class SourceFile {
|
|
|
223
240
|
if (isScalar) {
|
|
224
241
|
if (prop.enum) {
|
|
225
242
|
const method = enumMode === 'ts-enum' ? 'getEnumClassName' : 'getEnumTypeName';
|
|
243
|
+
if (prop.nativeEnumName) {
|
|
244
|
+
return this.namingStrategy[method](prop.nativeEnumName, undefined, this.meta.schema);
|
|
245
|
+
}
|
|
226
246
|
return this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
227
247
|
}
|
|
228
248
|
breakdownOfIType = this.breakdownOfIType(prop);
|
|
@@ -258,11 +278,12 @@ class SourceFile {
|
|
|
258
278
|
return `${padding}${ret};\n`;
|
|
259
279
|
}
|
|
260
280
|
if (prop.enum && typeof prop.default === 'string') {
|
|
261
|
-
const method = enumMode === 'union-type' ? 'getEnumTypeName' : 'getEnumClassName';
|
|
262
|
-
const enumClassName = this.namingStrategy[method](prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
263
281
|
if (enumMode === 'union-type') {
|
|
264
282
|
return `${padding}${ret} = ${this.quote(prop.default)};\n`;
|
|
265
283
|
}
|
|
284
|
+
const enumClassName = prop.nativeEnumName
|
|
285
|
+
? this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema)
|
|
286
|
+
: this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
266
287
|
const enumVal = this.namingStrategy.enumValueToEnumProperty(prop.default, prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
267
288
|
return `${padding}${ret} = ${enumClassName}${exports.identifierRegex.test(enumVal) ? `.${enumVal}` : `[${this.quote(enumVal)}]`};\n`;
|
|
268
289
|
}
|
|
@@ -280,10 +301,22 @@ class SourceFile {
|
|
|
280
301
|
return `${padding}${ret} = ${prop.ref ? this.referenceCoreImport('ref') : this.referenceCoreImport('rel')}(${propType}, ${defaultVal});\n`;
|
|
281
302
|
}
|
|
282
303
|
getEnumClassDefinition(prop, padLeft) {
|
|
304
|
+
const enumMode = this.options.enumMode;
|
|
305
|
+
if (prop.nativeEnumName) {
|
|
306
|
+
const imports = [];
|
|
307
|
+
if (enumMode !== 'union-type') {
|
|
308
|
+
imports.push(prop.runtimeType);
|
|
309
|
+
}
|
|
310
|
+
if (!this.options.inferEntityType && enumMode !== 'ts-enum') {
|
|
311
|
+
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
312
|
+
imports.push(enumTypeName);
|
|
313
|
+
}
|
|
314
|
+
this.enumImports.set(prop.runtimeType, imports);
|
|
315
|
+
return '';
|
|
316
|
+
}
|
|
283
317
|
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
284
318
|
const enumTypeName = this.namingStrategy.getEnumTypeName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
285
319
|
const padding = ' '.repeat(padLeft);
|
|
286
|
-
const enumMode = this.options.enumMode;
|
|
287
320
|
const enumValues = prop.items;
|
|
288
321
|
if (enumMode === 'union-type') {
|
|
289
322
|
return `export type ${enumTypeName} = ${enumValues.map(item => this.quote(item)).join(' | ')};\n`;
|
|
@@ -579,6 +612,11 @@ class SourceFile {
|
|
|
579
612
|
if (this.options.enumMode === 'union-type') {
|
|
580
613
|
options.items = `[${prop.items.map(item => this.quote(item)).join(', ')}]`;
|
|
581
614
|
}
|
|
615
|
+
else if (prop.nativeEnumName) {
|
|
616
|
+
const enumClassName = this.namingStrategy.getEnumClassName(prop.nativeEnumName, undefined, this.meta.schema);
|
|
617
|
+
options.items = `() => ${enumClassName}`;
|
|
618
|
+
options.nativeEnumName = this.quote(prop.nativeEnumName);
|
|
619
|
+
}
|
|
582
620
|
else {
|
|
583
621
|
const enumClassName = this.namingStrategy.getEnumClassName(prop.fieldNames[0], this.meta.collection, this.meta.schema);
|
|
584
622
|
options.items = `() => ${enumClassName}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/entity-generator",
|
|
3
|
-
"version": "6.5.10-dev.
|
|
3
|
+
"version": "6.5.10-dev.20",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.mjs",
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"access": "public"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@mikro-orm/knex": "6.5.10-dev.
|
|
61
|
+
"@mikro-orm/knex": "6.5.10-dev.20",
|
|
62
62
|
"fs-extra": "11.3.2"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@mikro-orm/core": "^6.5.9"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
|
-
"@mikro-orm/core": "6.5.10-dev.
|
|
68
|
+
"@mikro-orm/core": "6.5.10-dev.20"
|
|
69
69
|
}
|
|
70
70
|
}
|