Terry Sahaidak
Terry Sahaidak

Reputation: 513

active model serializer flat structure

I'm bulding my api server for my React/Redux app, where i need some flat response from server to manage and reduce my state.

For example, when i have this response:

[{
  id: 1,
  title: 'Some Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}, {
  id: 2,
  title: 'Other Article',
  author: {
    id: 1,
    name: 'Dan'
  }
}]

How can i make it looks like:

{
  result: [1, 2],
  entities: {
    articles: {
      1: {
        id: 1,
        title: 'Some Article',
        author: 1
      },
      2: {
        id: 2,
        title: 'Other Article',
        author: 1
      }
    },
    users: {
      1: {
        id: 1,
        name: 'Dan'
      }
    }
  }
}

Best feature is what each key is articles/users id, so i can easily get it from the response and merge it in my front-end app.

Upvotes: 2

Views: 809

Answers (1)

beauby
beauby

Reputation: 560

You can achieve this with AMS, but you will have to write a custom adapter, as none of the stock ones provide this exact format.

JSON API (and the corresponding AMS JsonApi adapter) solve this issue, but the format is slightly different from the one you want.

Upvotes: 2

Related Questions