Write better jobs with the Lazy State Operator

Hi Community!

I’m really excited to share a shiny new thing with you: The Lazy State Operator!

get($.data.url)

In a nutshell, the Lazy State Operator ($) provides an easier, neater way to reference properties on state. It can make your job code way more elegant.

It means you don’t have to wrap your state paths in dataValue() or arrow functions.

Here’s how you might use it in your code:

post('/patients', $.data.newPatient)

$ is the Lazy State Operator, and it means “Read this path from state just before this operation runs”. Its exactly equivalent to doing this:

post('/patients', (state) => state.data.newPatient)

In fact, you can think of $ as an alias for (state) => state. If you can pass a function, you can use lazy state instead.

You can use the lazy state operator in basically any expression, so long as it’s going into the argument of an operation. You can nest it in an object:

post('www', {
    type: 'patient',
    name: $.data.patient.name,
    address: $.data.patient.address,
})

Or use it in a template literal:

create({
  name: `${$.patients[0].first_name} ${$.patients[0].last_name}`
});

You can read more full documentation here at docs.openfn.org

The Lazy State Operator is new and experimental. We stealth released it a couple of weeks ago so that the team could try it out internally, then we fixed a few things that weren’t working. And now we’re looking from feedback from our community!

Try The Lazy State operator for yourself and let us know if it works well for you! Or, if it doesn’t work, we’d love to hear that too!

Thanks, and happy coding!

-Joe

3 Likes