Co-authored-by: Jacob Fletcher <jacobsfletch@gmail.com> Co-authored-by: Alessio Gravili <alessio@gravili.de> Co-authored-by: PatrikKozak <patrik@trbl.design> Co-authored-by: Lucas Blancas <lablancas@gmail.com> Co-authored-by: Stef Gootzen <37367280+stefgootzen@users.noreply.github.com> Co-authored-by: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Co-authored-by: Jessica Chowdhury <67977755+JessChowdhury@users.noreply.github.com> Co-authored-by: PatrikKozak <35232443+PatrikKozak@users.noreply.github.com> Co-authored-by: Greg Willard <Wickett06@gmail.com> Co-authored-by: James Mikrut <james@payloadcms.com> Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com> Co-authored-by: Elliot DeNolf <denolfe@gmail.com> fix: WhereBuilder component does not accept all valid Where queries (#3087) fix: passes in height to resizeOptions upload option to allow height resize (#3171)
94 lines
3.0 KiB
TypeScript
94 lines
3.0 KiB
TypeScript
/* eslint-disable no-restricted-syntax */
|
|
/* eslint-disable no-await-in-loop */
|
|
import { Operator, Where } from 'payload/types';
|
|
import { Field } from 'payload/dist/fields/config/types';
|
|
import { validOperators } from 'payload/dist/types/constants';
|
|
import { and, SQL } from 'drizzle-orm';
|
|
import { buildSearchParam } from './buildSearchParams';
|
|
import { buildAndOrConditions } from './buildAndOrConditions';
|
|
import { PostgresAdapter } from '../types';
|
|
|
|
export async function parseParams({
|
|
where,
|
|
collectionSlug,
|
|
globalSlug,
|
|
adapter,
|
|
locale,
|
|
fields,
|
|
}: {
|
|
where: Where,
|
|
collectionSlug?: string,
|
|
globalSlug?: string,
|
|
adapter: PostgresAdapter
|
|
locale: string,
|
|
fields: Field[],
|
|
}): Promise<SQL> {
|
|
let result: SQL;
|
|
|
|
if (typeof where === 'object') {
|
|
// We need to determine if the whereKey is an AND, OR, or a schema path
|
|
for (const relationOrPath of Object.keys(where)) {
|
|
const condition = where[relationOrPath];
|
|
let conditionOperator: 'and' | 'or';
|
|
if (relationOrPath.toLowerCase() === 'and') {
|
|
conditionOperator = 'and';
|
|
} else if (relationOrPath.toLowerCase() === 'or') {
|
|
conditionOperator = 'or';
|
|
}
|
|
if (Array.isArray(condition)) {
|
|
const builtConditions = await buildAndOrConditions({
|
|
collectionSlug,
|
|
fields,
|
|
globalSlug,
|
|
adapter,
|
|
locale,
|
|
where: condition,
|
|
});
|
|
|
|
if (builtConditions.length > 0) result = and(result, ...builtConditions);
|
|
} else {
|
|
// It's a path - and there can be multiple comparisons on a single path.
|
|
// For example - title like 'test' and title not equal to 'tester'
|
|
// So we need to loop on keys again here to handle each operator independently
|
|
const pathOperators = where[relationOrPath];
|
|
if (typeof pathOperators === 'object') {
|
|
for (const operator of Object.keys(pathOperators)) {
|
|
if (validOperators.includes(operator as Operator)) {
|
|
const searchParam = await buildSearchParam({
|
|
collectionSlug,
|
|
globalSlug,
|
|
adapter,
|
|
locale,
|
|
fields,
|
|
incomingPath: relationOrPath,
|
|
val: pathOperators[operator],
|
|
operator,
|
|
});
|
|
|
|
if (searchParam?.value && searchParam?.path) {
|
|
result = and(result, searchParam.value);
|
|
// result = {
|
|
// ...result,
|
|
// [searchParam.path]: searchParam.value,
|
|
// };
|
|
} else if (typeof searchParam?.value === 'object') {
|
|
result = and(result, searchParam.value);
|
|
// result = deepmerge(result, searchParam.value, { arrayMerge: combineMerge });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// await db.select().from(users).where(
|
|
// and(
|
|
// eq(users.id, 42),
|
|
// eq(users.name, 'Dan')
|
|
// )
|
|
// );
|
|
|
|
return result;
|
|
}
|