1

I'm using Vogel.js to connect to Amazon DynamoDB. It's great, however one thing I'm struggling with is that a call such as

MyEntity.get({ my_key: keyValue }, callback);

will either return null if there is no results, or will return a wrapper object.

If I just want to read MyEntity as an object, it appears that in my callback I then have to do

const myEntityAsAnObject = queryResult.get();

however, this will throw if there was no results. So then I have to do some null checking before I do the get. Which I could use lodash or something for, but I'm wondering - is there a nicer way to do this? something like

MyEntity.getAsObject({ my_key: keyValue }, callback);

that will always return the unwrapped object?

RodH257
  • 3,552
  • 5
  • 36
  • 46

1 Answers1

0

The least-inelegant thing I can think of is a higher order function wrapper:

function attrs(callback) {
  return function attrsInner(error, model) {
    callback(error, model && model.get())
  }
}

MyEntity.get({ my_key: keyValue }, attrs(callback));

You could also consider monkey patching the vogels model class prototype in a similar fashion, but I think that would violate the principle of least surprise for someone maintaining your code.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274