I want to make it so that each book has chapters and the user can add these chapters, and then another user can read them. What is the best way to set up a database? I can’t figure out how to make it so that the user can write as many chapters as he wants to a book.
Hi Anna, it depends on what you are using for database, if you are using some Postgres based service, you should to set two tables, one for the books and another for the chapters of the books, this way you can add as many chapters as you want to a book.
And is it possible to do this together with Plasmic CMS?
I don’t quite understand how to connect chapters to a book. Via List? Or via Ref? I read the documentation, but I still can’t figure out how to make sure that certain chapters are added to each book.
I will be glad to receive any answer Perhaps I am asking very stupid questions…
Hi, you can do it using Ref, which will allow you to assign the chapters to the registered books in your CMS. If you want to create chapters programmatically you will need to use the CMS Api Plasmic CMS - API Reference | Learn Plasmic. For filtering in the Studio, you can specify the ref field and use the bookId to filter chapters for a given book.
A good database structure for this would involve two main tables: Books and Chapters, linked with a one-to-many relationship. Here’s a simple setup:
Tables:
- Books (
id
,title
,author_id
,created_at
, etc.) - Chapters (
id
,book_id
,title
,content
,chapter_number
,created_at
)
Each chapter references a book_id
, allowing unlimited chapters per book. When retrieving chapters, you can order them by chapter_number
. This setup keeps it scalable and flexible for adding, editing, and reading chapters.
Thank you! With the ID it became much clearer
You are always welcome!
Grateful that I helped you in this process!