Is it possible to have MSW Data automatically generate a related object? #284
Replies: 1 comment
-
Hi, @philcunliffe. At its current state, Data doesn't automatically generate any end of a declared relationship. You have to associate models manually. The relationship itself acts as a type and runtime guard of the values you provide to the relational property (so you don't associate const db = factory({
user: {
id: primaryKey(String),
posts: manyOf('post')
},
post: {
id: primaryKey(String),
title: String
}
})
db.author.create({
posts: [
db.post.create({ title: 'First post' }),
db.post.create({ title: 'Second post' })
]
}) While this works for simple relationships, I acknowledge that it becomes problematic to manually associate many-to-one relationships, for example, where the nature of a relationship is circular ( I'm working on the next version of this library that already addresses this issue and introduces something called inversed relationships. If you have any particular use cases to share about how and when you want to use relationships that automatically generate values, please do so below. Thanks. |
Beta Was this translation helpful? Give feedback.
-
Let's take the user / post author example from the documentation. Is it possible to have MSW Data automatically generate a user to assign to the author variable if one isn't passed in?
Beta Was this translation helpful? Give feedback.
All reactions