OpenFn / adaptors

The new home for OpenFn adaptors; re-usable connectors for the most common DPGs and DPI building blocks.
GNU General Public License v3.0
7 stars 8 forks source link

common: add something like a `save()` or `as()` function #800

Open josephjclark opened 1 week ago

josephjclark commented 1 week ago

A common use case is to run an operation and then to save the result back to state on a key other than data

Like this:

get('fixture/?fixture_type=option_set_mapping', {}, state => {
  state.lookup_table.option_set = state.data;
  return state;
});

This is really quite painful.

What if we had a wrapper operation that was able to take any state.data object and proxy/alias/push it to some other key:

save('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))

The implementation is something like this:

function save(key, op) {
  return async (state) => {
       const data = state.data // save the incoming state
       const result  = await(op(state))
       state[key]=result;
       state.data = data;       
       return state;
  }
}

I don't like name though. Some ideas:

keyby('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
key('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
alias('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
proxy('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))
josephjclark commented 1 week ago

Ooh maybe as

as('lookup_table.option_set', get('fixture/?fixture_type=option_set_mapping'))

I like this because it's quite natural language-y. It would be better to do like get(something).as(key), but the as function would have no means to restore the prior state. The get would just blat it. References would be a workaround but we can't guarantee it will have written.