Skip to content

Naming errors in Entity Generator #5400

Open
@jefmenegazzo

Description

@jefmenegazzo

Describe the bug

There are two minor bugs about naming in the Entity Generator.

1. Class names that not follows Pascal Case

In entities with more than one underscore between words, pascal case is not applied correctly.

@Entity({ tableName: "wac_tela_principal_wac___ident" })
export class WacTelaPrincipalWac_dent { }

@Entity({ tableName: "sft_tqm___ocorrencia_envolvido" })
export class SftTqm_correnciaEnvolvido { }

@Entity({ tableName: "sft_tela_principal_sft___ident" })
export class SftTelaPrincipalSft_dent { }

As a temporary workaround, I'm using lodash as follows:

onInitialMetadata: (metadata: EntityMetadata<any>[], platform) => {

	for (const entity of metadata) {
		entity.className = lodash.upperFirst(lodash.camelCase(entity.tableName));
	}
},

2. When using scalarPropertiesForRelations: 'always', some relations are not formatted correctly

@ManyToOne({ entity: () => FrUsuario, ref: true, fieldName: "usr_codigo_app" })
public.frUsuario!: Ref<FrUsuario>;

@ManyToOne({ entity: () => SftSubProduto, ref: true, fieldName: "subpro_id_subtituto", nullable: true })
public.sftSubProduto?: Ref<SftSubProduto>;

@OneToOne({ entity: () => SftContatoWhatsOptIn, ref: true, mappedBy: "public.sftContato" })
public.sftContatoInverse?: Ref<SftContatoWhatsOptIn>;

It appears to only happen with ManyToOne and OneToOne relationships. In this case, 'public' is the schema name.

Reproduction

async function getDatabaseMetadata(connection: DatabaseConnection) {

	const orm = await MikroORM.init({
		discovery: {
			warnWhenNoEntities: false,
		},
		extensions: [EntityGenerator],
		driver: PostgreSqlDriver,
		schema: connection.schema,
		dbName: connection.dbName,
		host: connection.host,
		port: connection.port,
		user: connection.user,
		password: connection.password,
		logger: logger.log,
		// debug: true,
	});

	let initialMetadata: EntityMetadata<any>[] = [];
	let processedMetadata: EntityMetadata<any>[] = [];

	const entitiesSource = await orm.entityGenerator.generate({
		bidirectionalRelations: true,
		identifiedReferences: true,
		scalarTypeInDecorator: true,
		scalarPropertiesForRelations: 'always',
		onlyPurePivotTables: false,
		fileName: (className: string) => lodash.kebabCase(className),
		onInitialMetadata: (metadata: EntityMetadata<any>[], platform) => {

			// WORKAROUND
			for (const entity of metadata) {
				entity.className = lodash.upperFirst(lodash.camelCase(entity.tableName));
			}

			initialMetadata = metadata;
		},
		onProcessedMetadata: (metadata, platform) => { processedMetadata = metadata; },
	});

	await orm.close(true);

	return { entitiesSource, initialMetadata, processedMetadata };
}

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.1.12

Node.js version

v20.11.1, typescript 5.3.3

Operating system

Windows 11

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions