DEV Community

Discussion on: Best way to store array type data inside database?

Collapse
 
rhymes profile image
rhymes

Depends on what tags mean in your app, as in both examples have ids I guess they are part of your domain model. You can have a table which lists all available tags and another one with foreign keys to both posts and tags to list which posts have which tags.

Collapse
 
mittalyashu profile image
Yashu Mittal • Edited

As I have mentioned in the article. A post can have multiple tags.

Collapse
 
rhymes profile image
rhymes

So yeah, having a many to many table is one solution. But you can also store them as an array or as JSONB in PostgreSQL. It depends if tags themselves are an entity in your app you want to attach metadata to or are just, well, an array of names you might want to query on (for example to give you the list of posts that have a specific tag)

Thread Thread
 
mittalyashu profile image
Yashu Mittal

Yes, tags itself have a separate entity in the app, to explain it in simpler words, this is how the data output I am expecting.

{
  postId: 3453,
  title: "sdf",
  slug: "sfs",
  tags: [
    {
      name: "sdfs",
      id: "34534"
    },
    {
      name: 'sdfs',
      id: "35344"
    }
  ]
}