Created
November 22, 2015 09:21
-
-
Save jorinvo/84c05e5f5483c5007400 to your computer and use it in GitHub Desktop.
mergeOrders - cleaning db query with ramda
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
const sampleForRow = row => ({ | |
id: row.samplesId, | |
num: row.samplesNum, | |
type: row.samplesType, | |
title: sampleTypes[row.samplesType].title | |
}) | |
const fileForRow = row => ({ | |
id: row.filesId, | |
name: row.filesName, | |
mimetype: row.filesMimetype, | |
link: `/orders/${row.id}/attachments/${row.filesId}` | |
}) | |
const onlySamples = R.filter(R.has('samplesId')) | |
const onlyFiles = R.filter(R.has('filesId')) | |
const samplesForRows = R.pipe(onlySamples, R.map(sampleForRow)) | |
const filesForRows = R.pipe(onlyFiles, R.map(fileForRow)) | |
const groupById = R.groupBy(R.prop('id')) | |
const samplesById = R.pipe(groupById, R.map(samplesForRows)) | |
const filesById = R.pipe(groupById, R.map(filesForRows)) | |
const setSamplesAndFiles = (samples, files, orders) => orders.map( | |
order => R.merge(order, { | |
samples: samples[order.id], | |
attachments: files[order.id] | |
}) | |
) | |
const uniqueById = R.uniqWith(R.eqProps('id')) | |
const fieldsToOmit = [ | |
'samplesType', | |
'samplesId', | |
'samplesNum', | |
'filesId', | |
'filesName', | |
'filesMimetype' | |
] | |
const mergeOrders = R.pipe( | |
R.converge(setSamplesAndFiles, [samplesById, filesById, uniqueById]), | |
R.map(R.omit(fieldsToOmit)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment