@mikro-orm/entity-generator 6.5.10-dev.14 → 6.5.10-dev.16
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.d.ts +5 -0
- package/DefineEntitySourceFile.js +116 -0
- package/EntityGenerator.js +5 -1
- package/EntitySchemaSourceFile.d.ts +3 -2
- package/EntitySchemaSourceFile.js +44 -36
- package/SourceFile.d.ts +1 -1
- package/SourceFile.js +3 -3
- package/package.json +3 -3
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefineEntitySourceFile = void 0;
|
|
4
|
+
const core_1 = require("@mikro-orm/core");
|
|
5
|
+
const EntitySchemaSourceFile_1 = require("./EntitySchemaSourceFile");
|
|
6
|
+
class DefineEntitySourceFile extends EntitySchemaSourceFile_1.EntitySchemaSourceFile {
|
|
7
|
+
generate() {
|
|
8
|
+
const enumDefinitions = [];
|
|
9
|
+
for (const prop of Object.values(this.meta.properties)) {
|
|
10
|
+
if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR)) {
|
|
11
|
+
enumDefinitions.push(this.getEnumClassDefinition(prop, 2));
|
|
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
|
+
}
|
|
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[core_1.Config] = true;
|
|
44
|
+
entitySchemaOptions.properties[core_1.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;
|
|
52
|
+
}
|
|
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 core_1.ReferenceKind.ONE_TO_ONE:
|
|
59
|
+
builder += `() => ${p}.oneToOne(${prop.type})`;
|
|
60
|
+
break;
|
|
61
|
+
case core_1.ReferenceKind.ONE_TO_MANY:
|
|
62
|
+
builder += `() => ${p}.oneToMany(${prop.type})`;
|
|
63
|
+
break;
|
|
64
|
+
case core_1.ReferenceKind.MANY_TO_ONE:
|
|
65
|
+
builder += `() => ${p}.manyToOne(${prop.type})`;
|
|
66
|
+
break;
|
|
67
|
+
case core_1.ReferenceKind.MANY_TO_MANY:
|
|
68
|
+
builder += `() => ${p}.manyToMany(${prop.type})`;
|
|
69
|
+
break;
|
|
70
|
+
case core_1.ReferenceKind.EMBEDDED:
|
|
71
|
+
builder += `() => ${p}.embedded(${prop.type})`;
|
|
72
|
+
break;
|
|
73
|
+
case core_1.ReferenceKind.SCALAR:
|
|
74
|
+
default: {
|
|
75
|
+
if (options.type && !(options.type in core_1.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', 'ref', 'nullable', 'array', 'object', 'mapToPk', 'hidden', 'concurrencyCheck', 'lazy', 'eager',
|
|
85
|
+
'orphanRemoval', 'version', 'unsigned', 'returning', 'createForeignKeyConstraint', 'fixedOrder', 'owner',
|
|
86
|
+
'getter', 'setter', 'unique', 'index', 'hydrate', 'persist', 'autoincrement',
|
|
87
|
+
]);
|
|
88
|
+
const skipOptions = new Set(['entity', 'kind', 'type', 'items']);
|
|
89
|
+
const spreadOptions = new Set([
|
|
90
|
+
'fieldNames', 'joinColumns', 'inverseJoinColumns', 'referencedColumnNames', 'ownColumns', 'columnTypes',
|
|
91
|
+
'cascade', 'ignoreSchemaChanges', 'customOrder', 'groups', 'where', 'orderBy',
|
|
92
|
+
]);
|
|
93
|
+
const rename = {
|
|
94
|
+
fieldName: 'name',
|
|
95
|
+
};
|
|
96
|
+
for (const key of Object.keys(options)) {
|
|
97
|
+
if (typeof options[key] === 'undefined' || skipOptions.has(key)) {
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const method = rename[key] ?? key;
|
|
101
|
+
const params = simpleOptions.has(key) && options[key] === true ? '' : options[key];
|
|
102
|
+
builder += `.${method}`;
|
|
103
|
+
if (key === 'enum') {
|
|
104
|
+
builder += `(${options.items})`;
|
|
105
|
+
}
|
|
106
|
+
else if (spreadOptions.has(key) && typeof params === 'string' && params.startsWith('[')) {
|
|
107
|
+
builder += `(${params.slice(1, -1)})`;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
builder += `(${params})`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return builder;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.DefineEntitySourceFile = DefineEntitySourceFile;
|
package/EntityGenerator.js
CHANGED
|
@@ -5,6 +5,7 @@ const core_1 = require("@mikro-orm/core");
|
|
|
5
5
|
const knex_1 = require("@mikro-orm/knex");
|
|
6
6
|
const node_path_1 = require("node:path");
|
|
7
7
|
const fs_extra_1 = require("fs-extra");
|
|
8
|
+
const DefineEntitySourceFile_1 = require("./DefineEntitySourceFile");
|
|
8
9
|
const EntitySchemaSourceFile_1 = require("./EntitySchemaSourceFile");
|
|
9
10
|
const SourceFile_1 = require("./SourceFile");
|
|
10
11
|
class EntityGenerator {
|
|
@@ -37,7 +38,10 @@ class EntityGenerator {
|
|
|
37
38
|
const baseDir = core_1.Utils.normalizePath(options.path ?? defaultPath);
|
|
38
39
|
for (const meta of metadata) {
|
|
39
40
|
if (!meta.pivotTable || options.outputPurePivotTables || this.referencedEntities.has(meta)) {
|
|
40
|
-
if (options.
|
|
41
|
+
if (options.defineEntity) {
|
|
42
|
+
this.sources.push(new DefineEntitySourceFile_1.DefineEntitySourceFile(meta, this.namingStrategy, this.platform, { ...options, scalarTypeInDecorator: true }));
|
|
43
|
+
}
|
|
44
|
+
else if (options.entitySchema) {
|
|
41
45
|
this.sources.push(new EntitySchemaSourceFile_1.EntitySchemaSourceFile(meta, this.namingStrategy, this.platform, { ...options, scalarTypeInDecorator: true }));
|
|
42
46
|
}
|
|
43
47
|
else {
|
|
@@ -2,7 +2,8 @@ import { type Dictionary, type EntityProperty } from '@mikro-orm/core';
|
|
|
2
2
|
import { SourceFile } from './SourceFile';
|
|
3
3
|
export declare class EntitySchemaSourceFile extends SourceFile {
|
|
4
4
|
generate(): string;
|
|
5
|
-
|
|
5
|
+
protected generateClassDefinition(): string;
|
|
6
|
+
protected getPropertyOptions(prop: EntityProperty, quote?: boolean): Dictionary;
|
|
6
7
|
protected getPropertyIndexesOptions(prop: EntityProperty, options: Dictionary): void;
|
|
7
|
-
protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty): void;
|
|
8
|
+
protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty, quote?: boolean): void;
|
|
8
9
|
}
|
|
@@ -5,43 +5,14 @@ const core_1 = require("@mikro-orm/core");
|
|
|
5
5
|
const SourceFile_1 = require("./SourceFile");
|
|
6
6
|
class EntitySchemaSourceFile extends SourceFile_1.SourceFile {
|
|
7
7
|
generate() {
|
|
8
|
-
|
|
9
|
-
if (this.meta.className === this.options.customBaseEntityName) {
|
|
10
|
-
const defineConfigTypeSettings = {};
|
|
11
|
-
defineConfigTypeSettings.forceObject = this.platform.getConfig().get('serialization').forceObject ?? false;
|
|
12
|
-
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('Config')}]?: ${this.referenceCoreImport('DefineConfig')}<${this.serializeObject(defineConfigTypeSettings)}>;\n`;
|
|
13
|
-
}
|
|
8
|
+
const classDefinition = this.generateClassDefinition();
|
|
14
9
|
const enumDefinitions = [];
|
|
15
|
-
const eagerProperties = [];
|
|
16
|
-
const primaryProps = [];
|
|
17
|
-
const props = [];
|
|
18
10
|
for (const prop of Object.values(this.meta.properties)) {
|
|
19
|
-
props.push(this.getPropertyDefinition(prop, 2));
|
|
20
11
|
if (prop.enum && (typeof prop.kind === 'undefined' || prop.kind === core_1.ReferenceKind.SCALAR)) {
|
|
21
12
|
enumDefinitions.push(this.getEnumClassDefinition(prop, 2));
|
|
22
13
|
}
|
|
23
|
-
if (prop.eager) {
|
|
24
|
-
eagerProperties.push(prop);
|
|
25
|
-
}
|
|
26
|
-
if (prop.primary && (!['id', '_id', 'uuid'].includes(prop.name) || this.meta.compositePK)) {
|
|
27
|
-
primaryProps.push(prop);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
if (primaryProps.length > 0) {
|
|
31
|
-
const primaryPropNames = primaryProps.map(prop => `'${prop.name}'`);
|
|
32
|
-
if (primaryProps.length > 1) {
|
|
33
|
-
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: [${primaryPropNames.join(', ')}];\n`;
|
|
34
|
-
}
|
|
35
|
-
else {
|
|
36
|
-
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: ${primaryPropNames[0]};\n`;
|
|
37
|
-
}
|
|
38
14
|
}
|
|
39
|
-
|
|
40
|
-
const eagerPropertyNames = eagerProperties.map(prop => `'${prop.name}'`).sort();
|
|
41
|
-
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('EagerProps')}]?: ${eagerPropertyNames.join(' | ')};\n`;
|
|
42
|
-
}
|
|
43
|
-
classBody += `${props.join('')}`;
|
|
44
|
-
let ret = this.getEntityClass(classBody);
|
|
15
|
+
let ret = classDefinition;
|
|
45
16
|
if (enumDefinitions.length) {
|
|
46
17
|
ret += '\n' + enumDefinitions.join('\n');
|
|
47
18
|
}
|
|
@@ -67,7 +38,44 @@ class EntitySchemaSourceFile extends SourceFile_1.SourceFile {
|
|
|
67
38
|
ret = `${this.generateImports()}\n\n${ret}`;
|
|
68
39
|
return ret;
|
|
69
40
|
}
|
|
70
|
-
|
|
41
|
+
generateClassDefinition() {
|
|
42
|
+
let classBody = '';
|
|
43
|
+
if (!this.options.customBaseEntityName || this.meta.className === this.options.customBaseEntityName) {
|
|
44
|
+
const defineConfigTypeSettings = {};
|
|
45
|
+
defineConfigTypeSettings.forceObject = this.platform.getConfig().get('serialization').forceObject ?? false;
|
|
46
|
+
if (defineConfigTypeSettings.forceObject) {
|
|
47
|
+
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('Config')}]?: ${this.referenceCoreImport('DefineConfig')}<${this.serializeObject(defineConfigTypeSettings)}>;\n`;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const eagerProperties = [];
|
|
51
|
+
const primaryProps = [];
|
|
52
|
+
const props = [];
|
|
53
|
+
for (const prop of Object.values(this.meta.properties)) {
|
|
54
|
+
props.push(this.getPropertyDefinition(prop, 2));
|
|
55
|
+
if (prop.eager) {
|
|
56
|
+
eagerProperties.push(prop);
|
|
57
|
+
}
|
|
58
|
+
if (prop.primary && (!['id', '_id', 'uuid'].includes(prop.name) || this.meta.compositePK)) {
|
|
59
|
+
primaryProps.push(prop);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (primaryProps.length > 0) {
|
|
63
|
+
const primaryPropNames = primaryProps.map(prop => `'${prop.name}'`);
|
|
64
|
+
if (primaryProps.length > 1) {
|
|
65
|
+
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: [${primaryPropNames.join(', ')}];\n`;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('PrimaryKeyProp')}]?: ${primaryPropNames[0]};\n`;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (eagerProperties.length > 0) {
|
|
72
|
+
const eagerPropertyNames = eagerProperties.map(prop => `'${prop.name}'`).sort();
|
|
73
|
+
classBody += `${' '.repeat(2)}[${this.referenceCoreImport('EagerProps')}]?: ${eagerPropertyNames.join(' | ')};\n`;
|
|
74
|
+
}
|
|
75
|
+
classBody += `${props.join('')}`;
|
|
76
|
+
return this.getEntityClass(classBody);
|
|
77
|
+
}
|
|
78
|
+
getPropertyOptions(prop, quote = true) {
|
|
71
79
|
const options = {};
|
|
72
80
|
if (prop.primary) {
|
|
73
81
|
options.primary = true;
|
|
@@ -82,7 +90,7 @@ class EntitySchemaSourceFile extends SourceFile_1.SourceFile {
|
|
|
82
90
|
this.getOneToManyDecoratorOptions(options, prop);
|
|
83
91
|
}
|
|
84
92
|
else if (prop.kind === core_1.ReferenceKind.SCALAR || typeof prop.kind === 'undefined') {
|
|
85
|
-
this.getScalarPropertyDecoratorOptions(options, prop);
|
|
93
|
+
this.getScalarPropertyDecoratorOptions(options, prop, quote);
|
|
86
94
|
}
|
|
87
95
|
else if (prop.kind === core_1.ReferenceKind.EMBEDDED) {
|
|
88
96
|
this.getEmbeddedPropertyDeclarationOptions(options, prop);
|
|
@@ -126,15 +134,15 @@ class EntitySchemaSourceFile extends SourceFile_1.SourceFile {
|
|
|
126
134
|
processIndex('index');
|
|
127
135
|
processIndex('unique');
|
|
128
136
|
}
|
|
129
|
-
getScalarPropertyDecoratorOptions(options, prop) {
|
|
137
|
+
getScalarPropertyDecoratorOptions(options, prop, quote = true) {
|
|
130
138
|
if (prop.enum) {
|
|
131
139
|
options.enum = true;
|
|
132
140
|
options.items = `() => ${prop.runtimeType}`;
|
|
133
141
|
}
|
|
134
142
|
else {
|
|
135
|
-
options.type = this.quote(prop.type);
|
|
143
|
+
options.type = quote ? this.quote(prop.type) : prop.type;
|
|
136
144
|
}
|
|
137
|
-
super.getScalarPropertyDecoratorOptions(options, prop);
|
|
145
|
+
super.getScalarPropertyDecoratorOptions(options, prop, quote);
|
|
138
146
|
}
|
|
139
147
|
}
|
|
140
148
|
exports.EntitySchemaSourceFile = EntitySchemaSourceFile;
|
package/SourceFile.d.ts
CHANGED
|
@@ -30,7 +30,7 @@ export declare class SourceFile {
|
|
|
30
30
|
protected getCommonDecoratorOptions(options: Dictionary, prop: EntityProperty): void;
|
|
31
31
|
private propTypeBreakdowns;
|
|
32
32
|
private breakdownOfIType;
|
|
33
|
-
protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty): void;
|
|
33
|
+
protected getScalarPropertyDecoratorOptions(options: Dictionary, prop: EntityProperty, quote?: boolean): void;
|
|
34
34
|
protected getManyToManyDecoratorOptions(options: Dictionary, prop: EntityProperty): void;
|
|
35
35
|
protected getOneToManyDecoratorOptions(options: Dictionary, prop: EntityProperty): void;
|
|
36
36
|
protected getEmbeddedPropertyDeclarationOptions(options: Dictionary, prop: EntityProperty): void;
|
package/SourceFile.js
CHANGED
|
@@ -540,7 +540,7 @@ class SourceFile {
|
|
|
540
540
|
this.propTypeBreakdowns.set(prop, r);
|
|
541
541
|
return r;
|
|
542
542
|
}
|
|
543
|
-
getScalarPropertyDecoratorOptions(options, prop) {
|
|
543
|
+
getScalarPropertyDecoratorOptions(options, prop, quote = true) {
|
|
544
544
|
if (prop.fieldNames[0] !== this.namingStrategy.propertyToColumnName(prop.name)) {
|
|
545
545
|
options.fieldName = this.quote(prop.fieldNames[0]);
|
|
546
546
|
}
|
|
@@ -574,7 +574,7 @@ class SourceFile {
|
|
|
574
574
|
return ((useDefault && !hasUsableNullDefault) || (prop.optional && !prop.nullable));
|
|
575
575
|
})() // also when there is the "| Opt" type modifier (because reflect-metadata can't extract the base)
|
|
576
576
|
) {
|
|
577
|
-
options.type = this.quote(prop.type);
|
|
577
|
+
options.type = quote ? this.quote(prop.type) : prop.type;
|
|
578
578
|
}
|
|
579
579
|
}
|
|
580
580
|
const columnTypeFromMappedRuntimeType = mappedRuntimeType.getColumnType({ ...prop, autoincrement: false }, this.platform);
|
|
@@ -675,7 +675,7 @@ class SourceFile {
|
|
|
675
675
|
if (prop.array) {
|
|
676
676
|
options.array = true;
|
|
677
677
|
}
|
|
678
|
-
if (prop.object) {
|
|
678
|
+
if (prop.object && !prop.array) {
|
|
679
679
|
options.object = true;
|
|
680
680
|
}
|
|
681
681
|
if (prop.prefix === false || typeof prop.prefix === 'string') {
|
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.16",
|
|
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.16",
|
|
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.16"
|
|
69
69
|
}
|
|
70
70
|
}
|