-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathqueryDrafts.ts
More file actions
193 lines (168 loc) · 5.31 KB
/
queryDrafts.ts
File metadata and controls
193 lines (168 loc) · 5.31 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import type { PaginateOptions, PipelineStage, QueryOptions } from 'mongoose'
import type { QueryDrafts } from 'payload'
import { buildVersionCollectionFields, combineQueries, flattenWhereToOperators } from 'payload'
import type { MongooseAdapter } from './index.js'
import { buildQuery } from './queries/buildQuery.js'
import { buildSortParam } from './queries/buildSortParam.js'
import { aggregatePaginate } from './utilities/aggregatePaginate.js'
import { buildJoinAggregation } from './utilities/buildJoinAggregation.js'
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'
import { getCollection } from './utilities/getEntity.js'
import { getSession } from './utilities/getSession.js'
import { resolveJoins } from './utilities/resolveJoins.js'
import { transform } from './utilities/transform.js'
export const queryDrafts: QueryDrafts = async function queryDrafts(
this: MongooseAdapter,
{
collection: collectionSlug,
joins,
limit,
locale,
page,
pagination,
req,
select,
sort: sortArg,
where = {},
},
) {
const { collectionConfig, Model } = getCollection({
adapter: this,
collectionSlug,
versions: true,
})
let hasNearConstraint
let sort
if (where) {
const constraints = flattenWhereToOperators(where)
hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))
}
const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)
const sortAggregation: PipelineStage[] = []
if (!hasNearConstraint) {
sort = buildSortParam({
adapter: this,
config: this.payload.config,
fields,
locale,
sort: sortArg || collectionConfig.defaultSort,
sortAggregation,
timestamps: true,
versions: true,
})
}
const combinedWhere = combineQueries({ latest: { equals: true } }, where)
const versionQuery = await buildQuery({
adapter: this,
fields,
locale,
where: combinedWhere,
})
const projection = buildProjectionFromSelect({
adapter: this,
fields,
select,
})
const session = await getSession(this, req)
const options: QueryOptions = {
session,
}
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
const useEstimatedCount =
hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0
const paginationOptions: PaginateOptions = {
lean: true,
leanWithId: true,
options,
page,
pagination,
projection,
sort,
useEstimatedCount,
}
if (this.collation) {
const localizationConfig = this.payload.config.localization
const defaultLocale =
(typeof localizationConfig === 'object' && localizationConfig?.defaultLocale) || 'en'
paginationOptions.collation = {
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
...this.collation,
}
}
if (
!useEstimatedCount &&
Object.keys(versionQuery).length === 0 &&
this.disableIndexHints !== true
) {
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
// which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses
// the correct indexed field
paginationOptions.useCustomCountFn = () => {
return Promise.resolve(
Model.countDocuments(versionQuery, {
collation: paginationOptions.collation,
hint: { _id: 1 },
session,
}),
)
}
}
if (limit && limit > 0) {
paginationOptions.limit = limit
// limit must also be set here, it's ignored when pagination is false
paginationOptions.options!.limit = limit
}
let result
const aggregate = await buildJoinAggregation({
adapter: this,
collection: collectionSlug,
collectionConfig,
joins,
locale,
projection,
query: versionQuery,
versions: true,
})
if (aggregate || sortAggregation.length > 0) {
result = await aggregatePaginate({
adapter: this,
collation: paginationOptions.collation,
joinAggregation: aggregate,
limit: paginationOptions.limit,
Model,
page: paginationOptions.page,
pagination: paginationOptions.pagination,
projection: paginationOptions.projection,
query: versionQuery,
session: paginationOptions.options?.session ?? undefined,
sort: paginationOptions.sort as object,
sortAggregation,
useEstimatedCount: paginationOptions.useEstimatedCount,
})
} else {
result = await Model.paginate(versionQuery, paginationOptions)
}
if (!this.useJoinAggregations) {
await resolveJoins({
adapter: this,
collectionSlug,
docs: result.docs as Record<string, unknown>[],
joins,
locale,
versions: true,
})
}
transform({
adapter: this,
data: result.docs,
fields: buildVersionCollectionFields(this.payload.config, collectionConfig),
operation: 'read',
})
for (let i = 0; i < result.docs.length; i++) {
const id = result.docs[i].parent
result.docs[i] = result.docs[i].version ?? {}
result.docs[i].id = id
}
return result
}