Skip to content

Commit

Permalink
fix: ensures tquerying by relationship subpaths works
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Nov 1, 2021
1 parent 40b33d9 commit 37b21b0
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/mongoose/buildQuery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable no-restricted-syntax */
import deepmerge from 'deepmerge';
import mongoose, { FilterQuery } from 'mongoose';
import mongoose, { FilterQuery, SchemaType } from 'mongoose';
import { combineMerge } from '../utilities/combineMerge';
import { CollectionModel } from '../collections/config/types';
import { getSchemaTypeOptions } from './getSchemaTypeOptions';
Expand Down Expand Up @@ -97,12 +97,12 @@ class ParamParser {
if (validOperators.includes(operator)) {
const searchParam = await this.buildSearchParam(this.model.schema, relationOrPath, pathOperators[operator], operator);

if ('path' in searchParam) {
if (searchParam && 'path' in searchParam) {
result = {
...result,
[searchParam.path]: searchParam.value,
};
} else if (typeof searchParam.value === 'object') {
} else if (typeof searchParam?.value === 'object') {
result = deepmerge(result, searchParam.value, { arrayMerge: combineMerge });
}

Expand Down Expand Up @@ -149,9 +149,12 @@ class ParamParser {
const { path } = lastIncompletePath;

const currentPath = path ? `${path}.${segment}` : segment;
const currentSchemaType = schema.path(currentPath);
const currentSchemaType: SchemaType & { path: string } = schema.path(currentPath);

if (currentSchemaType) {
// If we find a schema type, and it matches the exact current path
// NOTE - not a sub-path. Some schema types like `mixed` will return anything
// nested within. Need to make sure that schema type path matches exactly
if (currentSchemaType && (currentSchemaType.path === currentPath || currentSchemaType.instance === 'Embedded')) {
const currentSchemaTypeOptions = getSchemaTypeOptions(currentSchemaType);

if (currentSchemaTypeOptions.localized) {
Expand Down Expand Up @@ -281,7 +284,6 @@ class ParamParser {
let overrideQuery = false;
let query;


// If there is a ref, this is a relationship or upload field
// IDs can be either string, number, or ObjectID
// So we need to build an `or` query for all these types
Expand Down

0 comments on commit 37b21b0

Please sign in to comment.