@payloadcms/drizzle 4.0.0-canary.32 → 4.0.0-canary.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/find/buildPolymorphicJoinQuery.d.ts +38 -0
- package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinQuery.js +135 -0
- package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
- package/dist/find/buildPolymorphicJoinQuery.spec.js +384 -0
- package/dist/find/buildPolymorphicJoinQuery.spec.js.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.js +301 -0
- package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.spec.js +805 -0
- package/dist/find/buildPolymorphicJoinWhere.spec.js.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts +37 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.js +191 -0
- package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.spec.js +778 -0
- package/dist/find/createPolymorphicJoinWherePlan.spec.js.map +1 -0
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +19 -191
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/postgres/createJSONQuery/index.js +3 -3
- package/dist/postgres/createJSONQuery/index.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +9 -0
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +59 -2
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
- package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/index.js +6 -5
- package/dist/sqlite/createJSONQuery/index.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +5 -14
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +41 -1
- package/dist/updateOne.js.map +1 -1
- package/dist/updateOne.spec.js +84 -0
- package/dist/updateOne.spec.js.map +1 -0
- package/dist/upsertRow/conditionalUpdate.spec.js +61 -0
- package/dist/upsertRow/conditionalUpdate.spec.js.map +1 -0
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +25 -5
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
- package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
- package/dist/utilities/parseJSONPathSegment.js +17 -0
- package/dist/utilities/parseJSONPathSegment.js.map +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { and, getTableName, isNotNull, isNull, or, sql } from 'drizzle-orm';
|
|
2
|
+
import { QueryError } from 'payload';
|
|
3
|
+
import toSnakeCase from 'to-snake-case';
|
|
4
|
+
import { buildOperatorConstraint } from '../queries/buildOperatorConstraint.js';
|
|
5
|
+
import { sanitizeQueryValue } from '../queries/sanitizeQueryValue.js';
|
|
6
|
+
const supportedJSONQueryOperators = new Set([
|
|
7
|
+
'contains',
|
|
8
|
+
'equals',
|
|
9
|
+
'exists',
|
|
10
|
+
'in',
|
|
11
|
+
'like'
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* Builds a where clause for one collection before the polymorphic branches are combined. A field
|
|
15
|
+
* that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current
|
|
16
|
+
* collection slug.
|
|
17
|
+
*/ export const buildPolymorphicJoinWhere = ({ adapter, collection, locale, table, where, wherePlan })=>{
|
|
18
|
+
const constraints = [];
|
|
19
|
+
for(const key in where){
|
|
20
|
+
if ([
|
|
21
|
+
'AND',
|
|
22
|
+
'OR'
|
|
23
|
+
].includes(key.toUpperCase())) {
|
|
24
|
+
if (!Array.isArray(where[key])) {
|
|
25
|
+
throw new QueryError([
|
|
26
|
+
{
|
|
27
|
+
path: key
|
|
28
|
+
}
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
const combine = key.toUpperCase() === 'AND' ? and : or;
|
|
32
|
+
const nestedConstraints = where[key].map((nestedWhere)=>buildPolymorphicJoinWhere({
|
|
33
|
+
adapter,
|
|
34
|
+
collection,
|
|
35
|
+
locale,
|
|
36
|
+
table,
|
|
37
|
+
where: nestedWhere,
|
|
38
|
+
wherePlan
|
|
39
|
+
}));
|
|
40
|
+
const nestedCondition = combine(...nestedConstraints);
|
|
41
|
+
if (nestedCondition) {
|
|
42
|
+
constraints.push(nestedCondition);
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const resolvedPath = resolveWherePath({
|
|
47
|
+
adapter,
|
|
48
|
+
collection,
|
|
49
|
+
schemaPath: key,
|
|
50
|
+
table,
|
|
51
|
+
wherePlan
|
|
52
|
+
});
|
|
53
|
+
for (let payloadOperator of Object.keys(where[key])){
|
|
54
|
+
const originalOperator = payloadOperator;
|
|
55
|
+
let value = where[key][payloadOperator];
|
|
56
|
+
if (resolvedPath.type === 'invalid') {
|
|
57
|
+
throw new QueryError([
|
|
58
|
+
{
|
|
59
|
+
path: `${key}.${payloadOperator}`
|
|
60
|
+
}
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
if (resolvedPath.type === 'hasManySelect') {
|
|
64
|
+
if (!supportedJSONQueryOperators.has(payloadOperator)) {
|
|
65
|
+
throw new QueryError([
|
|
66
|
+
{
|
|
67
|
+
path: `${key}.${payloadOperator}`
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
72
|
+
adapter,
|
|
73
|
+
field: resolvedPath.fieldContext,
|
|
74
|
+
isUUID: false,
|
|
75
|
+
operator: payloadOperator,
|
|
76
|
+
relationOrPath: key,
|
|
77
|
+
val: value
|
|
78
|
+
});
|
|
79
|
+
if (sanitizedQueryValue === null) {
|
|
80
|
+
throw new QueryError([
|
|
81
|
+
{
|
|
82
|
+
path: `${key}.${originalOperator}`
|
|
83
|
+
}
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
if (sanitizedQueryValue.columns) {
|
|
87
|
+
throw new QueryError([
|
|
88
|
+
{
|
|
89
|
+
path: `${key}.${originalOperator}`
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
value = sanitizedQueryValue.value;
|
|
94
|
+
if (payloadOperator === 'in' && Array.isArray(value) && value.length === 0) {
|
|
95
|
+
constraints.push(sql.raw('false'));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const jsonQueryOperator = payloadOperator === 'exists' ? 'exists' : sanitizedQueryValue.operator;
|
|
99
|
+
constraints.push(sql.raw(adapter.createJSONQuery({
|
|
100
|
+
column: resolvedPath.jsonExpression,
|
|
101
|
+
operator: jsonQueryOperator,
|
|
102
|
+
pathSegments: [
|
|
103
|
+
resolvedPath.pathPlan.columnPath
|
|
104
|
+
],
|
|
105
|
+
value
|
|
106
|
+
})));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (payloadOperator === '$raw') {
|
|
110
|
+
if (typeof value !== 'string') {
|
|
111
|
+
throw new QueryError([
|
|
112
|
+
{
|
|
113
|
+
path: `${key}.${payloadOperator}`
|
|
114
|
+
}
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
constraints.push(sql.raw(value));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const { column, fieldContext, queryValueContext } = resolvedPath;
|
|
121
|
+
if (payloadOperator === 'like' && (queryValueContext.field.type === 'number' || queryValueContext.field.type === 'relationship' || queryValueContext.field.type === 'upload' || queryValueContext.isUUID)) {
|
|
122
|
+
payloadOperator = 'equals';
|
|
123
|
+
}
|
|
124
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
125
|
+
adapter,
|
|
126
|
+
field: queryValueContext.field,
|
|
127
|
+
isUUID: queryValueContext.isUUID,
|
|
128
|
+
operator: payloadOperator,
|
|
129
|
+
relationOrPath: key,
|
|
130
|
+
val: value
|
|
131
|
+
});
|
|
132
|
+
if (sanitizedQueryValue === null) {
|
|
133
|
+
throw new QueryError([
|
|
134
|
+
{
|
|
135
|
+
path: `${key}.${originalOperator}`
|
|
136
|
+
}
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
if (sanitizedQueryValue.columns) {
|
|
140
|
+
throw new QueryError([
|
|
141
|
+
{
|
|
142
|
+
path: `${key}.${originalOperator}`
|
|
143
|
+
}
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
payloadOperator = sanitizedQueryValue.operator;
|
|
147
|
+
value = sanitizedQueryValue.value;
|
|
148
|
+
if (!(payloadOperator in adapter.operators)) {
|
|
149
|
+
throw new QueryError([
|
|
150
|
+
{
|
|
151
|
+
path: `${key}.${originalOperator}`
|
|
152
|
+
}
|
|
153
|
+
]);
|
|
154
|
+
}
|
|
155
|
+
const resolvedOperator = payloadOperator;
|
|
156
|
+
const operator = adapter.operators[resolvedOperator];
|
|
157
|
+
const buildColumnConstraint = (constraintValue)=>{
|
|
158
|
+
if (!fieldContext) {
|
|
159
|
+
return operator(column, constraintValue);
|
|
160
|
+
}
|
|
161
|
+
return buildOperatorConstraint({
|
|
162
|
+
adapter,
|
|
163
|
+
column,
|
|
164
|
+
field: fieldContext.field,
|
|
165
|
+
locale,
|
|
166
|
+
originalOperator,
|
|
167
|
+
path: key,
|
|
168
|
+
resolvedOperator,
|
|
169
|
+
value: constraintValue
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
if (originalOperator === 'equals' && value === null) {
|
|
173
|
+
constraints.push(isNull(column));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (originalOperator === 'not_equals') {
|
|
177
|
+
if (value === null) {
|
|
178
|
+
constraints.push(isNotNull(column));
|
|
179
|
+
} else {
|
|
180
|
+
const notEqualsConstraint = or(isNull(column), buildColumnConstraint(value));
|
|
181
|
+
if (notEqualsConstraint) {
|
|
182
|
+
constraints.push(notEqualsConstraint);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (originalOperator === 'in' && Array.isArray(value) && value.includes(null)) {
|
|
188
|
+
const nonNullValues = value.filter((item)=>item !== null);
|
|
189
|
+
const inConstraint = buildColumnConstraint(nonNullValues);
|
|
190
|
+
const nullAwareInConstraint = or(isNull(column), inConstraint);
|
|
191
|
+
if (nullAwareInConstraint) {
|
|
192
|
+
constraints.push(nullAwareInConstraint);
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (payloadOperator === 'like' && typeof value === 'string') {
|
|
197
|
+
const wordConstraints = value.split(' ').map((word)=>buildColumnConstraint(`%${word}%`));
|
|
198
|
+
const wordCondition = and(...wordConstraints);
|
|
199
|
+
if (wordCondition) {
|
|
200
|
+
constraints.push(wordCondition);
|
|
201
|
+
}
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
constraints.push(buildColumnConstraint(value));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return and(...constraints);
|
|
208
|
+
};
|
|
209
|
+
const resolveWherePath = ({ adapter, collection, schemaPath, table, wherePlan })=>{
|
|
210
|
+
const pathPlan = wherePlan.get(schemaPath);
|
|
211
|
+
if (!pathPlan) {
|
|
212
|
+
throw new QueryError([
|
|
213
|
+
{
|
|
214
|
+
path: schemaPath
|
|
215
|
+
}
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
if (pathPlan.type === 'invalid') {
|
|
219
|
+
return {
|
|
220
|
+
type: 'invalid',
|
|
221
|
+
pathPlan
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (schemaPath === 'relationTo') {
|
|
225
|
+
return {
|
|
226
|
+
type: 'scalar',
|
|
227
|
+
column: sql`${collection}`,
|
|
228
|
+
pathPlan,
|
|
229
|
+
queryValueContext: {
|
|
230
|
+
type: 'scalar',
|
|
231
|
+
field: {
|
|
232
|
+
name: 'relationTo',
|
|
233
|
+
type: 'text'
|
|
234
|
+
},
|
|
235
|
+
isUUID: false
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
if (pathPlan.type === 'hasManySelect') {
|
|
240
|
+
const fieldContext = [
|
|
241
|
+
...pathPlan.fieldsByCollection.values()
|
|
242
|
+
].find((field)=>field.type === 'hasManySelect')?.field;
|
|
243
|
+
if (!fieldContext) {
|
|
244
|
+
return {
|
|
245
|
+
type: 'invalid',
|
|
246
|
+
pathPlan
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
type: 'hasManySelect',
|
|
251
|
+
fieldContext,
|
|
252
|
+
jsonExpression: getHasManySelectExpression({
|
|
253
|
+
adapter,
|
|
254
|
+
collection,
|
|
255
|
+
pathPlan,
|
|
256
|
+
table
|
|
257
|
+
}),
|
|
258
|
+
pathPlan
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const collectionField = pathPlan.fieldsByCollection.get(collection);
|
|
262
|
+
const fieldContext = collectionField?.type === 'scalar' ? collectionField : undefined;
|
|
263
|
+
const queryValueContext = fieldContext ?? [
|
|
264
|
+
...pathPlan.fieldsByCollection.values()
|
|
265
|
+
].find((field)=>field.type === 'scalar');
|
|
266
|
+
if (!queryValueContext) {
|
|
267
|
+
return {
|
|
268
|
+
type: 'invalid',
|
|
269
|
+
pathPlan
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
type: 'scalar',
|
|
274
|
+
column: fieldContext ? table[pathPlan.columnPath] : sql`null`,
|
|
275
|
+
fieldContext,
|
|
276
|
+
pathPlan,
|
|
277
|
+
queryValueContext
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const getHasManySelectExpression = ({ adapter, collection, pathPlan, table })=>{
|
|
281
|
+
if (pathPlan.fieldsByCollection.get(collection)?.type !== 'hasManySelect') {
|
|
282
|
+
return adapter.name === 'postgres' ? `'[]'::jsonb` : `'[]'`;
|
|
283
|
+
}
|
|
284
|
+
const selectTablePath = pathPlan.schemaPath.split('.').map((pathSegment)=>toSnakeCase(pathSegment)).join('_');
|
|
285
|
+
const valuesTableName = adapter.tableNameMap.get(`${toSnakeCase(collection)}_${selectTablePath}`);
|
|
286
|
+
if (!valuesTableName) {
|
|
287
|
+
throw new Error(`Polymorphic join collection "${collection}" has no value table for "${pathPlan.schemaPath}"`);
|
|
288
|
+
}
|
|
289
|
+
let qualifiedValuesTableName = valuesTableName;
|
|
290
|
+
let parentTableName = getTableName(table);
|
|
291
|
+
if (adapter.schemaName) {
|
|
292
|
+
qualifiedValuesTableName = `"${adapter.schemaName}"."${valuesTableName}"`;
|
|
293
|
+
parentTableName = `"${adapter.schemaName}"."${parentTableName}"`;
|
|
294
|
+
}
|
|
295
|
+
if (adapter.name === 'postgres') {
|
|
296
|
+
return `(select coalesce(jsonb_agg(${qualifiedValuesTableName}.value), '[]'::jsonb) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
297
|
+
}
|
|
298
|
+
return `(select json_group_array(${qualifiedValuesTableName}.value) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
//# sourceMappingURL=buildPolymorphicJoinWhere.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/find/buildPolymorphicJoinWhere.ts"],"sourcesContent":["import type { Column, SQL } from 'drizzle-orm'\n\nimport { and, getTableName, isNotNull, isNull, or, sql } from 'drizzle-orm'\nimport { type FlattenedField, type Operator, QueryError, type Where } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleResolvedOperator } from '../queries/operatorMap.js'\nimport type { DrizzleAdapter, GenericTable } from '../types.js'\nimport type {\n PolymorphicJoinWherePlan,\n ScalarWhereField,\n WherePathPlan,\n} from './createPolymorphicJoinWherePlan.js'\n\nimport { buildOperatorConstraint } from '../queries/buildOperatorConstraint.js'\nimport { sanitizeQueryValue } from '../queries/sanitizeQueryValue.js'\n\nconst supportedJSONQueryOperators = new Set(['contains', 'equals', 'exists', 'in', 'like'])\n\ntype BuildPolymorphicJoinWhereArgs = {\n adapter: DrizzleAdapter\n collection: string\n locale?: string\n table: GenericTable\n where: Where\n wherePlan: PolymorphicJoinWherePlan\n}\n\ntype ResolvedWherePath =\n | {\n column: Column | SQL\n fieldContext?: ScalarWhereField\n pathPlan: WherePathPlan\n queryValueContext: ScalarWhereField\n type: 'scalar'\n }\n | {\n fieldContext: FlattenedField\n jsonExpression: string\n pathPlan: WherePathPlan\n type: 'hasManySelect'\n }\n | {\n pathPlan: WherePathPlan\n type: 'invalid'\n }\n\n/**\n * Builds a where clause for one collection before the polymorphic branches are combined. A field\n * that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current\n * collection slug.\n */\nexport const buildPolymorphicJoinWhere = ({\n adapter,\n collection,\n locale,\n table,\n where,\n wherePlan,\n}: BuildPolymorphicJoinWhereArgs): SQL | undefined => {\n const constraints: SQL[] = []\n\n for (const key in where) {\n if (['AND', 'OR'].includes(key.toUpperCase())) {\n if (!Array.isArray(where[key])) {\n throw new QueryError([{ path: key }])\n }\n\n const combine = key.toUpperCase() === 'AND' ? and : or\n const nestedConstraints = where[key].map((nestedWhere) =>\n buildPolymorphicJoinWhere({\n adapter,\n collection,\n locale,\n table,\n where: nestedWhere,\n wherePlan,\n }),\n )\n const nestedCondition = combine(...nestedConstraints)\n\n if (nestedCondition) {\n constraints.push(nestedCondition)\n }\n\n continue\n }\n\n const resolvedPath = resolveWherePath({\n adapter,\n collection,\n schemaPath: key,\n table,\n wherePlan,\n })\n\n for (let payloadOperator of Object.keys(where[key])) {\n const originalOperator = payloadOperator as Operator\n let value = where[key][payloadOperator]\n\n if (resolvedPath.type === 'invalid') {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n if (resolvedPath.type === 'hasManySelect') {\n if (!supportedJSONQueryOperators.has(payloadOperator)) {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n const sanitizedQueryValue = sanitizeQueryValue({\n adapter,\n field: resolvedPath.fieldContext,\n isUUID: false,\n operator: payloadOperator,\n relationOrPath: key,\n val: value,\n })\n\n if (sanitizedQueryValue === null) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n if (sanitizedQueryValue.columns) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n value = sanitizedQueryValue.value\n\n if (payloadOperator === 'in' && Array.isArray(value) && value.length === 0) {\n constraints.push(sql.raw('false'))\n continue\n }\n\n const jsonQueryOperator =\n payloadOperator === 'exists' ? 'exists' : sanitizedQueryValue.operator\n\n constraints.push(\n sql.raw(\n adapter.createJSONQuery({\n column: resolvedPath.jsonExpression,\n operator: jsonQueryOperator,\n pathSegments: [resolvedPath.pathPlan.columnPath],\n value,\n }),\n ),\n )\n continue\n }\n\n if (payloadOperator === '$raw') {\n if (typeof value !== 'string') {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n constraints.push(sql.raw(value))\n continue\n }\n\n const { column, fieldContext, queryValueContext } = resolvedPath\n\n if (\n payloadOperator === 'like' &&\n (queryValueContext.field.type === 'number' ||\n queryValueContext.field.type === 'relationship' ||\n queryValueContext.field.type === 'upload' ||\n queryValueContext.isUUID)\n ) {\n payloadOperator = 'equals'\n }\n\n const sanitizedQueryValue = sanitizeQueryValue({\n adapter,\n field: queryValueContext.field,\n isUUID: queryValueContext.isUUID,\n operator: payloadOperator,\n relationOrPath: key,\n val: value,\n })\n\n if (sanitizedQueryValue === null) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n if (sanitizedQueryValue.columns) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n payloadOperator = sanitizedQueryValue.operator\n value = sanitizedQueryValue.value\n\n if (!(payloadOperator in adapter.operators)) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n const resolvedOperator = payloadOperator as DrizzleResolvedOperator\n const operator = adapter.operators[resolvedOperator]\n const buildColumnConstraint = (constraintValue: unknown) => {\n if (!fieldContext) {\n return operator(column, constraintValue)\n }\n\n return buildOperatorConstraint({\n adapter,\n column,\n field: fieldContext.field,\n locale,\n originalOperator,\n path: key,\n resolvedOperator,\n value: constraintValue,\n })\n }\n\n if (originalOperator === 'equals' && value === null) {\n constraints.push(isNull(column))\n continue\n }\n\n if (originalOperator === 'not_equals') {\n if (value === null) {\n constraints.push(isNotNull(column))\n } else {\n const notEqualsConstraint = or(isNull(column), buildColumnConstraint(value))\n\n if (notEqualsConstraint) {\n constraints.push(notEqualsConstraint)\n }\n }\n continue\n }\n\n if (originalOperator === 'in' && Array.isArray(value) && value.includes(null)) {\n const nonNullValues = value.filter((item) => item !== null)\n const inConstraint = buildColumnConstraint(nonNullValues)\n const nullAwareInConstraint = or(isNull(column), inConstraint)\n\n if (nullAwareInConstraint) {\n constraints.push(nullAwareInConstraint)\n }\n continue\n }\n\n if (payloadOperator === 'like' && typeof value === 'string') {\n const wordConstraints = value.split(' ').map((word) => buildColumnConstraint(`%${word}%`))\n const wordCondition = and(...wordConstraints)\n\n if (wordCondition) {\n constraints.push(wordCondition)\n }\n continue\n }\n\n constraints.push(buildColumnConstraint(value))\n }\n }\n\n return and(...constraints)\n}\n\nconst resolveWherePath = ({\n adapter,\n collection,\n schemaPath,\n table,\n wherePlan,\n}: {\n schemaPath: string\n} & Omit<BuildPolymorphicJoinWhereArgs, 'locale' | 'where'>): ResolvedWherePath => {\n const pathPlan = wherePlan.get(schemaPath)\n\n if (!pathPlan) {\n throw new QueryError([{ path: schemaPath }])\n }\n\n if (pathPlan.type === 'invalid') {\n return { type: 'invalid', pathPlan }\n }\n\n if (schemaPath === 'relationTo') {\n return {\n type: 'scalar',\n column: sql`${collection}`,\n pathPlan,\n queryValueContext: {\n type: 'scalar',\n field: { name: 'relationTo', type: 'text' },\n isUUID: false,\n },\n }\n }\n\n if (pathPlan.type === 'hasManySelect') {\n const fieldContext = [...pathPlan.fieldsByCollection.values()].find(\n (field) => field.type === 'hasManySelect',\n )?.field\n\n if (!fieldContext) {\n return { type: 'invalid', pathPlan }\n }\n\n return {\n type: 'hasManySelect',\n fieldContext,\n jsonExpression: getHasManySelectExpression({\n adapter,\n collection,\n pathPlan,\n table,\n }),\n pathPlan,\n }\n }\n\n const collectionField = pathPlan.fieldsByCollection.get(collection)\n const fieldContext = collectionField?.type === 'scalar' ? collectionField : undefined\n const queryValueContext =\n fieldContext ??\n [...pathPlan.fieldsByCollection.values()].find(\n (field): field is ScalarWhereField => field.type === 'scalar',\n )\n\n if (!queryValueContext) {\n return { type: 'invalid', pathPlan }\n }\n\n return {\n type: 'scalar',\n column: fieldContext ? table[pathPlan.columnPath] : sql`null`,\n fieldContext,\n pathPlan,\n queryValueContext,\n }\n}\n\nconst getHasManySelectExpression = ({\n adapter,\n collection,\n pathPlan,\n table,\n}: {\n adapter: DrizzleAdapter\n collection: string\n pathPlan: WherePathPlan\n table: GenericTable\n}): string => {\n if (pathPlan.fieldsByCollection.get(collection)?.type !== 'hasManySelect') {\n return adapter.name === 'postgres' ? `'[]'::jsonb` : `'[]'`\n }\n\n const selectTablePath = pathPlan.schemaPath\n .split('.')\n .map((pathSegment) => toSnakeCase(pathSegment))\n .join('_')\n const valuesTableName = adapter.tableNameMap.get(`${toSnakeCase(collection)}_${selectTablePath}`)\n\n if (!valuesTableName) {\n throw new Error(\n `Polymorphic join collection \"${collection}\" has no value table for \"${pathPlan.schemaPath}\"`,\n )\n }\n\n let qualifiedValuesTableName = valuesTableName\n let parentTableName = getTableName(table)\n\n if (adapter.schemaName) {\n qualifiedValuesTableName = `\"${adapter.schemaName}\".\"${valuesTableName}\"`\n parentTableName = `\"${adapter.schemaName}\".\"${parentTableName}\"`\n }\n\n if (adapter.name === 'postgres') {\n return `(select coalesce(jsonb_agg(${qualifiedValuesTableName}.value), '[]'::jsonb) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`\n }\n\n return `(select json_group_array(${qualifiedValuesTableName}.value) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`\n}\n"],"names":["and","getTableName","isNotNull","isNull","or","sql","QueryError","toSnakeCase","buildOperatorConstraint","sanitizeQueryValue","supportedJSONQueryOperators","Set","buildPolymorphicJoinWhere","adapter","collection","locale","table","where","wherePlan","constraints","key","includes","toUpperCase","Array","isArray","path","combine","nestedConstraints","map","nestedWhere","nestedCondition","push","resolvedPath","resolveWherePath","schemaPath","payloadOperator","Object","keys","originalOperator","value","type","has","sanitizedQueryValue","field","fieldContext","isUUID","operator","relationOrPath","val","columns","length","raw","jsonQueryOperator","createJSONQuery","column","jsonExpression","pathSegments","pathPlan","columnPath","queryValueContext","operators","resolvedOperator","buildColumnConstraint","constraintValue","notEqualsConstraint","nonNullValues","filter","item","inConstraint","nullAwareInConstraint","wordConstraints","split","word","wordCondition","get","name","fieldsByCollection","values","find","getHasManySelectExpression","collectionField","undefined","selectTablePath","pathSegment","join","valuesTableName","tableNameMap","Error","qualifiedValuesTableName","parentTableName","schemaName"],"mappings":"AAEA,SAASA,GAAG,EAAEC,YAAY,EAAEC,SAAS,EAAEC,MAAM,EAAEC,EAAE,EAAEC,GAAG,QAAQ,cAAa;AAC3E,SAA6CC,UA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