-
Notifications
You must be signed in to change notification settings - Fork 133
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
Deeply saving factory #209
Comments
This is related on how the related entities are being processed: if (isPromiseLike(entity[attribute])) {
entity[attribute] = await entity[attribute]
} if (isSeeding) {
entity[attribute] = await (subEntityFactory as any).create()
} else {
entity[attribute] = await (subEntityFactory as any).make()
} In your definition, The only way this could work is changing that Also, please try to avoid using import faker from '@faker-js/faker';
define(Answer, () => {
const answer = new Answer();
...
answer.answer = faker.lorem.sentence();
...
}); import Faker from 'faker';
define(Answer, (faker: typeof Faker) => {
const answer = new Answer();
...
answer.answer = faker.lorem.sentence();
...
}); |
@jorgebodega Thanks for taking your time to answer. I am wondering if there is any way to force the persistence of the Entity and its relations at the moment of running the seed and not before. I am trying to avoid forcing my factories to introduce side effects in the database until its strictly necessary (this is, replacing the make() calls with create()). Also I dont want to modify the Entity schema to have Is that possible? |
I'm not understanding your problem, could you try to explain with code? |
@jorgebodega Let's assume that I don't change the factories. I want them to NOT store values in the database. Just build the entities and its associations, nothing else. However, in the seed, I would like to call my factory and tell it "save the entity and all its related records".
I would like to avoid adding |
I come from a Rails background, heavily using this factory gem called FactoryBot so maybe this is not possible in TypeORM nor with this lib. |
Hi, sorry about the delay, let me check this in the next few days and if is already fixed on the forked package |
@jorgebodega no worries. Just to clarify, in my codebase I temporarily replaced
with
But I think it would be ideal that rather than doing that, I can just do
And that somehow "hacks" TypeORM to store the associations first rather than being forced to add As mentioned, I am taking inspiration on https://github.com/thoughtbot/factory_bot that uses Rails' ActiveRecord and maybe this is something that cannot be done with TypeORM or requires a lot of work. I am a newbie in the JS backend world but maybe if you could guide me a little bit on what this would require I can try to make a proof of concept. |
I have the following factories:
question.factory.ts
answer.factory.ts
user.factory.ts
And also I have a seed:
When I run this, only the Questions records are stored, the Answers and User are ignored.
How can I persist them?
The text was updated successfully, but these errors were encountered: