-
-
Save RPallas92/5ee31bff76e875d66e9915d3aa9f737e to your computer and use it in GitHub Desktop.
Data mapping with sanctuary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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: '[email protected]', | |
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