'combine static query methods in mongoose

I have the following which works as expected:

function by_ts_or_message_ts(tsOrMessageTs) {
  return this.findOne({
    $or: [{ ts: tsOrMessageTs }, { "messages.ts": tsOrMessageTs }],
  })
}

However, I'd like to break it down further to smaller reusable methods, so given this:

threadSchema.static("by_ts", by_ts)
threadSchema.static("by_message_ts", by_message_ts)

function by_ts(ts) {
  return this.findOne({ ts })
}

function by_message_ts(ts) {
  return this.findOne({ "messages.ts": ts })
}

I'd like to do something like the following (psuedo code)

threadSchema.static("by_ts_or_message_ts", by_ts_or_message_ts)

function by_ts_or_message_ts(tsOrMessageTs) {
  return or([
    this.by_message_ts(tsOrMessageTs),
    this.by_ts(tsOrMessageTs),
  ])
}

is this possible in mongoose?

in rails for example, with the mongodb driver, I can do:

self.any_of(
  self.by_ts(ts).selector,
  self.by_ts_or_message_ts(ts).selector,
)

is there an equivalent way in mongoose?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source