Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
How can I turn this nested if function into functional style programming with Ramda in Javascript

I was looking at the code in Prisma for softdelete middleware:

  prisma.$use(async (params, next) => {
    // Check incoming query type
    if (params.model == 'Post') {
      if (params.action == 'delete') {
        // Delete queries
        // Change action to an update
        params.action = 'update'
        params.args['data'] = { deleted: true }
      }
      if (params.action == 'deleteMany') {
        // Delete many queries
        params.action = 'updateMany'
        if (params.args.data != undefined) {
          params.args.data['deleted'] = true
        } else {
          params.args['data'] = { deleted: true }
        }
      }
    }
    return next(params)
  })

The steps it takes is: Check if Post -> if it's a 'delete' or 'deleteMany' then change action to 'update' and set deleted to true.

The nested if's seems not ideal and it seemed that this would be cleaner using a functional style, so I used Ramda to try and make it functional:

const model = lensProp('model')
const action = lensProp('action')
const argsData = compose(lensProp('args'), lensProp('data'))
const cView = curry(view)
const modelView = cView(model)
const actionView = cView(action)
const _shouldBeModified = (models, actions, p) => and(
    includes(modelView(p), models),
    includes(actionView(p), actions)  
)
const shouldBeModified = curry(_shouldBeModified)
const timeElapsed = Date.now();
const today = new Date(timeElapsed);

const softDelete = (models, actions, params) => ifElse(
  shouldBeModified(models, actions), 
  pipe(
    set(argsData, { deleted: true, deletedAt: today.toISOString()}), 
    set(action, 'update')
  ),
  identity
)(params)

I can then call it like this:

softDelete(['Post'],['delete', 'deleteMany'],params)

/*
 returns: {"action": "update", "args": {"data": {"deleted": true, "deletedAt": "2022-04-17T19:49:00.294Z"}}, "model": "Post"}
*/

All of that said, I'm new to Ramda and it seems like my approach is messy, can someone help me clean this up?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use R.when with R.where to check if the params should be updated. If so, use R.evolve to update them.

Pass an actions object, instead of an array, so the code can map the update / updateMany according to the original action.

const { curry, when, where, includes, __, keys, evolve, prop, mergeDeepLeft } = R

const actionsMap = { delete: 'update', deleteMany: 'updateMany' };

const softDelete = curry((models, actions, params) => when(
  where({
    model: includes(__, models),
    action: includes(__, keys(actions))
  }),
  evolve({
    action: prop(__, actions),
    args: mergeDeepLeft({ data: { deleted: true, deletedAt: new Date().toISOString() }})
  })
)(params))

const params = {"action": "deleteMany", "args": { data: { something: 5 } }, "model": "Post"}

const result = softDelete(['Post'], actionsMap, params)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!