Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(entity-generator): use table name instead of class name in EntitySchema #3916

Merged
merged 6 commits into from
Jan 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/entity-generator/src/EntitySchemaSourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class EntitySchemaSourceFile extends SourceFile {
ret += ` class: ${this.meta.className},\n`;

if (this.meta.collection !== this.namingStrategy.classToTableName(this.meta.className)) {
ret += ` tableName: ${this.quote(this.meta.className)},\n`;
ret += ` tableName: ${this.quote(this.meta.tableName)},\n`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

meta.tableName is an alias for meta.collection, i'd like to use the same in the condition on line 44 as well, to reduce confusion. i dont mind using meta.tableName on both places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks. applied the same check at L44 now.

}

/* istanbul ignore next */
Expand Down
17 changes: 17 additions & 0 deletions tests/features/entity-generator/EntityGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ describe('EntityGenerator', () => {
await orm.close(true);
});

test('table name with underscore using entitySchema [mysql]', async () => {
const orm = await initORMMySql('mysql', {entityGenerator: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this passing the linter? :] i dont think so, the indents on following lines are also wrong

Suggested change
const orm = await initORMMySql('mysql', {entityGenerator: {
const orm = await initORMMySql('mysql', { entityGenerator: {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true that. i didn't run lint from my local

entitySchema: true,
identifiedReferences: true,
}}, true);
await orm.schema.dropSchema();
await orm.schema.execute(`
create table if not exists \`123_table_name\` (\`id\` int(10) unsigned not null auto_increment primary key) default character set utf8mb4 engine = InnoDB;
`);
const dump = await orm.entityGenerator.generate({ save: false, baseDir: './temp/entities' });
expect(dump).toMatchSnapshot('mysql-entity-dump-underscore-entity-schema');
await orm.schema.execute(`
drop table if exists \`123_table_name\`;
`);
await orm.close(true);
});

test('numeric nullable columns with null default [mysql]', async () => {
const orm = await initORMMySql('mysql', {}, true);
await orm.schema.dropSchema();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3553,3 +3553,24 @@ export class E123TableName {
",
]
`;

exports[`EntityGenerator table name with underscore using entitySchema [mysql]: mysql-entity-dump-underscore-entity-schema 1`] = `
[
"import { Entity, PrimaryKey } from '@mikro-orm/core';

export class E123TableName {

id!: number;

}

export const E123TableNameSchema = new EntitySchema({
class: E123TableName,
tableName: '123_table_name',
properties: {
id: { primary: true, type: 'number' },
},
});
",
]
`;