-
-
Notifications
You must be signed in to change notification settings - Fork 54
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
Support transient attributes #283
Comments
Hi, @veih14. Can you help me understand your requirement a little better? The way to define the relationship you described is this: import { factory, primaryKey, manyOf } from '@mswjs/data'
const db = factory({
user: {
id: primaryKey(faker.uuid),
name: String,
orders: manyOf('order')
},
order: {
id: primaryKey(fake.uuid)
}
})
db.user.create({
name: 'John',
orders: [
db.order.create(),
db.order.create()
]
}) I'm not sure what you mean about that ID relationship though. Each If you could give me a pseudo-code example of what you want to achieve, I'd be able to help you. |
Thank you for your reply @kettanaito ! An example of the object I’m trying to build: |
Thanks for elaborating on the expected behavior! Now it's clear to me. This is a viable use case that we don't support at the moment. Even with the V1 refactoring I'm working on, you'd be able to achieve this only through new functionality that I haven't spec'ed out yet. What I'm thinking, a combination of a relationship and something called a derivative property can work here: factory({
user: { id: id(Number) },
order: {
user: oneOf('user'),
userId: derivative(({ user }) => user.id))
}
}) This will work with V1 even in its current form but you will have the Alternatively, if you're okay with that, you can declare that |
Thank you @kettanaito for the suggestion. So as I understand it won't be supported until V1 is released (no timeline yet right?). |
Yes, that's correct. It would great to have a selective syntax for relationship properties but then it becomes something else, not a relationship between models in its purest form. I think derivatives properties is the right choice here. It's also not uncommon to have "internal" model properties that are not present on the created entities. In the context of Factory Bot, those are called transient attributes and can help achieve the use-case you're after. factory({
user: { id: id(Number) },
order: {
user: transient(oneOf('user')),
userId: derivative(({ user }) => user.id)
}
})
This way |
Hello,
How can I create the schema in a manyof relationship where the ID of one model matches the primary key of another table when the create method is used?
I use faker to generate the UUID.
For example SalesPerson has many Orders in a one to many relationship
How can the SalesPersonId column in the Orders table match the primary key of SalesPerson?
The text was updated successfully, but these errors were encountered: