react-use-database
react-use-database copied to clipboard
Provide an example with related entities
Can you add a related entity to your todo example? Like an Author or something similar. I can't understand how to manage cases like this.
Thank you
For the short term I would say have a look at this article, but I’ll try to include some more details in the readme about relationships and I’ll see if I can beef up the demo as well. Relationships are mostly in the domain of normalizr so you may wish to google something like “how does normalizr handle related entities”.
Agreed, this would be useful as managing one relational source of truth is the main purpose of this lib.
Here's the most relevant code extract from the article, for creating schemas of two linked entities, Users and Todos:
import {schema} from 'normalizr'
const UserSchema = new schema.Entity(
"User",
{},
{ idAttribute: "id" }
)
const TodoSchema = new schema.Entity(
"Todo",
{ author: UserSchema }, // create the User-Todo relation in the schemas
{ idAttribute: "id" }
)
const responseSchema = new schema.Object({
body: new schema.Array(TodoSchema)
})
This is more normalizr specific though.
@malerba118 could you show us here (with a quick example) how to use createDB with several schemas ? (or point us to an example that does that) ? Thanks in advance.
Yeah, sorry for the delay on this. I’ve been struggling to come up with a good demo to illustrate the full execution flow. I’ll try to get an interesting interactive demo out there soon to illustrate how the library can be useful for related models.
Awesome, thanks !