Skip to content

Commit

Permalink
fix(mongo): validate usage of migrator and entity generator
Browse files Browse the repository at this point in the history
Closes #1801
  • Loading branch information
B4nan committed May 13, 2021
1 parent 30392bc commit 1db1a63
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
8 changes: 2 additions & 6 deletions packages/core/src/MikroORM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,14 @@ export class MikroORM<D extends IDatabaseDriver = IDatabaseDriver> {
* Gets the EntityGenerator.
*/
getEntityGenerator<T extends IEntityGenerator = IEntityGenerator>(): T {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { EntityGenerator } = require('@mikro-orm/entity-generator');
return new EntityGenerator(this.em);
return this.driver.getPlatform().getEntityGenerator(this.em) as T;
}

/**
* Gets the Migrator.
*/
getMigrator<T extends IMigrator = IMigrator>(): T {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Migrator } = require('@mikro-orm/migrations');
return new Migrator(this.em);
return this.driver.getPlatform().getMigrator(this.em) as T;
}

}
12 changes: 10 additions & 2 deletions packages/core/src/platforms/Platform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EntityRepository } from '../entity';
import { NamingStrategy, UnderscoreNamingStrategy } from '../naming-strategy';
import { Constructor, EntityProperty, IPrimaryKey, ISchemaGenerator, Primary } from '../typings';
import { Constructor, EntityProperty, IEntityGenerator, IMigrator, IPrimaryKey, ISchemaGenerator, Primary } from '../typings';
import { ExceptionConverter } from './ExceptionConverter';
import { EntityManager } from '../EntityManager';
import { Configuration } from '../utils/Configuration';
Expand Down Expand Up @@ -292,7 +292,15 @@ export abstract class Platform {
}

getSchemaGenerator(em: EntityManager): ISchemaGenerator {
throw new Error(`${this.constructor.name} does not use a schema generator`);
throw new Error(`${this.constructor.name} does not support SchemaGenerator`);
}

getEntityGenerator(em: EntityManager): IEntityGenerator {
throw new Error(`${this.constructor.name} does not support EntityGenerator`);
}

getMigrator(em: EntityManager): IMigrator {
throw new Error(`${this.constructor.name} does not support Migrator`);
}

processDateProperty(value: unknown): string | number | Date {
Expand Down
12 changes: 12 additions & 0 deletions packages/knex/src/AbstractSqlPlatform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export abstract class AbstractSqlPlatform extends Platform {
return new SchemaGenerator(em as any); // cast as `any` to get around circular dependencies
}

getEntityGenerator(em: EntityManager) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { EntityGenerator } = require('@mikro-orm/entity-generator');
return new EntityGenerator(em);
}

getMigrator(em: EntityManager) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { Migrator } = require('@mikro-orm/migrations');
return new Migrator(em);
}

quoteValue(value: any): string {
/* istanbul ignore if */
if (Utils.isPlainObject(value)) {
Expand Down
6 changes: 6 additions & 0 deletions tests/features/entity-generator/EntityGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { pathExists, remove } from 'fs-extra';
import { MikroORM } from '@mikro-orm/core';
import { DatabaseTable } from '@mikro-orm/knex';
import { EntityGenerator } from '@mikro-orm/entity-generator';
import { initORMMySql, initORMPostgreSql, initORMSqlite } from '../../bootstrap';
Expand Down Expand Up @@ -68,4 +69,9 @@ describe('EntityGenerator', () => {
await orm.close(true);
});

test('not supported [mongodb]', async () => {
const orm = await MikroORM.init({ type: 'mongo', dbName: 'mikro-orm-test', discovery: { warnWhenNoEntities: false } }, false);
expect(() => orm.getEntityGenerator()).toThrowError('MongoPlatform does not support EntityGenerator');
});

});
10 changes: 10 additions & 0 deletions tests/features/migrations/Migrator.mongo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { MikroORM } from '@mikro-orm/core';

describe('Migrator', () => {

test('not supported [mongodb]', async () => {
const orm = await MikroORM.init({ type: 'mongo', dbName: 'mikro-orm-test', discovery: { warnWhenNoEntities: false } }, false);
expect(() => orm.getMigrator()).toThrowError('MongoPlatform does not support Migrator');
});

});
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MikroORM } from '@mikro-orm/core';
import { SchemaGenerator } from '@mikro-orm/knex';

describe('SchemaGenerator', () => {

test('not supported [mongodb]', async () => {
const orm = await MikroORM.init({ type: 'mongo', dbName: 'mikro-orm-test', discovery: { warnWhenNoEntities: false } }, false);
expect(() => orm.getSchemaGenerator()).toThrowError('MongoPlatform does not use a schema generator');
expect(() => orm.getSchemaGenerator()).toThrowError('MongoPlatform does not support SchemaGenerator');
});

});

0 comments on commit 1db1a63

Please sign in to comment.