payload
Version:
Node, React, Headless CMS and Application Framework built on Next.js
54 lines (53 loc) • 1.73 kB
JavaScript
/**
* Transforms a basic "where" query into a format in which the "where builder" can understand.
* Even though basic queries are valid, we need to hoist them into the "and" / "or" format.
* Use this function alongside `validateWhereQuery` to check that for valid queries before transforming.
* @example
* Inaccurate: [text][equals]=example%20post
* Accurate: [or][0][and][0][text][equals]=example%20post
*/ export const transformWhereQuery = (whereQuery)=>{
if (!whereQuery) {
return {};
}
// Check if 'whereQuery' has 'or' field but no 'and'. This is the case for "correct" queries
if (whereQuery.or && !whereQuery.and) {
return {
or: whereQuery.or.map((query)=>{
// ...but if the or query does not have an and, we need to add it
if (!query.and) {
return {
and: [
query
]
};
}
return query;
})
};
}
// Check if 'whereQuery' has 'and' field but no 'or'.
if (whereQuery.and && !whereQuery.or) {
return {
or: [
{
and: whereQuery.and
}
]
};
}
// Check if 'whereQuery' has neither 'or' nor 'and'.
if (!whereQuery.or && !whereQuery.and) {
return {
or: [
{
and: [
whereQuery
]
}
]
};
}
// If 'whereQuery' has 'or' and 'and', just return it as it is.
return whereQuery;
};
//# sourceMappingURL=transformWhereQuery.js.map