Skip to content

Instantly share code, notes, and snippets.

@jorinvo
Created November 22, 2015 09:21
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 jorinvo/84c05e5f5483c5007400 to your computer and use it in GitHub Desktop.
Save jorinvo/84c05e5f5483c5007400 to your computer and use it in GitHub Desktop.
mergeOrders - cleaning db query with ramda
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