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

Commit 4f70e60

Browse files
💥 Update where pipe with filters
1 parent a16a1d3 commit 4f70e60

File tree

2 files changed

+79
-15
lines changed

2 files changed

+79
-15
lines changed

src/pipes/cursor-slug.pipe.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
2+
import { CURSOR_PIPE_FORMAT } from '../errors/errors.constants';
3+
import { parseObjectLiteral } from '../helpers/parse-object-literal';
4+
5+
/** Convert a string like "slug: "ok", name: 'Anand'" to { slug: "ok", name: "Anand" } */
6+
@Injectable()
7+
export class CursorSlugPipe implements PipeTransform {
8+
transform(value: string): Record<string, string> | undefined {
9+
if (value == null) return undefined;
10+
if (!value.includes(':')) value = `slug:${value}`;
11+
try {
12+
const rules = parseObjectLiteral(value);
13+
const items: Record<string, string> = {};
14+
rules.forEach((rule) => {
15+
const num = String(rule[1]);
16+
if (!num) items[rule[0]] = num;
17+
else if (rule[1]) items[rule[0]] = rule[1];
18+
});
19+
return items;
20+
} catch (_) {
21+
throw new BadRequestException(CURSOR_PIPE_FORMAT);
22+
}
23+
}
24+
}

src/pipes/where.pipe.ts

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,70 @@
1-
import {
2-
ArgumentMetadata,
3-
BadRequestException,
4-
Injectable,
5-
PipeTransform,
6-
} from '@nestjs/common';
1+
import { BadRequestException, Injectable, PipeTransform } from '@nestjs/common';
72
import { WHERE_PIPE_FORMAT } from '../errors/errors.constants';
83
import { parseObjectLiteral } from '../helpers/parse-object-literal';
94

105
/** Convert a string like "id: 12, b: 'Anand'" to { id: 12, name: "Anand" } */
116
@Injectable()
127
export class WherePipe implements PipeTransform {
13-
transform(
14-
value: string,
15-
metadata: ArgumentMetadata,
16-
): Record<string, number | string> | undefined {
8+
transform(value: string): Record<string, any> | undefined {
179
if (value == null) return undefined;
1810
try {
1911
const rules = parseObjectLiteral(value);
20-
const items: Record<string, number | string> = {};
12+
const items: Record<string, any> = {};
2113
rules.forEach((rule) => {
22-
const num = Number(rule[1]);
23-
if (!isNaN(num)) items[rule[0]] = num;
24-
else if (rule[1]) items[rule[0]] = rule[1];
14+
const ruleKey = rule[0];
15+
let ruleValue: any = rule[1];
16+
if (ruleValue.endsWith(')')) {
17+
if (ruleValue.startsWith('int('))
18+
ruleValue = parseInt(/\(([^)]+)\)/.exec(ruleValue)[1]);
19+
else if (
20+
ruleValue.startsWith('date(') ||
21+
ruleValue.startsWith('datetime(')
22+
)
23+
ruleValue = new Date(
24+
/\(([^)]+)\)/.exec(ruleValue)[1],
25+
).toISOString();
26+
else if (ruleValue.startsWith('float('))
27+
ruleValue = parseFloat(/\(([^)]+)\)/.exec(ruleValue)[1]);
28+
else if (ruleValue.startsWith('string('))
29+
ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1];
30+
else if (
31+
ruleValue.startsWith('boolean(') ||
32+
ruleValue.startsWith('bool(')
33+
)
34+
ruleValue = /\(([^)]+)\)/.exec(ruleValue)[1] === 'true';
35+
}
36+
[
37+
'lt',
38+
'lte',
39+
'gt',
40+
'gte',
41+
'equals',
42+
'not',
43+
'contains',
44+
'startsWith',
45+
'endsWith',
46+
'every',
47+
'some',
48+
'none',
49+
].forEach((val) => {
50+
if (rule[1].startsWith(`${val} `)) {
51+
const data: Record<string, any> = {};
52+
data[val] = ruleValue.replace(`${val} `, '');
53+
if (data[val].includes(':') && !data[val].endsWith(':')) {
54+
const record: Record<string, any> = {};
55+
record[data[val].split(':')[0].trim()] = data[val]
56+
.split(':')[1]
57+
.trim();
58+
data[val] = record;
59+
}
60+
items[ruleKey] = data;
61+
}
62+
});
63+
if (ruleValue != null && ruleValue !== '')
64+
items[ruleKey] = items[ruleKey] || ruleValue;
2565
});
2666
return items;
27-
} catch (_) {
67+
} catch (error) {
2868
throw new BadRequestException(WHERE_PIPE_FORMAT);
2969
}
3070
}

0 commit comments

Comments
 (0)