This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| insert into "person" (id, first_name, last_name) | |
| values ($1, $2, $3) | |
| on conflict ("id") do nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| db.insertInto('person') | |
| .values(person) | |
| .onConflictDoNothing('id') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with "species_ages" as ( | |
| select "species", | |
| min("pet"."age") as "min_age", | |
| max("pet"."age") as "max_age" | |
| from "pet" | |
| group by "species" | |
| ) | |
| select "max_age" | |
| from "species_ages", ( | |
| select "species", count("toy"."id") as "num_toys" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| await Person.query() | |
| .joinRelated('children(isFemale, hasPets, isJennifer)') | |
| .modifiers({ | |
| hasPets(query: QueryBuilder<Person>): void { | |
| query.whereExists(Person.relatedQuery('pets')) | |
| }, | |
| isJennifer(query: QueryBuilder<Person>): void { | |
| query.modify('findByName', 'Jennifer') | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| await Person.query() | |
| .withGraphFetched('children(isFemale, hasPets, isJennifer)') | |
| .modifiers({ | |
| // New query-local modifier | |
| hasPets(query: QueryBuilder<Person>): void { | |
| query.whereExists(Person.relatedQuery('pets')) | |
| }, | |
| // Bind argument to existing modifier. | |
| isJennifer(query: QueryBuilder<Person>): void { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Modifiers can be used with any query. | |
| await Person.query().modify('findByName', fullName) | |
| // Modifiers can be used with eager loading. | |
| await Person.query().withGraphFetched('children(isFemale)') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person extends Model { | |
| static async beforeDelete({ | |
| asFindQuery, | |
| cancelQuery | |
| }: StaticHookArguments<Person>): Promise<any> { | |
| const idsOfPersonsAboutToBeDeleted = await asFindQuery().select('id') | |
| if (!(await shouldDelete(idsOfPersonsAboutToBeDeleted))) { | |
| cancelQuery() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Note that the following query isn't executed! There's no `await`. | |
| const jennifers = Actor.query().where('firstName', 'Jennifer') | |
| // This is the only query that gets executed. `jennifers` query is built into | |
| // the SQL as a subquery. | |
| const movies = await Actor.relatedQuery('movies') | |
| .for(jennifers) | |
| .where('genre', 'action') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Note that the following query isn't executed! There's no `await`. | |
| const finnishActorsThatHaveChildren = Actor.query() | |
| .where('nationality', 'finnish') | |
| .whereExists(Actor.relatedQuery('children')) | |
| // This is the only query that gets executed. `finnishActorsThatHaveChildren` | |
| // query is built into the SQL as a subquery. | |
| await Actor.relatedQuery('movies') | |
| .for(finnishActorsThatHaveChildren) | |
| .unrelate() |
NewerOlder