Skip to content

Commit df1fec5

Browse files
author
Nico Lazarus
committed
remove 'mergeFiltersWithSameKey' function
1 parent 4bc2408 commit df1fec5

File tree

2 files changed

+7
-35
lines changed

2 files changed

+7
-35
lines changed

dist/utils.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,16 @@ const buildWhereAndInclude = (query, whitelist, idField) => {
122122
where[k] = null;
123123
}
124124
else if (k === idField) {
125-
where[k] = (0, exports.mergeFiltersWithSameKey)(where, k, (0, exports.buildIdField)(value, whitelist));
125+
where[k] = (0, exports.buildIdField)(value, whitelist);
126126
}
127127
else if (k === '$or' && Array.isArray(value)) {
128128
where.OR = value.map((v) => (0, exports.buildWhereAndInclude)(v, whitelist, idField).where);
129129
}
130130
else if (k === '$and' && Array.isArray(value)) {
131-
value.forEach((v) => {
132-
const whereValue = (0, exports.buildWhereAndInclude)(v, whitelist, idField).where;
133-
Object.keys(whereValue).map((subKey) => {
134-
where[subKey] = (0, exports.mergeFiltersWithSameKey)(where, subKey, whereValue[subKey]);
135-
});
136-
});
131+
where.AND = value.map((v) => (0, exports.buildWhereAndInclude)(v, whitelist, idField).where);
137132
}
138133
else if (k !== '$eager' && typeof value === 'object' && !Array.isArray(value)) {
139-
where[k] = (0, exports.mergeFiltersWithSameKey)(where, k, (0, exports.castFeathersQueryToPrismaFilters)(value, whitelist));
134+
where[k] = (0, exports.castFeathersQueryToPrismaFilters)(value, whitelist);
140135
}
141136
else if (k !== '$eager' && typeof value !== 'object' && !Array.isArray(value)) {
142137
where[k] = (0, exports.castToNumberBooleanStringOrNull)(value);

src/utils.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { NotFound } from '@feathersjs/errors';
21
import { OPERATORS_MAP } from './constants';
32
import { EagerQuery, FeathersQueryData, IdField, QueryParam, QueryParamRecordFilters } from './types';
43

@@ -40,7 +39,7 @@ export const castFeathersQueryToPrismaFilters = (p: QueryParamRecordFilters, whi
4039

4140
export const castEagerQueryToPrismaInclude = (value: EagerQuery, whitelist: string[], idField: string) => {
4241
// we don't care about feathers compliance, we want where queries in our include
43-
// thus just returnung the $eager value as include 1:1
42+
// thus just returning the $eager value as include 1:1
4443
return value;
4544
// const include: Record<string, any> = {};
4645
// if (Array.isArray(value)) {
@@ -82,23 +81,6 @@ export const castEagerQueryToPrismaInclude = (value: EagerQuery, whitelist: stri
8281
// return include;
8382
};
8483

85-
export const mergeFiltersWithSameKey = (
86-
where: Record<string, any>,
87-
key: string,
88-
filter: Record<string, any> | string | number | boolean | null,
89-
): Record<string, any> | string | number | boolean => {
90-
const current = where[key];
91-
if (typeof filter === 'object') {
92-
const currentIsObj = typeof current === 'object';
93-
return {
94-
...(currentIsObj ? current : {}),
95-
...filter,
96-
...(!currentIsObj && current ? { equals: current } : {})
97-
};
98-
}
99-
return filter;
100-
};
101-
10284
/**
10385
* WARN: This method is not safe for Feathers queries because unwanted queries can reach the Prisma-Client.
10486
**/
@@ -125,18 +107,13 @@ export const buildWhereAndInclude = (query: QueryParam, whitelist: string[], idF
125107
if (value === null) {
126108
where[k] = null;
127109
} else if (k === idField) {
128-
where[k] = mergeFiltersWithSameKey(where, k, buildIdField(value, whitelist));
110+
where[k] = buildIdField(value, whitelist);
129111
} else if (k === '$or' && Array.isArray(value)) {
130112
where.OR = value.map((v) => buildWhereAndInclude(v, whitelist, idField).where);
131113
} else if (k === '$and' && Array.isArray(value)) {
132-
value.forEach((v) => {
133-
const whereValue = buildWhereAndInclude(v, whitelist, idField).where;
134-
Object.keys(whereValue).map((subKey) => {
135-
where[subKey] = mergeFiltersWithSameKey(where, subKey, whereValue[subKey]);
136-
});
137-
});
114+
where.AND = value.map((v) => buildWhereAndInclude(v, whitelist, idField).where);
138115
} else if (k !== '$eager' && typeof value === 'object' && !Array.isArray(value)) {
139-
where[k] = mergeFiltersWithSameKey(where, k, castFeathersQueryToPrismaFilters(value, whitelist));
116+
where[k] = castFeathersQueryToPrismaFilters(value, whitelist);
140117
} else if (k !== '$eager' && typeof value !== 'object' && !Array.isArray(value)) {
141118
where[k] = castToNumberBooleanStringOrNull(value);
142119
} else if (k === '$eager' && whitelist.includes(k)) {

0 commit comments

Comments
 (0)