-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
query_builder.ts
136 lines (120 loc) · 3.36 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
/*
* @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 { LucidRow } from '../../../types/model.js'
import { QueryClientContract } from '../../../types/database.js'
import { HasOne } 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 HasOneQueryBuilder extends BaseQueryBuilder {
protected appliedConstraints: boolean = false
constructor(
builder: Knex.QueryBuilder,
client: QueryClientContract,
private parent: LucidRow | LucidRow[],
private relation: HasOne
) {
super(builder, client, relation, (userFn) => {
return ($builder) => {
const subQuery = new HasOneQueryBuilder($builder, this.client, this.parent, this.relation)
subQuery.isChildQuery = true
subQuery.isRelatedPreloadQuery = this.isRelatedPreloadQuery
userFn(subQuery)
subQuery.applyWhere()
}
})
}
/**
* Profiler data for HasOne 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 HasOneQueryBuilder(
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
}
this.appliedConstraints = true
const queryAction = this.queryAction()
/**
* 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)
/**
* Do not add limit when updating or deleting
*/
if (!['update', 'delete'].includes(queryAction)) {
this.limit(1)
}
}
/**
* Dis-allow hasOne pagination
*/
paginate(): Promise<any> {
throw new Error(`Cannot paginate a hasOne relationship "(${this.relation.relationName})"`)
}
/**
* Dis-allow hasOne group query limit
*/
getGroupLimitQuery(): never {
throw new Error(
`Cannot apply groupLimit or groupOrderBy on hasOne relationship "(${this.relation.relationName})"`
)
}
}