range
Range is used in combination with where and and clauses in Dream.
range supports start, end, or start and end values
import { range } from '@rvoh/dream/utils'
// search Places that sleep at least 3 people
await Place.where({ sleeps: range(3) }).count()
// search Places that sleep 2 or fewer people
await Place.where({ sleeps: range(null, 2) }).count()
// search Places that sleep between 2 and 4 people, inclusive
await Place.where({ sleeps: range(2, 4) }).count()
// search Places that sleep at least 2 and fewer than 4 people
await Place.where({ sleeps: range(2, 4, true) }).count()
The signature is range(begin, end?, excludeEnd?). Pass null for begin to leave the range open at the start, and omit end to leave it open at the end; passing true as excludeEnd leaves end itself out of the range.
range supports CalendarDate, DateTime, ClockTime, and number values
import { range } from '@rvoh/dream/utils'
import { CalendarDate, ClockTime, DateTime } from '@rvoh/dream'
await user
.associationQuery('pets')
.where({
adoptedOn: range(CalendarDate.today().minus({ months: 2 }), CalendarDate.today().minus({ month: 1 })),
})
.order({ adoptedOn: 'desc' })
.all()
await user
.associationQuery('pets')
.where({
adoptedOn: range(DateTime.now().minus({ months: 2 }), DateTime.now().minus({ month: 1 })),
})
.order({ adoptedOn: 'desc' })
.all()
await Appointment.where({
startTime: range(ClockTime.fromObject({ hour: 9, minute: 0 }), ClockTime.fromObject({ hour: 17, minute: 0 })),
}).all()
range can be used in association declaration and statements
const deco = new Decorators<typeof Post>()
export default class Post extends ApplicationModel {
...
@deco.HasMany(Comment, {
and: {
createdAt: () =>
range(CalendarDate.today().minus({ weeks: 2 }))
}
})
public recentComments: Comment[]
}