Objection.js: Named modifiers don't work with optional arguments

Created on 12 Apr 2019  路  5Comments  路  Source: Vincit/objection.js

I want to provide some additional argument in named modifier, but when I'm calling it this way

query.midify('filters', [filter_query])

it's try to find named modifier with [filter_query].
Is it expected behavior?

bug

Most helpful comment

As a workaround, you can use the query context

class Person extends Model {
  static modifiers = {
    filters: query => query.where({ age: query.context().age })
  }
}

Person.query()
  .mergeContext({ age: 21 })
  .modify('filters')

All 5 comments

Cannot reproduce based on your example. Works on my tests.

@koskimas Maybe my example wasn't so clear.
My model file:

class Person extends Model {
  staic modifiers = {
    filters: (query, context) => query.where({ age: context.age })
  }
}

calling this modifier

Person.query()
  .modify('filters', { age: 21 })

I'm expecting to have { age: 21 } in my modifier second argument (context). But instead I doesn't have second argument at all (undefined);

Oh, ok. Yeah, that's a known limitation with the modifiers. Fixing this is a breaking change and needs to wait for 2.0

As a workaround, you can use the query context

class Person extends Model {
  static modifiers = {
    filters: query => query.where({ age: query.context().age })
  }
}

Person.query()
  .mergeContext({ age: 21 })
  .modify('filters')

Fixed in v2.0 branch.

Was this page helpful?
0 / 5 - 0 ratings