Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 26 additions & 39 deletions src/baseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,20 +124,16 @@ export class BaseService<T> implements OnInit, IBaseService {
};

protected modeToTypeMappers = {
EM: (prismaFilters: any, value: any, fieldName: string, fieldInfo: any, isRelation: boolean) => {
if (fieldInfo.type === 'Int' && !fieldInfo.isRequired) {
_.set(prismaFilters, `${fieldName}`, null);
}

if (fieldInfo.type === 'String' && !fieldInfo.isRequired) {
_.set(prismaFilters, `${fieldName}`, null);
EM: ({prismaFilters, value, fieldName, fieldInfo, isRelation,nestedFieldPath}) => {
if ((fieldInfo?.type === 'Int' || fieldInfo?.type === 'String' || nestedFieldPath) && !fieldInfo?.isRequired) {
_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:fieldName}`, null);
}
},
EQ: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
if (fieldInfo.type === 'Int') {
EQ: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if (fieldInfo?.type === 'Int') {
_.set(prismaFilters, `${propertyName}.equals`, value);
}
if (fieldInfo.type === 'DateTime') {
if (fieldInfo?.type === 'DateTime') {
_.set(prismaFilters, `${propertyName}.equals`, value);
}
if (_.isArray(value)) {
Expand All @@ -147,15 +143,15 @@ export class BaseService<T> implements OnInit, IBaseService {
_.set(prismaFilters, `${propertyName}.in`, value);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a support for array value, keep in mind we don't have fieldInfo however we can know type of value and based on that we can manage prismafilters

}
if (fieldInfo.type === 'String') {
_.set(prismaFilters, `${propertyName}.contains`, value);
if (fieldInfo?.type === 'String' || nestedFieldPath) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can move it to it's independent if block

_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:propertyName}.contains`, value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same in independent if block

}
},
EX: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
EX: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if (_.isNumber(value)) {
_.set(prismaFilters, `${propertyName}.not.equals`, value);
}
if (fieldInfo.type === 'DateTime') {
if (fieldInfo?.type === 'DateTime') {
_.set(prismaFilters, `${propertyName}.not.equals`, value);
}
if (_.isArray(value)) {
Expand All @@ -165,52 +161,43 @@ export class BaseService<T> implements OnInit, IBaseService {
_.set(prismaFilters, `${propertyName}.not.in`, value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

}
}
if (_.isString(value)) {
_.set(prismaFilters, `${propertyName}.not.contains`, value);
if (_.isString(value) || nestedFieldPath) {
_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:propertyName}.not.contains`, value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seperate block

}
},
LT: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
if (_.isNumber(value)) {
_.set(prismaFilters, `${propertyName}.lt`, value);
}
if (fieldInfo.type === 'DateTime') {
LT: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if (_.isNumber(value) || fieldInfo?.type === 'DateTime' || nestedFieldPath) {
_.set(prismaFilters, `${propertyName}.lt`, value);
}
},
GT: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
if (_.isNumber(value)) {
_.set(prismaFilters, `${propertyName}.gt`, value);
}
if (fieldInfo.type === 'DateTime') {
GT: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if (_.isNumber(value) || fieldInfo?.type === 'DateTime' || nestedFieldPath) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well

_.set(prismaFilters, `${propertyName}.gt`, value);
}
},
NEM: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
if (fieldInfo.type === 'Int' && !fieldInfo.isRequired) {
_.set(prismaFilters, `${propertyName}.not`, null);
}
if (fieldInfo.type === 'String' && !fieldInfo.isRequired) {
_.set(prismaFilters, `${propertyName}.not`, null);
NEM: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if ((fieldInfo?.type === 'Int' || fieldInfo?.type === 'String' || nestedFieldPath) && !fieldInfo?.isRequired) {
_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:propertyName}.not`, null);
}
if (fieldInfo.type === 'DateTime') {
if (fieldInfo?.type === 'DateTime') {
_.set(prismaFilters, `${propertyName}.not`, null);
}
},
RG: (prismaFilters: any, value: any, propertyName: string, fieldInfo: any, isRelation: boolean) => {
RG: ({ prismaFilters, value, propertyName, fieldInfo, isRelation, nestedFieldPath }) => {
if (_.isArray(value)) {
const [startValue, endValue] = value
_.set(prismaFilters, `${propertyName}.lte`, endValue);
_.set(prismaFilters, `${propertyName}.gte`, startValue);
_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:propertyName}.lte`, endValue);
_.set(prismaFilters, `${nestedFieldPath?nestedFieldPath:propertyName}.gte`, startValue);
}
},
}

protected modeToFilter(filters?: Record<string, { mode: string; value: any, isRelation?: boolean }>) {
protected modeToFilter(filters?: Record<string, { mode: string; value: any, isRelation?: boolean,nestedFieldPath?:string }>) {
return _.transform(
filters ?? {},
(finalFilters: any, filter, fieldName) => {
const { mode, value, isRelation } = filter;
this.modeToTypeMappers[mode](finalFilters, value, fieldName, this.currentModelFieldsMapping[fieldName], isRelation);
const { mode, value, isRelation,nestedFieldPath } = filter;
this.modeToTypeMappers[mode]({ prismaFilters: finalFilters, value, propertyName: fieldName, fieldInfo: this.currentModelFieldsMapping[fieldName], isRelation, nestedFieldPath });
},
{}
);
Expand Down