Skip to content

Instantly share code, notes, and snippets.

@koskimas
Last active November 18, 2019 08:40
Show Gist options
  • Select an option

  • Save koskimas/d4593bd2df4afd405394c19ab641ad51 to your computer and use it in GitHub Desktop.

Select an option

Save koskimas/d4593bd2df4afd405394c19ab641ad51 to your computer and use it in GitHub Desktop.
class Person extends Model {
static modifiers = {
isFemale(query: QueryBuilder<Person>): void {
query.where('gender', 'female')
},
findByName(query: QueryBuilder<Person>, name: string): void {
const { ref, fn } = Person
const { lower } = fn
const [firstName, lastName] = name.split(' ')
if (firstName) {
// Generates `WHERE lower("persons"."firstName") = ?`
query.where(lower(ref('firstName')), firstName.toLowerCase())
}
if (lastName) {
// Generates `AND lower("persons"."lastName") = ?`
query.where(lower(ref('lastName')), lastName.toLowerCase())
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment