Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Forked from josete89/dataTransform.js
Created November 14, 2017 20:02
Show Gist options
  • Save davidchambers/3b3fce18d0dda1057fa4e31a3ea5511d to your computer and use it in GitHub Desktop.
Save davidchambers/3b3fce18d0dda1057fa4e31a3ea5511d to your computer and use it in GitHub Desktop.
Data mapping with sanctuary
'use strict';
const R = require('ramda');
const S = require('sanctuary');
const data = {
id: 1,
orderName: 'Order from spain',
teamName: 'Portland penguins',
orderType: 'lock',
discount: 5,
contactName: 'John doe',
contactPhonePrefix: '+32',
contactPhoneNumber: '3423232',
contactEmail: 'jhon.doe@mycompany.com',
contactAddress: '830 Southwest',
dealerId: '123',
dealerNote: 'Lorem ipsum',
createdAt: '2017-11-13T16:40:12.000Z',
updatedAt: '2017-11-13T16:40:12.000Z',
products: [
{
id: 5,
externalId: 'C77124',
createdAt: '2017-11-13T16:40:12.000Z',
updatedAt: '2017-11-13T16:40:12.000Z',
OrderProduct: {
createdAt: '2017-11-13T16:40:12.000Z',
updatedAt: '2017-11-13T16:40:12.000Z',
orderId: 1,
productId: 5,
},
selectedSizes: [
{
id: 1,
technicalSize: 520,
quantity: 5,
createdAt: '2017-11-13T16:40:12.000Z',
updatedAt: '2017-11-13T16:40:12.000Z',
ProductSelectedSize: {
createdAt: '2017-11-13T16:40:12.000Z',
updatedAt: '2017-11-13T16:40:12.000Z',
productId: 5,
selectedSizeId: 1,
},
},
],
},
],
};
const transformation = {
id: 'id',
order_name: 'orderName',
team_name: 'teamName',
order_type: 'orderType',
discount: 'discount',
'contact.name': 'contactName',
'contact.phone_number.prefix': 'contactPhonePrefix',
'contact.phone_number.number': 'contactPhoneNumber',
'contact.email': 'contactEmail',
'contact.address': 'contactAddress',
product_list: [
'products',
{
id: 'id',
product_id: 'externalId',
selected_sizes: [
'selectedSizes',
{
id: 'id',
technical_size: 'technicalSize',
quantity: 'quantity',
},
],
},
],
dealer_id: 'dealerId',
dealer_note: 'dealerNote',
};
const mappingStuff = S.curry2((transformation, data) =>
S.reduce(
acc => val => S.chain(fillObject(data, transformation, val), acc),
S.Right({}),
Object.keys(transformation)
)
);
const propertyExists = S.curry2((object, property) =>
R.has(property, object) ?
S.Right(object[property]) :
S.Left(`Property not exists ${property}`)
);
const fillObject = S.curry4((dbData, transform, currentKey, acc) => {
const dbPropName = propertyExists(transform, currentKey);
const dbValue = S.chain(propertyExists(dbData), dbPropName);
return S.chain(
x =>
S.is(Array, x) ?
handleArrayProperty(dbData, transform, acc, currentKey, x) :
S.map(R.assocPath(S.splitOn('.', currentKey), S.__, acc), dbValue),
dbPropName
);
});
function handleArrayProperty(dbData, transform, acc, currentKey, dbPropName) {
const [label, record] = transform[currentKey];
return S.pipe([S.prop(label),
S.map(mappingStuff(record)),
R.assoc(currentKey, S.__, acc),
S.Right],
dbData);
}
console.log(mappingStuff(transformation, data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment