Add the ability to set a foreign key on a model to create relationships #297
Replies: 2 comments 4 replies
-
Hi, @canrozanes. Thanks for raising this. Tell me, would the usage of transient + derivative properties work for you? const db = factory({
user: {
id: id(Number),
// Create a transient "user.city" property.
// It can be given initial value but it will never
// be set on the user entities.
city: transient(oneOf('city')),
// Derive a public "user.cityId" foreign key
// from the transient "user.city" one-of relationship.
cityId: derivative(({ city }) => city.name),
},
city: {
name: id(String),
},
})
const user = db.user.create({
id: 1,
city: db.city.create({ name: 'London' }),
})
user.cityId // "London"
user.city // undefined
I'm tackling this precise use case in the Data rewrite and I could use your feedback. Why not use
|
Beta Was this translation helpful? Give feedback.
-
I wanted to give a quick update on this: I'm finalizing the revamp of the Data, and relationship declaration will be finally fixed. I believe in conventional database table design, you get a foreign ID column first, and then define a relationship over its value. This is how all ORM tools I found work. I will share more updates once I open the pull request. Need to finalize a few design decisions, battle through TypeScript, add tests, and then we will have it. Thank you for your patience. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be great if developers could be a given the option to create one-to-one relationships using foreign keys. This pattern should feel natural for developers who are familiar with relational databases.
And then we could get posts belonging to a specific user with the following syntax.
Alternatively, the library could implicitly create a
userId
field on a blogPost if blogPost was declared with aoneOf("user")
relationship. I think this is what mirage does.Beta Was this translation helpful? Give feedback.
All reactions