Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
💥 Update where pipe with filters
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Jan 9, 2021
1 parent a16a1d3 commit 4f70e60
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 15 deletions.
24 changes: 24 additions & 0 deletions src/pipes/cursor-slug.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
import { CURSOR_PIPE_FORMAT } from '../errors/errors.constants';
import { parseObjectLiteral } from '../helpers/parse-object-literal';

/** Convert a string like "slug: "ok", name: 'Anand'" to { slug: "ok", name: "Anand" } */
@Injectable()
export class CursorSlugPipe implements PipeTransform {
transform(value: string): Record<string, string> | undefined {
if (value == null) return undefined;
if (!value.includes(':')) value = `slug:${value}`;
try {
const rules = parseObjectLiteral(value);
const items: Record<string, string> = {};
rules.forEach((rule) => {
const num = String(rule[1]);
if (!num) items[rule[0]] = num;
else if (rule[1]) items[rule[0]] = rule[1];
});
return items;
} catch (_) {
throw new BadRequestException(CURSOR_PIPE_FORMAT);
}
}
}
70 changes: 55 additions & 15 deletions src/pipes/where.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,70 @@
import {
ArgumentMetadata,
BadRequestException,
Injectable,
PipeTransform,
} from '@nestjs/common';
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
import { WHERE_PIPE_FORMAT } from '../errors/errors.constants';
import { parseObjectLiteral } from '../helpers/parse-object-literal';

/** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
@Injectable()
export class WherePipe implements PipeTransform {
transform(
value: string,
metadata: ArgumentMetadata,
): Record<string, number | string> | undefined {
transform(value: string): Record<string, any> | undefined {
if (value == null) return undefined;
try {
const rules = parseObjectLiteral(value);
const items: Record<string, number | string> = {};
const items: Record<string, any> = {};
rules.forEach((rule) => {
const num = Number(rule[1]);
if (!isNaN(num)) items[rule[0]] = num;
else if (rule[1]) items[rule[0]] = rule[1];
const ruleKey = rule[0];
let ruleValue: any = rule[1];
if (ruleValue.endsWith(')')) {
if (ruleValue.startsWith('int('))
ruleValue = parseInt(/\(([^)]+)\)/.exec(ruleValue)[1]);
else if (
ruleValue.startsWith('date(') ||
ruleValue.startsWith('datetime(')
)
ruleValue = new Date(
/\(([^)]+)\)/.exec(ruleValue)[1],
).toISOString();
else if (ruleValue.startsWith('float('))
ruleValue = parseFloat(/\(([^)]+)\)/.exec(ruleValue)[1]);
else if (ruleValue.startsWith('string('))
ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1];
else if (
ruleValue.startsWith('boolean(') ||
ruleValue.startsWith('bool(')
)
ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1] === 'true';
}
[
'lt',
'lte',
'gt',
'gte',
'equals',
'not',
'contains',
'startsWith',
'endsWith',
'every',
'some',
'none',
].forEach((val) => {
if (rule[1].startsWith(`${val} `)) {
const data: Record<string, any> = {};
data[val] = ruleValue.replace(`${val} `, '');
if (data[val].includes(':') && !data[val].endsWith(':')) {
const record: Record<string, any> = {};
record[data[val].split(':')[0].trim()] = data[val]
.split(':')[1]
.trim();
data[val] = record;
}
items[ruleKey] = data;
}
});
if (ruleValue != null && ruleValue !== '')
items[ruleKey] = items[ruleKey] || ruleValue;
});
return items;
} catch (_) {
} catch (error) {
throw new BadRequestException(WHERE_PIPE_FORMAT);
}
}
Expand Down

0 comments on commit 4f70e60

Please sign in to comment.