Objection.js: Add a reload() helper method?

Created on 24 Oct 2017  路  2Comments  路  Source: Vincit/objection.js

Coming from the rails word, I kind of miss the reload method.

I have noticed the following instructions in the documentation:

// If you need to refresh the same instance you can do this:
person.$query().then(reFetchedPerson => {
  // Note that `person` did not get modified by the fetch.
  person.$set(reFetchedPerson);
});

But It has proven to be cumbersome in practice. We have been writing the following plugin to make things easier:

function utils(Model) {
  return class extends Model {
    // Close to https://apidock.com/rails/ActiveRecord/Persistence/reload
    async reload() {
      const reFetched = await this.$query()
      this.$set(reFetched)
    }
  }
}

export default utils

You could be interested in moving this helper into the core of the lib (I'm not sure about it), at least I wanted to give some feedback about my experience using objection.js.

Most helpful comment

IMO this shouldn't be a part in core, since there are so much this kind of small helpers that some people finds useful, that all of them cannot be added. Also we don't want to pollute namespace of base model, so method name would be $reload() I think.

I believe that its best to keep objection's base model really clean and let all the users define themselves these small helpers to their own base classes or to implement them as plugins.

All 2 comments

IMO this shouldn't be a part in core, since there are so much this kind of small helpers that some people finds useful, that all of them cannot be added. Also we don't want to pollute namespace of base model, so method name would be $reload() I think.

I believe that its best to keep objection's base model really clean and let all the users define themselves these small helpers to their own base classes or to implement them as plugins.

I agree with the previous dude.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ahlid picture Ahlid  路  3Comments

haywirez picture haywirez  路  3Comments

sgangwisch picture sgangwisch  路  4Comments

bsdo64 picture bsdo64  路  3Comments

Gustav0ar picture Gustav0ar  路  4Comments