-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
query_builder.ts
164 lines (142 loc) · 4.39 KB
/
query_builder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import { Knex } from 'knex'
import { QueryClientContract } from '../../../types/database.js'
import { LucidRow, LucidModel } from '../../../types/model.js'
import { HasManyQueryBuilderContract } from '../../../types/relations.js'
import { HasMany } from './index.js'
import { getValue, unique } from '../../../utils/index.js'
import { BaseQueryBuilder } from '../base/query_builder.js'
/**
* Extends the model query builder for executing queries in scope
* to the current relationship
*/
export class HasManyQueryBuilder
extends BaseQueryBuilder
implements HasManyQueryBuilderContract<LucidModel, LucidModel>
{
protected appliedConstraints: boolean = false
constructor(
builder: Knex.QueryBuilder,
client: QueryClientContract,
private parent: LucidRow | LucidRow[],
private relation: HasMany
) {
super(builder, client, relation, (userFn) => {
return ($builder) => {
const subQuery = new HasManyQueryBuilder($builder, this.client, this.parent, this.relation)
subQuery.isChildQuery = true
subQuery.isRelatedPreloadQuery = this.isRelatedPreloadQuery
userFn(subQuery)
subQuery.applyWhere()
}
})
}
/**
* Profiler data for HasMany relationship
*/
protected profilerData() {
return {
type: this.relation.type,
model: this.relation.model.name,
relatedModel: this.relation.relatedModel().name,
}
}
/**
* The keys for constructing the join query
*/
protected getRelationKeys(): string[] {
return [this.relation.foreignKey]
}
/**
* Clones the current query
*/
clone() {
const clonedQuery = new HasManyQueryBuilder(
this.knexQuery.clone(),
this.client,
this.parent,
this.relation
)
this.applyQueryFlags(clonedQuery)
clonedQuery.appliedConstraints = this.appliedConstraints
clonedQuery.isRelatedPreloadQuery = this.isRelatedPreloadQuery
clonedQuery.debug(this.debugQueries)
clonedQuery.reporterData(this.customReporterData)
return clonedQuery
}
/**
* Applies constraint to limit rows to the current relationship
* only.
*/
protected applyConstraints() {
if (this.appliedConstraints) {
return
}
const queryAction = this.queryAction()
this.appliedConstraints = true
/**
* Eager query constraints
*/
if (Array.isArray(this.parent)) {
this.wrapExisting().whereIn(
this.relation.foreignKey,
unique(
this.parent.map((model) => {
return getValue(model, this.relation.localKey, this.relation, queryAction)
})
)
)
return
}
/**
* Query constraints
*/
const value = getValue(this.parent, this.relation.localKey, this.relation, queryAction)
this.wrapExisting().where(this.relation.foreignKey, value)
}
/**
* Same as standard model query builder paginate method. But ensures that
* it is not invoked during eagerloading
*/
paginate(page: number, perPage: number = 20) {
if (this.isRelatedPreloadQuery) {
throw new Error(`Cannot paginate relationship "${this.relation.relationName}" during preload`)
}
this.applyConstraints()
return super.paginate(page, perPage)
}
/**
* Returns the group limit query
*/
getGroupLimitQuery() {
const { direction, column } = this.groupConstraints.orderBy || {
column: this.resolveKey(this.relation.relatedModel().primaryKey),
direction: 'desc',
}
const rowName = 'adonis_group_limit_counter'
const partitionBy = `PARTITION BY ${this.relation.foreignKeyColumnName}`
const orderBy = `ORDER BY ${column} ${direction}`
/**
* Select * when no columns are selected
*/
if (!this.getSelectedColumns()) {
this.select('*')
}
this.select(this.client.raw(`row_number() over (${partitionBy} ${orderBy}) as ${rowName}`)).as(
'adonis_temp'
)
const groupQuery = this.relation.relatedModel().query()
groupQuery.usePreloader(this.preloader)
groupQuery.sideload(this.sideloaded)
groupQuery.debug(this.debugQueries)
this.customReporterData && groupQuery.reporterData(this.customReporterData)
return groupQuery.from(this).where(rowName, '<=', this.groupConstraints.limit!)
}
}