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
Providing a value for dependent objects #244
Comments
I don't understand the role of Would it help if traits could define their arguments as well as names ? const department = DepartmentFactory.use(['withTeams', { organizationId: organization.organizationId }]).create(); or , Maybe you're describing feature like https://github.com/thoughtbot/factory_bot/blob/master/GETTING_STARTED.md#transient-attributes ? |
I think that "if traits could define the arguments as well as names" would get the job done, yes! You might want to consider a syntax like Here is an example copy/paste from my work for more background. All of the data here came from Factories but we have to associate records back to their parents in a higher level function. export const buyableDistroOrderFactory = async (orderId: string) => {
const distro = await getDistroFactory().create();
const distroOrder = await DistroOrderFactory.create({
distro: {
connect: {
id: distro.id,
},
},
order: {
connect: {
id: orderId,
},
},
});
const orderItems = await Promise.all(
[0, 1, 2].map(() =>
getOrderItemFactory()
// the getter function returns `OrderItemFactoryInterface` and it does not know about the `wearable` trait
.use('wearable' as never)
.create({
distroOrder: {
connect: {
id: distroOrder.id,
},
},
order: {
connect: {
id: orderId,
},
},
}),
),
);
// the `merchItem` is associated with the order and we need to connect it to the distro
await getPrismaClient().merchItem.updateMany({
where: {
variants: {
some: {
options: {
some: {
orderItems: {
some: {
id: {
in: orderItems.map((oi) => oi.id),
},
},
},
},
},
},
},
},
data: {
// We need to set the distro ID here.
distroId: distro.id,
},
});
return distroOrder;
}; I need to associate the
And then the next factory receives those values. Think of it like a context provider/consumer in React if you use that. Global values that a factory function can use. IMO a benefit of doing it at the factory level instead of trait is it might cut down on some duplication. But on the other hand, letting traits define their own arguments would let someone say "This trait MUST have this extra data in order to be created", so that might be better. Thank you again for the time and work. I am happy to contribute to this library if possible! |
@subvertallchris
for now, first I'll consider a feature that allows user to add arbitrary parameters at the factory level. Do you use method chaining (i.e.
|
@subvertallchris |
Hello! First, I want to thank you so much for this library. I was so happy to find it and even happier to discover how well it's working and being maintained. I gave you and the project a shout out on Twitter, I hope you get more recognition for your work.
I'm curious to know what (if any) patterns exist for providing data to a factory for use in the building of its relations. For instance, image that objects in a project are scoped to an
Organization
. We want to ensure that as a chain of objects are created, they all get the sameorganizationId
key associated with them. What I want to do is something like:In this example,
withTeams
might create a team, each team might create two offices, each office might create three desks. I each to use theorganizationId
if it is available.Right now, I'm doing this manually. I'm using more narrowly scoped factories without traits then gluing them together. But there's a lot of boilerplate that I think the factory could provide. Does a pattern exist for this already, or is it something you'd be open to adding?
The text was updated successfully, but these errors were encountered: