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: ensure implicit config options load automatically #191

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"@nestjs/core": "^10.0.0"
},
"devDependencies": {
"@mikro-orm/core": "^6.2.7",
"@mikro-orm/sqlite": "^6.2.7",
"@mikro-orm/core": "^6.3.14-dev.67",
"@mikro-orm/sqlite": "^6.3.14-dev.67",
"@nestjs/common": "^10.3.8",
"@nestjs/core": "^10.3.8",
"@nestjs/platform-express": "^10.3.8",
Expand Down
8 changes: 6 additions & 2 deletions src/mikro-orm-core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,14 @@ export class MikroOrmCoreModule implements OnApplicationShutdown {
}

try {
let config;
let config: Configuration | undefined;

if (!options || Object.keys(options).length === 0) {
config = await ConfigurationLoader.getConfiguration(false);
const configPathFromArg = ConfigurationLoader.configPathsFromArg();
config = await ConfigurationLoader.getConfiguration(process.env.MIKRO_ORM_CONTEXT_NAME, configPathFromArg ?? ConfigurationLoader.getConfigPaths());
if (configPathFromArg) {
config.getLogger().warn('deprecated', 'Path for config file was inferred from the command line arguments. Instead, you should set the MIKRO_ORM_CLI_CONFIG environment variable to specify the path, or if you really must use the command line arguments, import the config manually based on them, and pass it to init.', { label: 'D0001' });
}
}

if (!config && 'useFactory' in options!) {
Expand Down
7 changes: 6 additions & 1 deletion src/mikro-orm.providers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ export function createMikroOrmProvider(
}

if (!options || Object.keys(options).length === 0) {
const config = await ConfigurationLoader.getConfiguration();
ConfigurationLoader.commonJSCompat({});
const configPathFromArg = ConfigurationLoader.configPathsFromArg();
const config = await ConfigurationLoader.getConfiguration(process.env.MIKRO_ORM_CONTEXT_NAME, configPathFromArg ?? ConfigurationLoader.getConfigPaths());
config.set('logger', logger.log.bind(logger));
options = config as unknown as MikroOrmModuleOptions;
if (configPathFromArg) {
config.getLogger().warn('deprecated', 'Path for config file was inferred from the command line arguments. Instead, you should set the MIKRO_ORM_CLI_CONFIG environment variable to specify the path, or if you really must use the command line arguments, import the config manually based on them, and pass it to init.', { label: 'D0001' });
}
}

return MikroORM.init(options);
Expand Down
21 changes: 20 additions & 1 deletion tests/mikro-orm.module.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository, Options, EntityManager, MikroORM } from '@mikro-orm/core';
import { EntityRepository, Options, EntityManager, MikroORM, Utils } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';
import { Inject, Logger, Module, Scope } from '@nestjs/common';
import { ContextIdFactory } from '@nestjs/core';
Expand Down Expand Up @@ -82,6 +82,25 @@ describe('MikroORM Module', () => {
});

describe('Single Database', () => {
it('forRoot without options', async () => {
const spy = jest.spyOn(Utils, 'dynamicImport').mockImplementation(id => import(id));
const tmp = process.env.MIKRO_ORM_CLI_CONFIG;
process.env.MIKRO_ORM_CLI_CONFIG = `${__dirname}/test.config.ts`;

const module = await Test.createTestingModule({
imports: [MikroOrmModule.forRoot()],
}).compile();

const orm = module.get<MikroORM>(MikroORM);
expect(orm).toBeDefined();
expect(orm.config.get('contextName')).toBe('default');
expect(module.get<EntityManager>(EntityManager)).toBeDefined();
await orm.close();

process.env.MIKRO_ORM_CLI_CONFIG = tmp;
spy.mockRestore();
});

it('forRoot', async () => {
const module = await Test.createTestingModule({
imports: [MikroOrmModule.forRoot(testOptions)],
Expand Down
10 changes: 10 additions & 0 deletions tests/test.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';

export default defineConfig({
dbName: 'memory:',
driver: SqliteDriver,
baseDir: __dirname,
entities: ['entities'],
connect: false,
});
54 changes: 32 additions & 22 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.