Open moltar opened 4 years ago
@moltar Good catch - I agree this is a design flaw and should be fixed.
There are a couple of solutions that immediately come to mind.
Then when creating your entities the syntax could look something like...
const books = await bookFactory.saveMany(5, {...options}, 'admin');
Then the factory performs different logic based on the parameter.
I am heavily leaning towards (2) as it is more flexible in the longterm. I dislike how Laravel does the options parameters as it encourages 'god factories' which do too much. Option (2) encourages the creation of smaller more specific factories.
I am definitely in favor of (2).
I'm trying to use this and here's some feedback.
I think there is a design flaw where there is a tight coupling of
Entity
andEntityFactory
.Here's a use case:
I need to create two types of users:
customer
andadmin
.I defined two factories:
CustomerFactory
andAdminFactory
, both usingUser
entity (just different values).But when I call
init
, the name for internal storage is obtained from the Entity class:https://github.com/adamdubicki/typeorm-entity-factory/blob/a739b3bdec974df33d409064675c553a9e175eb8/src/factory-container.ts#L30
Meaning that if I load two of my above entities, one will override the other, as both of the entities are
User
.And I wouldn't be able to create two types of users.
There is a workaround, to create two separate containers, but it doesn't seem to be very ergonomic.