-
-
Notifications
You must be signed in to change notification settings - Fork 611
Description
Is your feature request related to a problem? Please describe.
I'm building a new Custom Type but it is frustrating that it can only be instantiated from within the entity decorator. The Type I'm building has a dependency on a rather complex object. Put it simple, it wouldn't be feasable to construct such dependency on the decorator.
For example, given a Type like this:
import { Type } from '@mikro-orm/core';
export class MyCustomType extends Type<MyObject, string> {
public constructor(manager: Manager) {
// ...
}
// ...
}It could happen that Manager has certain complexity to construct so, the following would simply be impractical:
import { Entity, Property } from '@mikro-orm/core';
import { v4 } from 'uuid';
@Entity()
export class MyEntity {
@Property({ type: new MyCustomType(/* complex construction code */) })
myObject: MyObject;
}Describe the solution you'd like
I think adding a new key to the MikroORM.init() function to add custom types shouldn't be that difficult. Admitedly, I haven't taken a look at the code yet but seeing that the decorator also takes an string suggests to me that there might be some sort of resolution of types later on so, my proposal would be something like this:
const manager = /* complex construction code */;
const myCustomType = new MyCustomType(manager);
MikroORM.init({
customTypes: {
my_custom_type: myCustomType
},
// ...
});With this code and given the beforementioned ability to resolve types on run time, the entity would then be like:
import { Entity, Property } from '@mikro-orm/core';
import { v4 } from 'uuid';
@Entity()
export class MyEntity {
@Property({ type: 'my_custom_type' })
myObject: MyObject;
}Describe alternatives you've considered
The simplest solution to this problem would be to abstract the complex construction into a module and import it on any required file. However, this is not always posible as, in my case, I'm constructing the manager with a container.
Additional context
In addition to this problem, sometimes some data needs to be asynchronously retreived (for example, an object loaded from a different DB, API, etc). I've noticed that there isn't a single mention of Types returning promises. Is it possible? If not, I can raise another issue for this matter plus I don't think it would be that difficult to achieve since the core should run in an asynchronous context anyway.