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

Cant use mikro-orm's v6.4 feature of having multiple configs within mikro-orm.config.ts #202

Open
yawhide opened this issue Jan 24, 2025 · 5 comments

Comments

@yawhide
Copy link

yawhide commented Jan 24, 2025

Describe the bug
Mikro-orm v6.4 has a new feature where you can define multiple configs within mikro-orm.config.ts and then reference each one using contextName.
This enables great synergy with their cli tool so that you can reference which context you want to use in a single config file (something like this: npx mikro-orm --contextName=super).

When I try to set this up following their example, i get an error:
error: No driver specified, please fill in the `driver` option or use `defineConfig` helper (to define your ORM config) or `MikroORM` class (to call the `init` method) exported from the driver package (e.g. `import { defineConfig } from '@mikro-orm/mysql'; export defineConfig({ ... })`).

This problem is forcing me to have a multi config setup (aka something like mikro-orm.super.config.ts and mikro-orm.mini.config.ts) and makes setting up the cli less dev friendly.

To Reproduce
Steps to reproduce the behavior:

in mikro-orm.config.ts

import { defineConfig } from '@mikro-orm/postgresql';

export default [
  defineConfig({
    contextName: 'mini',
    // other settings
  }),
  defineConfig({
    contextName: 'super',
    // other settings
  }),

In app.module.ts

import mikroOrmConfigfrom "./mikro-orm.config";

MikroOrmModule.forRoot(mikroOrmConfig)

In some.service.ts

import { OnModuleInit } from "@nestjs/common";

@Injectable()
export class SomeService implements OnModuleInit {

constructor(
@InjectEntityManager("super") private readonly em: EntityManager
)

...

When I try to run my app, I get the error I posted above.

Expected behavior
Be able to have all my configs in a single file.

Dependency Version
node v20
typescript 5.5
mikro-orm/core 6.4
mikro-orm/nestjs 6.0.2
your-driver postgresql driver

let me know if you cant reproduce this issue and it is an issue on my end. thanks!

@B4nan
Copy link
Member

B4nan commented Jan 24, 2025

I don't think it can work the way you do it. forRoot needs to have a single confix, and you are feeding it an array. To have multiple ORM instances, you would need multiple forRoot calls.

But I guess we could expand the calls automatically if we see it's an array. Thoughts @boenrobot?

@yawhide
Copy link
Author

yawhide commented Jan 24, 2025

I don't think it can work the way you do it. forRoot needs to have a single confix, and you are feeding it an array. To have multiple ORM instances, you would need multiple forRoot calls.

But I guess we could expand the calls automatically if we see it's an array. Thoughts @boenrobot?

that explains it. what is nice about mikro-orm supporting multiple configs in a single config file is that it plays well with the rest of their ecosystem. even in nestjs, in my app.module.ts, i could do:

MikroOrmModule.forRoot()

and 'nestjs just magically works' with multiple configs. then i select which context in my services.

@B4nan
Copy link
Member

B4nan commented Jan 24, 2025

Hmm, I guess we would still need a bit different approach, since forRoot needs to return a nest module, not multiple, just one.

@yawhide
Copy link
Author

yawhide commented Jan 24, 2025

Hmm, I guess we would still need a bit different approach, since forRoot needs to return a nest module, not multiple, just one.

ah i see.

@B4nan
Copy link
Member

B4nan commented Jan 24, 2025

I guess we can have two signatures and users would just need to restructure the return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants