react-use-database icon indicating copy to clipboard operation
react-use-database copied to clipboard

Provide an example with related entities

Open silviodeligios opened this issue 5 years ago • 4 comments

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

silviodeligios avatar Apr 18 '19 13:04 silviodeligios

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”.

malerba118 avatar Apr 18 '19 13:04 malerba118

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.

nmrshll avatar May 26 '19 17:05 nmrshll

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.

malerba118 avatar May 26 '19 19:05 malerba118

Awesome, thanks !

nmrshll avatar May 26 '19 20:05 nmrshll