I’m working on a courses Application where the user can sort his content however he like
for example say there is
- Courses Table – contains course data(title, thumnail…) sub-courses and videos
- Videos Table – contains video data(video url, title…)
so what I did was creating 2 OneToMany relationships in the Courses Table
- one that references sub courses inside Courses Table(like parent/children)
- the other one references videos in the Videos Table
however I’m trying to make the user able to sort them however he like not like a creation date sort but each child has an index and content get’s sorted based on that index in the UI
I’m using a PostgreSQL database
I’m new to sql and databases in general and I don’t know if my design is right if you have an suggestions please tell me
also if you know any good articles or tutorials in this topics please link them
2
Answers
I found a solution my self
I created a separate table called
CourseChildren
the table has and sort_index column
it has 2 nullable relations one with the courses table and one for the videos table
I added a check constraint which will ensure that one of course or video relations is not null (not both not neither)
I added a composite unique key that will make sure that parentId and sort_index isn't dublicated
this is the implementation in typeorm
You can add a new column in both tables which stores the sorting order.
Lets call this column "sort_index". Whenever a user changes the sort order from frontend, you just update this index value for courses and videos.
Something like this (applicable to both courses & videos):
Next, you just need to display it on UI as per the index.