malerba118 / react-use-database

React hooks for easy relational data management on the client
54 stars 3 forks source link

Provide an example with related entities #9

Open silviodeligios opened 5 years ago

silviodeligios commented 5 years ago

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

malerba118 commented 5 years ago

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

nmrshll commented 5 years ago

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.

malerba118 commented 5 years ago

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.

nmrshll commented 5 years ago

Awesome, thanks !