teamMay / factory

🏭 Testing made easier with factories for your entities/models.
ISC License
7 stars 1 forks source link

[Question] how would you see setting attributes that are related to each other? #4

Closed kevin-mitchell closed 2 years ago

kevin-mitchell commented 2 years ago

Apologies if this is a dense question, but in this example I want the name and code to be related - e.g. the code should be the lowercase value of name - but name is generated using faker (or chancejs, etc).

export class ChannelFactory extends Factory<Channel> {

  entity = Channel;

  attrs = {
    name: () => `Web-${faker.datatype.number({ min: 1000, max: 9999 })}`,
    code: // should be the name .toLowerCase(),
    ...
  };
}

Do you have a suggested pattern in terms of how to handle this situation?

Thanks for the work so far on this tool, seems useful!

adrien-may commented 2 years ago

Hi Kevin, I'm really happy this lib is useful to someone else than me. I have some plans to extend it to work outside of typeorm but I'm giving myself some time to think about it

You can achieve what you want with what I called LazyAttribute. You can apply a function which get as an argument the instance to be created. In your case, it means you could access your name property ! There is an example in the readme about LazyAttribute

adrien-may commented 2 years ago

Unless there is a bug with the implementation, I'll close this issue

kevin-mitchell commented 2 years ago

Thanks so much for taking the time to make the tool.

I spent some time looking at some of the other options and like your approach. Clearly I should have spent some more time reading your documentation instead :).

I will take a look at the LazyAttribute, it sounds like what I want!

One other unrelated note (I can make an issue if you want for tracking?) - I think it might be useful to add more information about HOW the persistence works with TypeORM. You have

The adapter allows you to persist your data. If you want to save your data in a database via typeorm, you can use the TypeormAdapter. Default Adapter is ObjectAdapter and does not persist anything. You can create your own adapter to persist your data the way you want.

But it isn't clear to me from this documentation alone how this actually works, or what the intended work flow would be here. I will likely use this for seeding some basic data in lower dev / sandbox environments, or potentially for end-to-end testing, and I can always try to open a PR with more details if I get to it.

For now, thanks again, I'll close out this issue!

adrien-may commented 2 years ago

If you need more information, you can have a look at typeorm This project does not aim to document typeorm features.

But you can also use this lib without using typeorm !

It is quite common when using factories, seeding etc... to have data being persisted in a database, a file or whatever. Factory uses a method called save to persist data. If no adapter is provided, this method does... almost nothing. Adapters, like the typeorm one, allow you to use typeorm features and save the data in your database. What is not written in my documentation is how typeorm should be setup, how you open a connection to your database etc... This should be covered on their documentation though.

If you need any kind of custom logic in your save method, you could write your own adapter to suit your needs.