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

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

Closed
kevin-mitchell opened this issue Feb 3, 2022 · 4 comments
Labels
question Further information is requested

Comments

@kevin-mitchell
Copy link

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
Copy link
Contributor

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 adrien-may added the question Further information is requested label Feb 3, 2022
@adrien-may
Copy link
Contributor

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

@kevin-mitchell
Copy link
Author

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
Copy link
Contributor

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.

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

No branches or pull requests

2 participants