Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posts with categories #196

Closed
vitalijm opened this issue May 11, 2021 · 3 comments
Closed

Posts with categories #196

vitalijm opened this issue May 11, 2021 · 3 comments

Comments

@vitalijm
Copy link

vitalijm commented May 11, 2021

Hello, thanks for very helpful plugin.
One question here is: how better organize posts with categories [logic]?

  1. Categories better put separately from posts and then do some join?
  2. How better create relations between categories and posts [some ids]?
  3. Sub categories [feature]
  4. Pagination: will be no problem?
  5. How better organize this from performance point?

Maybe someone have example of similar logic: "Posts with categories"?

Thanks

=============================

My some thoughts on solving this "problem":

categories store:
top_category json:
[ { "id": 1, "name": "Cat name 1", "posts": [ 1, 5, 7, 10, 100 ] } ]
sub_category json:
[ { "id": 2, "parent": [ 1, 3 ], "name": "Cat name 2", "posts": [ 1, 10, 20, 23 ] } ]

posts store:
post json:
[ { "id": 1, "name": "Post title 1", "cats": [ 1, 2, 3 ], "other_val": "some_val" } ]

@Timu57
Copy link
Member

Timu57 commented May 29, 2021

Hi @vitalijm
First of all, your solution is good.
You can use joins with SleekDB to organize your NoSQL database if you wish to.
All subsequent requests after the first one are very fast thanks to our caching layer solution. Means joins are made just once.

That said:
Normally when using a NoSQL database you aim to us a different kind of database structure, where you store a subset of the document needed to show the result to the user.
Example:

[ 
  { 
    "id": 1, 
    "name": "Cat name 1", 
    "posts": [ 
      {
        "id": 1, 
        "name": "Post title 1"
      },
      {
        "id": 2, 
        "name": "Post title 2"
      },
      {
        "id": 53, 
        "name": "Post title 53"
      }
    ]
  }
]

With this solution you are able to show all posts with their titles in the category page without any joins or additional requests.
Means: READ is very fast.
But if you rename a Post, you have to change the name not just in the "post" document, you also have to change it in the category "posts" field.
So you have to manage the change by your own.
That is why everyone says that READ is very fast with NoSQL and WRITE is very slow.

If you decide to use a relational approach, SleekDB supports it with joins out of the box.
But be aware that you have to manage the relation by your own, like you have to do with every NoSQL database.
Means if a post gets deleted, you have to remove the id from the category array with an additional query.

I found an "not that bad" blog post regarding this topic, if my explanation was not enough:
https://robvolk.com/nosql-design-patterns-for-relational-data-9c2c11ae3b4a

PS: Pagination is no problem with SleekDB as you can see in this example:
https://sleekdb.github.io/#/complete-examples#retrieve-store

Hope I able to answer your questions and help you.

@Timu57 Timu57 closed this as completed May 29, 2021
@vitalijm
Copy link
Author

thanks for explanation

@Timu57
Copy link
Member

Timu57 commented May 29, 2021

@vitalijm You are welcome.
If you have further questions, feel free to ask

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants