Skip to content

Instantly share code, notes, and snippets.

@adrianmcli
Last active April 17, 2017 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianmcli/f572e28ed89694c3c099551a0a3fe7dd to your computer and use it in GitHub Desktop.
Save adrianmcli/f572e28ed89694c3c099551a0a3fe7dd to your computer and use it in GitHub Desktop.
When the data that the client wants to consume is different from the database model, transform it on the backend before sending it down.
export default (show, venueType) => {
const tiers = show.tierPrices.map((price, index) => ({
title: venueType[`tier${index + 1}Title`],
description: venueType[`tier${index + 1}Description`],
price,
}));
return {
name: show.name,
venueType: venueType.name,
tiers,
};
};
/* global describe, expect */
import transformData from './tierTransform';
const dbModel = {
venueType: {
name: 'Stadium',
tier1Title: 'Upper Balcony',
tier1Description: '...',
tier2Title: 'Balcony',
tier2Description: '...',
tier3Title: 'Floor',
tier3Description: '...',
},
show: {
name: 'Awesome Show',
tierPrices: [5000, 7500, 10000],
},
};
describe('from db model to client consumption', () => {
it('should transform data correctly', () => {
const showData = {
name: 'Awesome Show',
venueType: 'Stadium',
tiers: [
{
title: 'Upper Balcony',
description: '...',
price: 5000,
},
{
title: 'Balcony',
description: '...',
price: 7500,
},
{
title: 'Floor',
description: '...',
price: 10000,
},
],
};
expect(transformData(dbModel.show, dbModel.venueType)).toEqual(showData);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment