Skip to content

Commit 38973f8

Browse files
new way to send filters, removed include
1 parent 2a16877 commit 38973f8

File tree

5 files changed

+48
-44
lines changed

5 files changed

+48
-44
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@januscaler/tsed-helper",
3-
"version": "2.2.2",
3+
"version": "2.2.3",
44
"type": "module",
55
"publishConfig": {
66
"@januscaler:registry": "https://npm.pkg.github.com"
@@ -36,9 +36,10 @@
3636
"@tsed/di": "8.15.0",
3737
"@tsed/prisma": "8.15.0",
3838
"@tsed/schema": "8.15.0",
39+
"@types/lodash": "^4.17.20",
3940
"aigle": "1.14.1",
4041
"glob": "11.0.3",
4142
"lodash": "4.17.21",
4243
"rxjs": "7.8.2"
4344
}
44-
}
45+
}

src/baseCrud.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useDecorators } from '@tsed/core';
22
import { Delete, Get, inject, Post, Put, } from '@tsed/common';
3-
import { Any, CollectionOf, Default, Property, Returns, Summary } from '@tsed/schema';
3+
import { Any, CollectionOf, Default, Example, Property, Returns, Summary } from '@tsed/schema';
44
import _ from 'lodash';
5+
import { SearchFilterRecord } from './types.js';
56

67
function nameWithoutModel(model: any): string {
78
return _.replace(model.name, 'Model', '');
@@ -75,18 +76,9 @@ export interface GetItems {
7576
export class SearchParams {
7677
@Default(10) limit?: number;
7778
@Default(0) offset?: number;
78-
@CollectionOf(Object) orderBy?: Record<string, 'asc' | 'desc'>;
79-
@CollectionOf(String) fields?: string[];
80-
@Any() @Default({}) filters?: Record<
81-
string,
82-
{
83-
mode: string;
84-
value: any;
85-
isRelation?: boolean;
86-
}
87-
>;
88-
@Any()
89-
include?: any;
79+
@Default({ id: 'asc' }) @CollectionOf(Object) orderBy?: Record<string, 'asc' | 'desc'>;
80+
@Default(['name', 'roles.name']) @CollectionOf(String) fields?: string[];
81+
@Default({ name: { mode: 'EQ', value: 'test', isRelation: false } }) @CollectionOf(Object) filters?: SearchFilterRecord[];
9082
}
9183

9284

src/baseService.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SearchParams } from './baseCrud.js';
44
import { Subject } from 'rxjs';
55
import { PrismaMapperEntity, PrismaMapperEntityField, PrismaMetaMapper } from './prismaMetaMapper.js'
66
import { Generics } from '@tsed/schema';
7+
import { SearchFilterRecord, SearchFilterValue } from './types.js';
78

89
export interface IBaseService {
910
onUpdate: Subject<{ id: number, inputData: any, result: any }>
@@ -129,7 +130,7 @@ export class BaseService<T> implements OnInit, IBaseService {
129130
RG: 'RG'
130131
};
131132

132-
protected modeToTypeMappers = {
133+
protected modeToTypeMappers: Record<string, (prismaFilters: any, value: any, fieldName: string, fieldInfo: any, isRelation: boolean) => void> = {
133134
EM: (prismaFilters: any, value: any, fieldName: string, fieldInfo: any, isRelation: boolean) => {
134135
if (fieldInfo.type === 'Int' && !fieldInfo.isRequired) {
135136
_.set(prismaFilters, `${fieldName}`, null);
@@ -211,41 +212,48 @@ export class BaseService<T> implements OnInit, IBaseService {
211212
},
212213
}
213214

214-
protected modeToFilter(filters?: Record<string, { mode: string; value: any, isRelation?: boolean }>) {
215-
return _.transform(
216-
filters ?? {},
217-
(finalFilters: any, filter, fieldName) => {
218-
const { mode, value, isRelation } = filter;
219-
this.modeToTypeMappers[mode](finalFilters, value, fieldName, this.currentModelFieldsMapping[fieldName], isRelation);
220-
},
221-
{}
222-
);
215+
protected modeToPrismaFilter(filters: SearchFilterRecord) {
216+
return _.transform<SearchFilterValue, any>(filters, (finalFilters, filter, fieldName) => {
217+
const { mode, value, isRelation } = filter;
218+
if (!_.has(this.modeToTypeMappers, mode)) {
219+
throw new Error(`Unsupported filter mode: ${mode}`);
220+
}
221+
this.modeToTypeMappers[mode](finalFilters, value, fieldName, this.currentModelFieldsMapping[fieldName], isRelation);
222+
}, {});
223+
}
224+
225+
protected filtersToPrismaOrCondition(filters: SearchFilterRecord[]) {
226+
return _.transform<SearchFilterRecord, any>(filters, (finalFilters, modeFilter) => {
227+
if (!finalFilters.OR) {
228+
finalFilters.OR = [];
229+
}
230+
const prismaFilter = this.modeToPrismaFilter(modeFilter);
231+
finalFilters.OR.push(prismaFilter);
232+
}, {});
223233
}
224234

225-
async getAll({ filters, offset, limit, fields, include, orderBy }: SearchParams) {
235+
async getAll({ filters, offset, limit, fields, orderBy }: SearchParams) {
236+
const prismaWhere = this.filtersToPrismaOrCondition(filters)
237+
const selectFields = _.transform(fields, (result, field) => {
238+
if (_.includes(field, '.')) {
239+
const [relations, relationColumnName] = _.split(field, '.')
240+
if (!result[relations]) {
241+
result[relations] = { select: {} };
242+
}
243+
result[relations].select[relationColumnName] = true;
244+
return;
245+
}
246+
result[field] = true;
247+
}, {})
226248
const properties = {
227249
skip: offset,
228250
take: limit,
229-
include: {
230-
...include,
231-
..._.transform(_.remove(fields, 'id'), (result, field) => {
232-
result[field] = true;
233-
}, {})
234-
},
235251
orderBy: orderBy,
236-
where: this.modeToFilter(filters),
237-
select: _.isNil(fields)
238-
? null
239-
: _.transform(fields, (result, field) => {
240-
result[field] = true;
241-
}, {})
252+
where: prismaWhere,
253+
select: selectFields
242254
};
243-
if (_.size(properties.include)) {
244-
delete properties.select;
245-
}
246-
247255
const { _count: { id: total } } = await this.injectedRepository.aggregate({
248-
where: this.modeToFilter(filters),
256+
where: prismaWhere,
249257
_count: {
250258
id: true,
251259
}

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './baseCrud.js'
22
export * from './baseService.js'
33
export * from './prismaMetaMapper.js'
4-
export * from './seederHelper.js'
4+
export * from './seederHelper.js'
5+
export * from './types.js'

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type SearchFilterValue = { mode: string; value: any; isRelation?: boolean };
2+
export type SearchFilterRecord = Record<string, SearchFilterValue>

0 commit comments

Comments
 (0)