crud-query-parser 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +31 -10
- package/dist/adapters/typeorm/index.d.mts +22 -5
- package/dist/adapters/typeorm/index.d.ts +22 -5
- package/dist/adapters/typeorm/index.js +1 -1
- package/dist/adapters/typeorm/index.js.map +1 -1
- package/dist/adapters/typeorm/index.mjs +1 -1
- package/dist/adapters/typeorm/index.mjs.map +1 -1
- package/dist/{parsed-request-where.builder-DME55XmN.d.ts → crud-request-where.builder-D60W-QEd.d.ts} +6 -6
- package/dist/{parsed-request-where.builder-DRZUAxu4.d.mts → crud-request-where.builder-mP8y-fQp.d.mts} +6 -6
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/parsers/crud/index.d.mts +3 -2
- package/dist/parsers/crud/index.d.ts +3 -2
- package/dist/parsers/crud/index.js +1 -1
- package/dist/parsers/crud/index.js.map +1 -1
- package/dist/parsers/crud/index.mjs +1 -1
- package/dist/parsers/crud/index.mjs.map +1 -1
- package/package.json +17 -2
package/README.md
CHANGED
@@ -5,14 +5,9 @@ This library parses HTTP requests and converts them to TypeORM query builders, a
|
|
5
5
|
## Install
|
6
6
|
|
7
7
|
```sh
|
8
|
-
npm install
|
8
|
+
npm install crud-query-parser
|
9
9
|
```
|
10
10
|
|
11
|
-
### Other modules
|
12
|
-
|
13
|
-
- `@crud-query-parser/typeorm`: TypeORM adapter
|
14
|
-
- `@crud-query-parser/nestjs`: NestJS utilities
|
15
|
-
|
16
11
|
## Usage
|
17
12
|
|
18
13
|
You have to pick a request parser and a query adapter.
|
@@ -40,12 +35,15 @@ console.log(result);
|
|
40
35
|
|
41
36
|
### CRUD
|
42
37
|
|
43
|
-
|
38
|
+
The CRUD parser is an implementation of the `@nestjsx/crud` [query params format](https://github.com/nestjsx/crud/wiki/Requests#query-params).
|
44
39
|
|
45
40
|
```ts
|
46
41
|
import { CrudRequestParser } from 'crud-query-parser/parsers/crud';
|
47
42
|
|
48
43
|
const parser = new CrudRequestParser();
|
44
|
+
|
45
|
+
// Then, you have to pass a query string object to it
|
46
|
+
// const crudRequest = parser.parse(request.query);
|
49
47
|
```
|
50
48
|
|
51
49
|
## Database adapters
|
@@ -59,7 +57,8 @@ import { TypeormQueryAdapter } from 'crud-query-parser/adapters/typeorm';
|
|
59
57
|
|
60
58
|
const adapter = new TypeormQueryAdapter();
|
61
59
|
|
62
|
-
//
|
60
|
+
// Then, you can pass a query builder to it:
|
61
|
+
// const result = adapter.getMany(repository.createQueryBuilder(), crudRequest);
|
63
62
|
```
|
64
63
|
|
65
64
|
## Helpers
|
@@ -74,15 +73,37 @@ Sample code:
|
|
74
73
|
@Controller('users')
|
75
74
|
export class UserController {
|
76
75
|
|
76
|
+
constructor(
|
77
|
+
private service: UserService,
|
78
|
+
) {}
|
79
|
+
|
77
80
|
@Get()
|
78
|
-
@Crud(CrudRequestParser)
|
79
|
-
public async getMany(@ParseCrudRequest() crudRequest: CrudRequest) {
|
81
|
+
@Crud(CrudRequestParser) // <- You specify which parser to use
|
82
|
+
public async getMany(@ParseCrudRequest() crudRequest: CrudRequest) { // <- The request query will be automatically parsed
|
80
83
|
return this.service.getMany(crudRequest);
|
81
84
|
}
|
82
85
|
|
83
86
|
}
|
84
87
|
```
|
85
88
|
|
89
|
+
```ts
|
90
|
+
@Injectable()
|
91
|
+
export class UserService {
|
92
|
+
|
93
|
+
protected crudAdapter = new TypeormQueryAdapter();
|
94
|
+
|
95
|
+
constructor(
|
96
|
+
@InjectRepository(UserEntity)
|
97
|
+
private repository: Repository<UserEntity>,
|
98
|
+
) {}
|
99
|
+
|
100
|
+
public async getMany(crudRequest: CrudRequest) {
|
101
|
+
return await this.crudAdapter.getMany(this.repository.createQueryBuilder(), crudRequest);
|
102
|
+
}
|
103
|
+
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
86
107
|
## Filters
|
87
108
|
|
88
109
|
You may need to filter what the user can or cannot query. You can modify the `CrudRequest` object as needed.
|
@@ -2,10 +2,20 @@ import { SelectQueryBuilder, ObjectLiteral, WhereExpressionBuilder } from 'typeo
|
|
2
2
|
import { Q as QueryAdapter, G as GetManyResult } from '../../query-adapter-BliD9hJN.mjs';
|
3
3
|
import { c as CrudRequest, P as ParsedRequestSelect, a as CrudRequestRelation, b as CrudRequestOrder, d as CrudRequestWhere, g as CrudRequestWhereField } from '../../crud-request-CvDKp6Iy.mjs';
|
4
4
|
|
5
|
+
interface TypeOrmQueryAdapterOptions {
|
6
|
+
/**
|
7
|
+
* Whether it will use ILIKE for case-insensitive operations.
|
8
|
+
*
|
9
|
+
* If undefined, it will be enabled by default for postgres and aurora-postgres databases
|
10
|
+
*/
|
11
|
+
ilike?: boolean;
|
12
|
+
}
|
5
13
|
/**
|
6
14
|
* Adapts queries to TypeORM query builder object
|
7
15
|
*/
|
8
|
-
declare class
|
16
|
+
declare class TypeOrmQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {
|
17
|
+
private readonly options;
|
18
|
+
constructor(options?: TypeOrmQueryAdapterOptions);
|
9
19
|
/**
|
10
20
|
* @inheritDoc
|
11
21
|
*/
|
@@ -44,8 +54,9 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
44
54
|
*
|
45
55
|
* @param qb The query builder
|
46
56
|
* @param relations The parsed relation list
|
57
|
+
* @param select The parsed select fields
|
47
58
|
*/
|
48
|
-
protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[]): void;
|
59
|
+
protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[], select: ParsedRequestSelect): void;
|
49
60
|
/**
|
50
61
|
* Adapts the order by list
|
51
62
|
*
|
@@ -56,12 +67,14 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
56
67
|
/**
|
57
68
|
* Adapts a where condition
|
58
69
|
*
|
70
|
+
* @param alias The query builder alias
|
59
71
|
* @param qb The query builder
|
60
72
|
* @param where The quere condition
|
61
73
|
* @param or Whether this where condition is AND/OR
|
62
74
|
* @param params The registered parameter name list
|
75
|
+
* @param isILikeEnabled Whether the ILIKE operator can be used
|
63
76
|
*/
|
64
|
-
protected adaptWhere(qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[]): void;
|
77
|
+
protected adaptWhere(alias: string, qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[], isILikeEnabled: boolean): void;
|
65
78
|
/**
|
66
79
|
* Creates a query parameter name based on a field
|
67
80
|
*
|
@@ -72,13 +85,17 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
72
85
|
/**
|
73
86
|
* Maps where operators to a pseudo-SQL statement and a parameter map
|
74
87
|
*
|
88
|
+
* @param alias The query builder alias
|
75
89
|
* @param where The where condition
|
76
90
|
* @param param The parameter name
|
91
|
+
* @param isILikeEnabled Whether the ILIKE operator can be used
|
77
92
|
*/
|
78
|
-
protected mapWhereOperators(where: CrudRequestWhereField, param: string): {
|
93
|
+
protected mapWhereOperators(alias: string, where: CrudRequestWhereField, param: string, isILikeEnabled: boolean): {
|
79
94
|
where: string;
|
80
95
|
params: ObjectLiteral;
|
81
96
|
};
|
97
|
+
protected createLowerLike(isILikeEnabled: boolean, field: string, not?: boolean): string;
|
98
|
+
protected isILikeEnabled(qb: SelectQueryBuilder<any>): boolean;
|
82
99
|
}
|
83
100
|
|
84
|
-
export {
|
101
|
+
export { TypeOrmQueryAdapter, type TypeOrmQueryAdapterOptions };
|
@@ -2,10 +2,20 @@ import { SelectQueryBuilder, ObjectLiteral, WhereExpressionBuilder } from 'typeo
|
|
2
2
|
import { Q as QueryAdapter, G as GetManyResult } from '../../query-adapter-Vebxws3V.js';
|
3
3
|
import { c as CrudRequest, P as ParsedRequestSelect, a as CrudRequestRelation, b as CrudRequestOrder, d as CrudRequestWhere, g as CrudRequestWhereField } from '../../crud-request-CvDKp6Iy.js';
|
4
4
|
|
5
|
+
interface TypeOrmQueryAdapterOptions {
|
6
|
+
/**
|
7
|
+
* Whether it will use ILIKE for case-insensitive operations.
|
8
|
+
*
|
9
|
+
* If undefined, it will be enabled by default for postgres and aurora-postgres databases
|
10
|
+
*/
|
11
|
+
ilike?: boolean;
|
12
|
+
}
|
5
13
|
/**
|
6
14
|
* Adapts queries to TypeORM query builder object
|
7
15
|
*/
|
8
|
-
declare class
|
16
|
+
declare class TypeOrmQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {
|
17
|
+
private readonly options;
|
18
|
+
constructor(options?: TypeOrmQueryAdapterOptions);
|
9
19
|
/**
|
10
20
|
* @inheritDoc
|
11
21
|
*/
|
@@ -44,8 +54,9 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
44
54
|
*
|
45
55
|
* @param qb The query builder
|
46
56
|
* @param relations The parsed relation list
|
57
|
+
* @param select The parsed select fields
|
47
58
|
*/
|
48
|
-
protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[]): void;
|
59
|
+
protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[], select: ParsedRequestSelect): void;
|
49
60
|
/**
|
50
61
|
* Adapts the order by list
|
51
62
|
*
|
@@ -56,12 +67,14 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
56
67
|
/**
|
57
68
|
* Adapts a where condition
|
58
69
|
*
|
70
|
+
* @param alias The query builder alias
|
59
71
|
* @param qb The query builder
|
60
72
|
* @param where The quere condition
|
61
73
|
* @param or Whether this where condition is AND/OR
|
62
74
|
* @param params The registered parameter name list
|
75
|
+
* @param isILikeEnabled Whether the ILIKE operator can be used
|
63
76
|
*/
|
64
|
-
protected adaptWhere(qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[]): void;
|
77
|
+
protected adaptWhere(alias: string, qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[], isILikeEnabled: boolean): void;
|
65
78
|
/**
|
66
79
|
* Creates a query parameter name based on a field
|
67
80
|
*
|
@@ -72,13 +85,17 @@ declare class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any
|
|
72
85
|
/**
|
73
86
|
* Maps where operators to a pseudo-SQL statement and a parameter map
|
74
87
|
*
|
88
|
+
* @param alias The query builder alias
|
75
89
|
* @param where The where condition
|
76
90
|
* @param param The parameter name
|
91
|
+
* @param isILikeEnabled Whether the ILIKE operator can be used
|
77
92
|
*/
|
78
|
-
protected mapWhereOperators(where: CrudRequestWhereField, param: string): {
|
93
|
+
protected mapWhereOperators(alias: string, where: CrudRequestWhereField, param: string, isILikeEnabled: boolean): {
|
79
94
|
where: string;
|
80
95
|
params: ObjectLiteral;
|
81
96
|
};
|
97
|
+
protected createLowerLike(isILikeEnabled: boolean, field: string, not?: boolean): string;
|
98
|
+
protected isILikeEnabled(qb: SelectQueryBuilder<any>): boolean;
|
82
99
|
}
|
83
100
|
|
84
|
-
export {
|
101
|
+
export { TypeOrmQueryAdapter, type TypeOrmQueryAdapterOptions };
|
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";var
|
1
|
+
"use strict";var E=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var g=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var c=(o,t)=>E(o,"name",{value:t,configurable:!0});var _=(o,t)=>{for(var r in t)E(o,r,{get:t[r],enumerable:!0})},w=(o,t,r,e)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of g(t))!O.call(o,i)&&i!==r&&E(o,i,{get:()=>t[i],enumerable:!(e=T(t,i))||e.enumerable});return o};var S=o=>w(E({},"__esModule",{value:!0}),o);var R={};_(R,{TypeOrmQueryAdapter:()=>N});module.exports=S(R);var h=require("typeorm");function d(o,t,r=0){if(!Array.isArray(t)||t.length<r)throw new Error(`${o} must be an array with at least ${r} items`);return t}c(d,"ensureArray");function f(o,t){if(t)throw new Error(`${o} must be null`)}c(f,"ensureFalsy");function L(o){return o!=null}c(L,"isValid");function $(o,t){return o.length-1!==t.length?!1:t.every((r,e)=>o[e]===r)}c($,"pathHasBase");var N=class{constructor(t={}){this.options=t}static{c(this,"TypeOrmQueryAdapter")}build(t,r){return t=this.createBaseQuery(t,r),t=this.paginateQuery(t,r),t}async getOne(t,r){return await this.createBaseQuery(t,r).getOne()??null}async getMany(t,r){let e=this.createBaseQuery(t,r),s=await this.paginateQuery(e.clone(),r).getMany(),a=await e.getCount(),n=r.offset??0,l=r.limit??a,u=s.length,p=Math.floor(n/l)+1,y=Math.ceil(a/l);return{data:s,count:u,page:p,pageCount:y,total:a}}createBaseQuery(t,r){let e=[],i=this.isILikeEnabled(t);return this.adaptSelect(t,r.select),this.adaptRelations(t,r.relations,r.select),this.adaptWhere(t.alias,t,r.where,!1,e,i),this.adaptOrder(t,r.order),t}paginateQuery(t,r){return t.limit(r.limit).offset(r.offset)}adaptSelect(t,r){if(r.length===0)return;let e=r.filter(i=>i.field.length===1);t.select(e.map(i=>[t.alias,...i.field].join(".")))}adaptRelations(t,r,e){for(let i of r){let s=[t.alias,...i.field].join("."),a=i.alias||s.replace(".","_"),n=e.filter(l=>$(l.field,i.field));n.length===0?t.leftJoinAndSelect(s,a):(t.leftJoin(s,a),t.addSelect(n.map(l=>[a,l.field[l.field.length-1]].join("."))))}}adaptOrder(t,r){for(let e of r){let i=[t.alias,...e.field].join(".");t.addOrderBy(i,e.order)}}adaptWhere(t,r,e,i,s,a){let n=(i?r.orWhere:r.andWhere).bind(r);if(e.or&&e.or.length>0)n(new h.Brackets(l=>e.or.forEach(u=>this.adaptWhere(t,l,u,!0,s,a))));else if(e.and&&e.and.length>0)n(new h.Brackets(l=>e.and.forEach(u=>this.adaptWhere(t,l,u,!1,s,a))));else if(e.field){let l=this.createParam(s,e.field),u=this.mapWhereOperators(t,e,l,a);n(u.where,u.params)}}createParam(t,r){let e=r.length>0?r[r.length-1]:"",i,s=0;do i="req_"+e+"_"+s,s++;while(t.includes(i));return t.push(i),i}mapWhereOperators(t,r,e,i){let s=[t,...r.field].join("."),a=r.operator,n=r.value;switch(a){case"eq":return{where:`${s} = :${e}`,params:{[e]:n}};case"neq":return{where:`${s} != :${e}`,params:{[e]:n}};case"gt":return{where:`${s} > :${e}`,params:{[e]:n}};case"gte":return{where:`${s} >= :${e}`,params:{[e]:n}};case"lt":return{where:`${s} < :${e}`,params:{[e]:n}};case"lte":return{where:`${s} <= :${e}`,params:{[e]:n}};case"starts":return{where:`${s} LIKE :${e}`,params:{[e]:`${n}%`}};case"ends":return{where:`${s} LIKE :${e}`,params:{[e]:`%${n}`}};case"contains":return{where:`${s} LIKE :${e}`,params:{[e]:`%${n}%`}};case"not_contains":return{where:`${s} NOT LIKE :${e}`,params:{[e]:`%${n}%`}};case"in":return n=d("IN operator",n,1),{where:`${s} IN (:...${e})`,params:{[e]:n}};case"not_in":return n=d("NOT IN operator",n,1),{where:`${s} NOT IN (:...${e})`,params:{[e]:n}};case"between":let l=d("BETWEEN operator",n,2);return{where:`${s} BETWEEN :${e}_start AND :${e}_end`,params:{[`${e}_start`]:l[0],[`${e}_end`]:l[1]}};case"is_null":return f("IS NULL operator",n),{where:`${s} IS NULL`,params:{}};case"not_null":return f("NOT NULL operator",n),{where:`${s} IS NOT NULL`,params:{}};case"eq_lower":return{where:`LOWER(${s}) = :${e}`,params:{[e]:n}};case"neq_lower":return{where:`LOWER(${s}) != :${e}`,params:{[e]:n}};case"starts_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`${n}%`}};case"ends_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`%${n}`}};case"contains_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`%${n}%`}};case"not_contains_lower":return{where:`${this.createLowerLike(i,s,!0)} :${e}`,params:{[e]:`%${n}%`}};case"in_lower":return d("IN operator",n,1),{where:`LOWER(${s}) IN (...:${e})`,params:{[e]:n}};case"not_in_lower":return d("NOT IN operator",n,1),{where:`LOWER(${s}) NOT IN (...:${e})`,params:{[e]:n}};default:throw new Error(`Unknown operator "${a}"`)}}createLowerLike(t,r,e=!1){return t?e?`${r} NOT ILIKE`:`${r} ILIKE`:e?`LOWER(${r}) NOT LIKE`:`LOWER(${r}) LIKE`}isILikeEnabled(t){let r=this.options.ilike;if(L(r))return r;let e=t.connection.options.type;return e==="postgres"||e==="aurora-postgres"}};0&&(module.exports={TypeOrmQueryAdapter});
|
2
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/typeorm/index.ts","../../../src/adapters/typeorm/typeorm.query-adapter.ts","../../../src/utils/functions.ts"],"sourcesContent":["\r\nexport * from './typeorm.query-adapter';\r\n","import { Brackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from 'typeorm';\r\nimport { QueryAdapter } from '../../models/query-adapter';\r\nimport {\r\n CrudRequest,\r\n CrudRequestOrder,\r\n CrudRequestRelation,\r\n ParsedRequestSelect\r\n} from '../../models/crud-request';\r\nimport {\r\n CrudRequestWhere,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator\r\n} from '../../models/crud-request-where';\r\nimport { GetManyResult } from '../../models/get-many-result';\r\nimport { ensureArray, ensureFalsy } from '../../utils/functions';\r\n\r\n/**\r\n * Adapts queries to TypeORM query builder object\r\n */\r\nexport class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public build<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n qb = this.createBaseQuery(qb, query);\r\n qb = this.paginateQuery(qb, query);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getOne<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<E | null> {\r\n const query = this.createBaseQuery(qb, request);\r\n const entity = await query.getOne();\r\n\r\n return entity ?? null;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getMany<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<GetManyResult<E>> {\r\n const fullQuery = this.createBaseQuery(qb, request);\r\n const paginatedQuery = this.paginateQuery(fullQuery.clone(), request);\r\n\r\n const data = await paginatedQuery.getMany();\r\n const total = await fullQuery.getCount();\r\n\r\n const offset = request.offset ?? 0;\r\n const limit = request.limit ?? total;\r\n\r\n const count = data.length;\r\n const page = Math.floor(offset / limit) + 1;\r\n const pageCount = Math.ceil(total / limit);\r\n\r\n return {\r\n data,\r\n count,\r\n page,\r\n pageCount,\r\n total,\r\n };\r\n }\r\n\r\n /**\r\n * Creates a query filtered from the parsed request\r\n *\r\n * @param qb The base query builder\r\n * @param query The parsed query\r\n */\r\n protected createBaseQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n const paramsDefined: string[] = [];\r\n\r\n this.adaptSelect(qb, query.select);\r\n this.adaptRelations(qb, query.relations);\r\n this.adaptWhere(qb, query.where, false, paramsDefined);\r\n this.adaptOrder(qb, query.order);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * Paginates a query based on the parsed request\r\n *\r\n * @param qb The query builder\r\n * @param query The parsed query\r\n */\r\n protected paginateQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n return qb.limit(query.limit).offset(query.offset);\r\n }\r\n\r\n /**\r\n * Adapts a select\r\n *\r\n * @param qb The query builder\r\n * @param select The parsed select fields\r\n */\r\n protected adaptSelect<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, select: ParsedRequestSelect): void {\r\n qb.addSelect(select.map(s => s.field.join('.')));\r\n }\r\n\r\n /**\r\n * Adapts the join relation list\r\n *\r\n * @param qb The query builder\r\n * @param relations The parsed relation list\r\n */\r\n protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[]): void {\r\n for (const relation of relations) {\r\n const path = relation.field.join('.');\r\n const alias = relation.alias || path.replace('.', '_');\r\n\r\n qb.leftJoin(path, alias);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts the order by list\r\n *\r\n * @param qb The query builder\r\n * @param ordering The parsed ordering\r\n */\r\n protected adaptOrder<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, ordering: CrudRequestOrder[]): void {\r\n for (const order of ordering) {\r\n const path = order.field.join('.');\r\n\r\n qb.addOrderBy(path, order.order);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts a where condition\r\n *\r\n * @param qb The query builder\r\n * @param where The quere condition\r\n * @param or Whether this where condition is AND/OR\r\n * @param params The registered parameter name list\r\n */\r\n protected adaptWhere(qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[]): void {\r\n const addWhere = (or ? qb.orWhere : qb.andWhere).bind(qb);\r\n\r\n if (where.or && where.or.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.or!.forEach(item => this.adaptWhere(wqb, item, true, params))\r\n ));\r\n } else if (where.and && where.and.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.and!.forEach(item => this.adaptWhere(wqb, item, false, params))\r\n ));\r\n } else if (where.field) {\r\n const param = this.createParam(params, where.field);\r\n const query = this.mapWhereOperators(where as CrudRequestWhereField, param);\r\n\r\n addWhere(query.where, query.params);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a query parameter name based on a field\r\n *\r\n * @param paramsDefined The array the parameter will be registered onto\r\n * @param field The field path\r\n */\r\n protected createParam(paramsDefined: string[], field: string[]): string {\r\n const name = field.length > 0 ? field[field.length - 1] : '';\r\n let param: string;\r\n let iteration: number = 0;\r\n\r\n do {\r\n param = 'req_' + name + '_' + iteration;\r\n iteration++;\r\n } while (paramsDefined.includes(param));\r\n\r\n paramsDefined.push(param);\r\n return param;\r\n }\r\n\r\n /**\r\n * Maps where operators to a pseudo-SQL statement and a parameter map\r\n *\r\n * @param where The where condition\r\n * @param param The parameter name\r\n */\r\n protected mapWhereOperators(where: CrudRequestWhereField, param: string): { where: string, params: ObjectLiteral } {\r\n const field = where.field.join('.');\r\n const operator = where.operator;\r\n let value: unknown = where.value;\r\n\r\n switch (operator) {\r\n case CrudRequestWhereOperator.EQ:\r\n return { where: `${field} = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ:\r\n return { where: `${field} != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GT:\r\n return { where: `${field} > :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GTE:\r\n return { where: `${field} >= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LT:\r\n return { where: `${field} < :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LTE:\r\n return { where: `${field} <= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN:\r\n value = ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN:\r\n value = ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.BETWEEN:\r\n const arr = ensureArray('BETWEEN operator', value, 2);\r\n\r\n return {\r\n where: `${field} BETWEEN :${param}_start AND :${param}_end`,\r\n params: { [`${param}_start`]: arr[0], [`${param}_end`]: arr[1] },\r\n };\r\n\r\n case CrudRequestWhereOperator.IS_NULL:\r\n ensureFalsy('IS NULL operator', value);\r\n\r\n return { where: `${field} IS NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.NOT_NULL:\r\n ensureFalsy('NOT NULL operator', value);\r\n\r\n return { where: `${field} IS NOT NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.EQ_LOWER:\r\n return { where: `LOWER(${field}) = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ_LOWER:\r\n return { where: `LOWER(${field}) != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS_LOWER:\r\n return { where: `LOWER(${field}) LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS_LOWER:\r\n return { where: `LOWER(${field}) LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS_LOWER:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS_LOWER:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN_LOWER:\r\n ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (...:${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN_LOWER:\r\n ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (...:${param})`, params: { [param]: value } };\r\n\r\n default:\r\n throw new Error(`Unknown operator \"${operator}\"`);\r\n }\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid(value: any): value is object {\r\n return value !== null && value !== undefined;\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,yBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAoF,mBCiB7E,SAASC,EAAeC,EAAmBC,EAAiBC,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQD,CAAI,GAAKA,EAAK,OAASC,EACxC,MAAM,IAAI,MAAM,GAAGF,CAAS,mCAAmCE,CAAS,QAAQ,EAElF,OAAOD,CACT,CALgBE,EAAAJ,EAAA,eAOT,SAASK,EAAYJ,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBG,EAAAC,EAAA,eDLT,IAAMC,EAAN,KAA0F,CAnBjG,MAmBiG,CAAAC,EAAA,4BAKxF,MAA+BC,EAA2BC,EAA2C,CAC1G,OAAAD,EAAK,KAAK,gBAAgBA,EAAIC,CAAK,EACnCD,EAAK,KAAK,cAAcA,EAAIC,CAAK,EAE1BD,CACT,CAKA,MAAa,OAAgCA,EAAiCE,EAAyC,CAIrH,OAFe,MADD,KAAK,gBAAgBF,EAAIE,CAAO,EACnB,OAAO,GAEjB,IACnB,CAKA,MAAa,QAAiCF,EAAi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BACE,OAAAC,EAAY,kBAAmBD,EAAO,CAAC,EAEhC,CAAE,MAAO,GAAGJ,CAAK,gBAAgBD,CAAK,IAAK,OAAQ,CAAE,CAACA,CAAK,EAAGK,CAAM,CAAE,EAE/E,QACE,MAAM,IAAI,MAAM,qBAAqBD,CAAQ,GAAG,CACpD,CACF,CAEF","names":["typeorm_exports","__export","TypeormQueryAdapter","__toCommonJS","import_typeorm","ensureArray","fieldName","data","minLength","__name","ensureFalsy","TypeormQueryAdapter","__name","qb","query","request","fullQuery","data","total","offset","limit","count","page","pageCount","paramsDefined","select","s","relations","relation","path","alias","ordering","order","where","or","params","addWhere","wqb","item","param","field","name","iteration","operator","value","ensureArray","arr","ensureFalsy"]}
|
1
|
+
{"version":3,"sources":["../../../src/adapters/typeorm/index.ts","../../../src/adapters/typeorm/typeorm.query-adapter.ts","../../../src/utils/functions.ts","../../../src/utils/field-path.ts"],"sourcesContent":["\r\nexport * from './typeorm.query-adapter';\r\n","import { Brackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from 'typeorm';\r\nimport { QueryAdapter } from '../../models/query-adapter';\r\nimport { CrudRequest, CrudRequestOrder, CrudRequestRelation, ParsedRequestSelect } from '../../models/crud-request';\r\nimport { CrudRequestWhere, CrudRequestWhereField, CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { GetManyResult } from '../../models/get-many-result';\r\nimport { ensureArray, ensureFalsy, isValid } from '../../utils/functions';\r\nimport { pathHasBase } from '../../utils/field-path';\r\n\r\nexport interface TypeOrmQueryAdapterOptions {\r\n /**\r\n * Whether it will use ILIKE for case-insensitive operations.\r\n *\r\n * If undefined, it will be enabled by default for postgres and aurora-postgres databases\r\n */\r\n ilike?: boolean;\r\n}\r\n\r\n/**\r\n * Adapts queries to TypeORM query builder object\r\n */\r\nexport class TypeOrmQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {\r\n\r\n constructor(\r\n private readonly options: TypeOrmQueryAdapterOptions = {},\r\n ) {}\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public build<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n qb = this.createBaseQuery(qb, query);\r\n qb = this.paginateQuery(qb, query);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getOne<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<E | null> {\r\n const query = this.createBaseQuery(qb, request);\r\n const entity = await query.getOne();\r\n\r\n return entity ?? null;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getMany<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<GetManyResult<E>> {\r\n const fullQuery = this.createBaseQuery(qb, request);\r\n const paginatedQuery = this.paginateQuery(fullQuery.clone(), request);\r\n\r\n const data = await paginatedQuery.getMany();\r\n const total = await fullQuery.getCount();\r\n\r\n const offset = request.offset ?? 0;\r\n const limit = request.limit ?? total;\r\n\r\n const count = data.length;\r\n const page = Math.floor(offset / limit) + 1;\r\n const pageCount = Math.ceil(total / limit);\r\n\r\n return {\r\n data,\r\n count,\r\n page,\r\n pageCount,\r\n total,\r\n };\r\n }\r\n\r\n /**\r\n * Creates a query filtered from the parsed request\r\n *\r\n * @param qb The base query builder\r\n * @param query The parsed query\r\n */\r\n protected createBaseQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n const paramsDefined: string[] = [];\r\n const isILikeEnabled = this.isILikeEnabled(qb);\r\n\r\n this.adaptSelect(qb, query.select);\r\n this.adaptRelations(qb, query.relations, query.select);\r\n this.adaptWhere(qb.alias, qb, query.where, false, paramsDefined, isILikeEnabled);\r\n this.adaptOrder(qb, query.order);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * Paginates a query based on the parsed request\r\n *\r\n * @param qb The query builder\r\n * @param query The parsed query\r\n */\r\n protected paginateQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n return qb.limit(query.limit).offset(query.offset);\r\n }\r\n\r\n /**\r\n * Adapts a select\r\n *\r\n * @param qb The query builder\r\n * @param select The parsed select fields\r\n */\r\n protected adaptSelect<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, select: ParsedRequestSelect): void {\r\n if (select.length === 0)\r\n return;\r\n\r\n // Only fields that are one level deep\r\n const fields = select.filter(f => f.field.length === 1);\r\n\r\n qb.select(fields.map(s => [qb.alias, ...s.field].join('.')));\r\n }\r\n\r\n /**\r\n * Adapts the join relation list\r\n *\r\n * @param qb The query builder\r\n * @param relations The parsed relation list\r\n * @param select The parsed select fields\r\n */\r\n protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[], select: ParsedRequestSelect): void {\r\n for (const relation of relations) {\r\n const path = [qb.alias, ...relation.field].join('.');\r\n const alias = relation.alias || path.replace('.', '_');\r\n\r\n const fields = select.filter(f => pathHasBase(f.field, relation.field));\r\n\r\n if (fields.length === 0) {\r\n qb.leftJoinAndSelect(path, alias);\r\n } else {\r\n qb.leftJoin(path, alias);\r\n qb.addSelect(fields.map(f => [alias, f.field[f.field.length - 1]].join('.')));\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Adapts the order by list\r\n *\r\n * @param qb The query builder\r\n * @param ordering The parsed ordering\r\n */\r\n protected adaptOrder<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, ordering: CrudRequestOrder[]): void {\r\n for (const order of ordering) {\r\n const path = [qb.alias, ...order.field].join('.');\r\n\r\n qb.addOrderBy(path, order.order);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts a where condition\r\n *\r\n * @param alias The query builder alias\r\n * @param qb The query builder\r\n * @param where The quere condition\r\n * @param or Whether this where condition is AND/OR\r\n * @param params The registered parameter name list\r\n * @param isILikeEnabled Whether the ILIKE operator can be used\r\n */\r\n protected adaptWhere(\r\n alias: string,\r\n qb: WhereExpressionBuilder,\r\n where: CrudRequestWhere,\r\n or: boolean,\r\n params: string[],\r\n isILikeEnabled: boolean,\r\n ): void {\r\n const addWhere = (or ? qb.orWhere : qb.andWhere).bind(qb);\r\n\r\n if (where.or && where.or.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.or!.forEach(item => this.adaptWhere(alias, wqb, item, true, params, isILikeEnabled))\r\n ));\r\n } else if (where.and && where.and.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.and!.forEach(item => this.adaptWhere(alias, wqb, item, false, params, isILikeEnabled))\r\n ));\r\n } else if (where.field) {\r\n const param = this.createParam(params, where.field);\r\n const query = this.mapWhereOperators(alias, where as CrudRequestWhereField, param, isILikeEnabled);\r\n\r\n addWhere(query.where, query.params);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a query parameter name based on a field\r\n *\r\n * @param paramsDefined The array the parameter will be registered onto\r\n * @param field The field path\r\n */\r\n protected createParam(paramsDefined: string[], field: string[]): string {\r\n const name = field.length > 0 ? field[field.length - 1] : '';\r\n let param: string;\r\n let iteration: number = 0;\r\n\r\n do {\r\n param = 'req_' + name + '_' + iteration;\r\n iteration++;\r\n } while (paramsDefined.includes(param));\r\n\r\n paramsDefined.push(param);\r\n return param;\r\n }\r\n\r\n /**\r\n * Maps where operators to a pseudo-SQL statement and a parameter map\r\n *\r\n * @param alias The query builder alias\r\n * @param where The where condition\r\n * @param param The parameter name\r\n * @param isILikeEnabled Whether the ILIKE operator can be used\r\n */\r\n protected mapWhereOperators(\r\n alias: string,\r\n where: CrudRequestWhereField,\r\n param: string,\r\n isILikeEnabled: boolean,\r\n ): { where: string, params: ObjectLiteral } {\r\n const field = [alias, ...where.field].join('.');\r\n const operator = where.operator;\r\n let value: unknown = where.value;\r\n\r\n switch (operator) {\r\n case CrudRequestWhereOperator.EQ:\r\n return { where: `${field} = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ:\r\n return { where: `${field} != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GT:\r\n return { where: `${field} > :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GTE:\r\n return { where: `${field} >= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LT:\r\n return { where: `${field} < :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LTE:\r\n return { where: `${field} <= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN:\r\n value = ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN:\r\n value = ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.BETWEEN:\r\n const arr = ensureArray('BETWEEN operator', value, 2);\r\n\r\n return {\r\n where: `${field} BETWEEN :${param}_start AND :${param}_end`,\r\n params: { [`${param}_start`]: arr[0], [`${param}_end`]: arr[1] },\r\n };\r\n\r\n case CrudRequestWhereOperator.IS_NULL:\r\n ensureFalsy('IS NULL operator', value);\r\n\r\n return { where: `${field} IS NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.NOT_NULL:\r\n ensureFalsy('NOT NULL operator', value);\r\n\r\n return { where: `${field} IS NOT NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.EQ_LOWER:\r\n return { where: `LOWER(${field}) = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ_LOWER:\r\n return { where: `LOWER(${field}) != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field, true)} :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN_LOWER:\r\n ensureArray('IN operator', value, 1);\r\n\r\n return { where: `LOWER(${field}) IN (...:${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN_LOWER:\r\n ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `LOWER(${field}) NOT IN (...:${param})`, params: { [param]: value } };\r\n\r\n default:\r\n throw new Error(`Unknown operator \"${operator}\"`);\r\n }\r\n }\r\n\r\n protected createLowerLike(isILikeEnabled: boolean, field: string, not: boolean = false): string {\r\n if (isILikeEnabled)\r\n return not ? `${field} NOT ILIKE` : `${field} ILIKE`;\r\n\r\n return not ? `LOWER(${field}) NOT LIKE` : `LOWER(${field}) LIKE`;\r\n }\r\n\r\n protected isILikeEnabled(qb: SelectQueryBuilder<any>): boolean {\r\n const ilikeEnabled = this.options.ilike;\r\n\r\n if (isValid(ilikeEnabled))\r\n return ilikeEnabled;\r\n\r\n const type = qb.connection.options.type;\r\n return type === 'postgres' || type === 'aurora-postgres';\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","/**\r\n * Checks whether two field paths are equal\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] is equal to [\"path\", \"to\", \"field\"] but not [\"something\", \"else\"]\r\n */\r\nexport function pathEquals(path1: string[], path2: string[]): boolean {\r\n if (path1.length !== path2.length)\r\n return false;\r\n\r\n return path1.every((p1, i) => path2[i] === p1);\r\n}\r\n\r\n/**\r\n * Checks whether a path starts with another path.\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] starts with [\"path\"] or [\"path\", \"to\"] but not [\"something\", \"else\"]\r\n */\r\nexport function pathStartsWith(path: string[], start: string[]): boolean {\r\n if (path.length < start.length)\r\n return false;\r\n\r\n return start.every((start, i) => path[i] === start);\r\n}\r\n\r\n/**\r\n * Checks whether the base of a path matches.\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] has a base of [\"path\", \"to\"] but not [\"path\"]\r\n */\r\nexport function pathHasBase(path: string[], base: string[]): boolean {\r\n if (path.length - 1 !== base.length)\r\n return false;\r\n\r\n return base.every((start, i) => path[i] === start);\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,yBAAAE,IAAA,eAAAC,EAAAH,GCAA,IAAAI,EAAoF,mBC8B7E,SAASC,EAAeC,EAAmBC,EAAiBC,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQD,CAAI,GAAKA,EAAK,OAASC,EACxC,MAAM,IAAI,MAAM,GAAGF,CAAS,mCAAmCE,CAAS,QAAQ,EAElF,OAAOD,CACT,CALgBE,EAAAJ,EAAA,eAOT,SAASK,EAAYJ,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBG,EAAAC,EAAA,eAKT,SAASC,EAAWC,EAAyC,CAClE,OAAOA,GAAU,IACnB,CAFgBH,EAAAE,EAAA,WCbT,SAASE,EAAYC,EAAgBC,EAAyB,CACnE,OAAID,EAAK,OAAS,IAAMC,EAAK,OACpB,GAEFA,EAAK,MAAM,CAACC,EAAOC,IAAMH,EAAKG,CAAC,IAAMD,CAAK,CACnD,CALgBE,EAAAL,EAAA,eFTT,IAAMM,EAAN,KAA0F,CAE/F,YACmBC,EAAsC,CAAC,EACxD,CADiB,aAAAA,CAChB,CAxBL,MAoBiG,CAAAC,EAAA,4BASxF,MAA+BC,EAA2BC,EAA2C,CAC1G,OAAAD,EAAK,KAAK,gBAAgBA,EAAIC,CAAK,EACnCD,EAAK,KAAK,cAAcA,EAAIC,CAAK,EAE1BD,CACT,CAKA,MAAa,OAAgCA,EAAiCE,EAAyC,CAIrH,OAFe,MADD,KAAK,gBAAgBF,EAAIE,CAAO,EACnB,OAAO,GAEjB,IACnB,CAKA,MAAa,QAAiCF,EAAiCE,EAAiD,CAC9H,IAAMC,EAAY,KAAK,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AcA,IAAS,iBACzC,CAEF","names":["typeorm_exports","__export","TypeOrmQueryAdapter","__toCommonJS","import_typeorm","ensureArray","fieldName","data","minLength","__name","ensureFalsy","isValid","value","pathHasBase","path","base","start","i","__name","TypeOrmQueryAdapter","options","__name","qb","query","request","fullQuery","data","total","offset","limit","count","page","pageCount","paramsDefined","isILikeEnabled","select","fields","f","s","relations","relation","path","alias","pathHasBase","ordering","order","where","or","params","addWhere","wqb","item","param","field","name","iteration","operator","value","ensureArray","arr","ensureFalsy","not","ilikeEnabled","isValid","type"]}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
var
|
1
|
+
var y=Object.defineProperty;var c=(a,t)=>y(a,"name",{value:t,configurable:!0});import{Brackets as N}from"typeorm";function d(a,t,r=0){if(!Array.isArray(t)||t.length<r)throw new Error(`${a} must be an array with at least ${r} items`);return t}c(d,"ensureArray");function E(a,t){if(t)throw new Error(`${a} must be null`)}c(E,"ensureFalsy");function f(a){return a!=null}c(f,"isValid");function h(a,t){return a.length-1!==t.length?!1:t.every((r,e)=>a[e]===r)}c(h,"pathHasBase");var L=class{constructor(t={}){this.options=t}static{c(this,"TypeOrmQueryAdapter")}build(t,r){return t=this.createBaseQuery(t,r),t=this.paginateQuery(t,r),t}async getOne(t,r){return await this.createBaseQuery(t,r).getOne()??null}async getMany(t,r){let e=this.createBaseQuery(t,r),s=await this.paginateQuery(e.clone(),r).getMany(),l=await e.getCount(),n=r.offset??0,o=r.limit??l,u=s.length,$=Math.floor(n/o)+1,p=Math.ceil(l/o);return{data:s,count:u,page:$,pageCount:p,total:l}}createBaseQuery(t,r){let e=[],i=this.isILikeEnabled(t);return this.adaptSelect(t,r.select),this.adaptRelations(t,r.relations,r.select),this.adaptWhere(t.alias,t,r.where,!1,e,i),this.adaptOrder(t,r.order),t}paginateQuery(t,r){return t.limit(r.limit).offset(r.offset)}adaptSelect(t,r){if(r.length===0)return;let e=r.filter(i=>i.field.length===1);t.select(e.map(i=>[t.alias,...i.field].join(".")))}adaptRelations(t,r,e){for(let i of r){let s=[t.alias,...i.field].join("."),l=i.alias||s.replace(".","_"),n=e.filter(o=>h(o.field,i.field));n.length===0?t.leftJoinAndSelect(s,l):(t.leftJoin(s,l),t.addSelect(n.map(o=>[l,o.field[o.field.length-1]].join("."))))}}adaptOrder(t,r){for(let e of r){let i=[t.alias,...e.field].join(".");t.addOrderBy(i,e.order)}}adaptWhere(t,r,e,i,s,l){let n=(i?r.orWhere:r.andWhere).bind(r);if(e.or&&e.or.length>0)n(new N(o=>e.or.forEach(u=>this.adaptWhere(t,o,u,!0,s,l))));else if(e.and&&e.and.length>0)n(new N(o=>e.and.forEach(u=>this.adaptWhere(t,o,u,!1,s,l))));else if(e.field){let o=this.createParam(s,e.field),u=this.mapWhereOperators(t,e,o,l);n(u.where,u.params)}}createParam(t,r){let e=r.length>0?r[r.length-1]:"",i,s=0;do i="req_"+e+"_"+s,s++;while(t.includes(i));return t.push(i),i}mapWhereOperators(t,r,e,i){let s=[t,...r.field].join("."),l=r.operator,n=r.value;switch(l){case"eq":return{where:`${s} = :${e}`,params:{[e]:n}};case"neq":return{where:`${s} != :${e}`,params:{[e]:n}};case"gt":return{where:`${s} > :${e}`,params:{[e]:n}};case"gte":return{where:`${s} >= :${e}`,params:{[e]:n}};case"lt":return{where:`${s} < :${e}`,params:{[e]:n}};case"lte":return{where:`${s} <= :${e}`,params:{[e]:n}};case"starts":return{where:`${s} LIKE :${e}`,params:{[e]:`${n}%`}};case"ends":return{where:`${s} LIKE :${e}`,params:{[e]:`%${n}`}};case"contains":return{where:`${s} LIKE :${e}`,params:{[e]:`%${n}%`}};case"not_contains":return{where:`${s} NOT LIKE :${e}`,params:{[e]:`%${n}%`}};case"in":return n=d("IN operator",n,1),{where:`${s} IN (:...${e})`,params:{[e]:n}};case"not_in":return n=d("NOT IN operator",n,1),{where:`${s} NOT IN (:...${e})`,params:{[e]:n}};case"between":let o=d("BETWEEN operator",n,2);return{where:`${s} BETWEEN :${e}_start AND :${e}_end`,params:{[`${e}_start`]:o[0],[`${e}_end`]:o[1]}};case"is_null":return E("IS NULL operator",n),{where:`${s} IS NULL`,params:{}};case"not_null":return E("NOT NULL operator",n),{where:`${s} IS NOT NULL`,params:{}};case"eq_lower":return{where:`LOWER(${s}) = :${e}`,params:{[e]:n}};case"neq_lower":return{where:`LOWER(${s}) != :${e}`,params:{[e]:n}};case"starts_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`${n}%`}};case"ends_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`%${n}`}};case"contains_lower":return{where:`${this.createLowerLike(i,s)} :${e}`,params:{[e]:`%${n}%`}};case"not_contains_lower":return{where:`${this.createLowerLike(i,s,!0)} :${e}`,params:{[e]:`%${n}%`}};case"in_lower":return d("IN operator",n,1),{where:`LOWER(${s}) IN (...:${e})`,params:{[e]:n}};case"not_in_lower":return d("NOT IN operator",n,1),{where:`LOWER(${s}) NOT IN (...:${e})`,params:{[e]:n}};default:throw new Error(`Unknown operator "${l}"`)}}createLowerLike(t,r,e=!1){return t?e?`${r} NOT ILIKE`:`${r} ILIKE`:e?`LOWER(${r}) NOT LIKE`:`LOWER(${r}) LIKE`}isILikeEnabled(t){let r=this.options.ilike;if(f(r))return r;let e=t.connection.options.type;return e==="postgres"||e==="aurora-postgres"}};export{L as TypeOrmQueryAdapter};
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/adapters/typeorm/typeorm.query-adapter.ts","../../../src/utils/functions.ts"],"sourcesContent":["import { Brackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from 'typeorm';\r\nimport { QueryAdapter } from '../../models/query-adapter';\r\nimport {\r\n CrudRequest,\r\n CrudRequestOrder,\r\n CrudRequestRelation,\r\n ParsedRequestSelect\r\n} from '../../models/crud-request';\r\nimport {\r\n CrudRequestWhere,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator\r\n} from '../../models/crud-request-where';\r\nimport { GetManyResult } from '../../models/get-many-result';\r\nimport { ensureArray, ensureFalsy } from '../../utils/functions';\r\n\r\n/**\r\n * Adapts queries to TypeORM query builder object\r\n */\r\nexport class TypeormQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public build<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n qb = this.createBaseQuery(qb, query);\r\n qb = this.paginateQuery(qb, query);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getOne<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<E | null> {\r\n const query = this.createBaseQuery(qb, request);\r\n const entity = await query.getOne();\r\n\r\n return entity ?? null;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getMany<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<GetManyResult<E>> {\r\n const fullQuery = this.createBaseQuery(qb, request);\r\n const paginatedQuery = this.paginateQuery(fullQuery.clone(), request);\r\n\r\n const data = await paginatedQuery.getMany();\r\n const total = await fullQuery.getCount();\r\n\r\n const offset = request.offset ?? 0;\r\n const limit = request.limit ?? total;\r\n\r\n const count = data.length;\r\n const page = Math.floor(offset / limit) + 1;\r\n const pageCount = Math.ceil(total / limit);\r\n\r\n return {\r\n data,\r\n count,\r\n page,\r\n pageCount,\r\n total,\r\n };\r\n }\r\n\r\n /**\r\n * Creates a query filtered from the parsed request\r\n *\r\n * @param qb The base query builder\r\n * @param query The parsed query\r\n */\r\n protected createBaseQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n const paramsDefined: string[] = [];\r\n\r\n this.adaptSelect(qb, query.select);\r\n this.adaptRelations(qb, query.relations);\r\n this.adaptWhere(qb, query.where, false, paramsDefined);\r\n this.adaptOrder(qb, query.order);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * Paginates a query based on the parsed request\r\n *\r\n * @param qb The query builder\r\n * @param query The parsed query\r\n */\r\n protected paginateQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n return qb.limit(query.limit).offset(query.offset);\r\n }\r\n\r\n /**\r\n * Adapts a select\r\n *\r\n * @param qb The query builder\r\n * @param select The parsed select fields\r\n */\r\n protected adaptSelect<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, select: ParsedRequestSelect): void {\r\n qb.addSelect(select.map(s => s.field.join('.')));\r\n }\r\n\r\n /**\r\n * Adapts the join relation list\r\n *\r\n * @param qb The query builder\r\n * @param relations The parsed relation list\r\n */\r\n protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[]): void {\r\n for (const relation of relations) {\r\n const path = relation.field.join('.');\r\n const alias = relation.alias || path.replace('.', '_');\r\n\r\n qb.leftJoin(path, alias);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts the order by list\r\n *\r\n * @param qb The query builder\r\n * @param ordering The parsed ordering\r\n */\r\n protected adaptOrder<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, ordering: CrudRequestOrder[]): void {\r\n for (const order of ordering) {\r\n const path = order.field.join('.');\r\n\r\n qb.addOrderBy(path, order.order);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts a where condition\r\n *\r\n * @param qb The query builder\r\n * @param where The quere condition\r\n * @param or Whether this where condition is AND/OR\r\n * @param params The registered parameter name list\r\n */\r\n protected adaptWhere(qb: WhereExpressionBuilder, where: CrudRequestWhere, or: boolean, params: string[]): void {\r\n const addWhere = (or ? qb.orWhere : qb.andWhere).bind(qb);\r\n\r\n if (where.or && where.or.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.or!.forEach(item => this.adaptWhere(wqb, item, true, params))\r\n ));\r\n } else if (where.and && where.and.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.and!.forEach(item => this.adaptWhere(wqb, item, false, params))\r\n ));\r\n } else if (where.field) {\r\n const param = this.createParam(params, where.field);\r\n const query = this.mapWhereOperators(where as CrudRequestWhereField, param);\r\n\r\n addWhere(query.where, query.params);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a query parameter name based on a field\r\n *\r\n * @param paramsDefined The array the parameter will be registered onto\r\n * @param field The field path\r\n */\r\n protected createParam(paramsDefined: string[], field: string[]): string {\r\n const name = field.length > 0 ? field[field.length - 1] : '';\r\n let param: string;\r\n let iteration: number = 0;\r\n\r\n do {\r\n param = 'req_' + name + '_' + iteration;\r\n iteration++;\r\n } while (paramsDefined.includes(param));\r\n\r\n paramsDefined.push(param);\r\n return param;\r\n }\r\n\r\n /**\r\n * Maps where operators to a pseudo-SQL statement and a parameter map\r\n *\r\n * @param where The where condition\r\n * @param param The parameter name\r\n */\r\n protected mapWhereOperators(where: CrudRequestWhereField, param: string): { where: string, params: ObjectLiteral } {\r\n const field = where.field.join('.');\r\n const operator = where.operator;\r\n let value: unknown = where.value;\r\n\r\n switch (operator) {\r\n case CrudRequestWhereOperator.EQ:\r\n return { where: `${field} = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ:\r\n return { where: `${field} != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GT:\r\n return { where: `${field} > :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GTE:\r\n return { where: `${field} >= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LT:\r\n return { where: `${field} < :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LTE:\r\n return { where: `${field} <= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN:\r\n value = ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN:\r\n value = ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.BETWEEN:\r\n const arr = ensureArray('BETWEEN operator', value, 2);\r\n\r\n return {\r\n where: `${field} BETWEEN :${param}_start AND :${param}_end`,\r\n params: { [`${param}_start`]: arr[0], [`${param}_end`]: arr[1] },\r\n };\r\n\r\n case CrudRequestWhereOperator.IS_NULL:\r\n ensureFalsy('IS NULL operator', value);\r\n\r\n return { where: `${field} IS NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.NOT_NULL:\r\n ensureFalsy('NOT NULL operator', value);\r\n\r\n return { where: `${field} IS NOT NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.EQ_LOWER:\r\n return { where: `LOWER(${field}) = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ_LOWER:\r\n return { where: `LOWER(${field}) != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS_LOWER:\r\n return { where: `LOWER(${field}) LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS_LOWER:\r\n return { where: `LOWER(${field}) LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS_LOWER:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS_LOWER:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN_LOWER:\r\n ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (...:${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN_LOWER:\r\n ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (...:${param})`, params: { [param]: value } };\r\n\r\n default:\r\n throw new Error(`Unknown operator \"${operator}\"`);\r\n }\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid(value: any): value is object {\r\n return value !== null && value !== undefined;\r\n}\r\n"],"mappings":"+EAAA,OAAS,YAAAA,MAA2E,UCiB7E,SAASC,EAAeC,EAAmBC,EAAiBC,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQD,CAAI,GAAKA,EAAK,OAASC,EACxC,MAAM,IAAI,MAAM,GAAGF,CAAS,mCAAmCE,CAAS,QAAQ,EAElF,OAAOD,CACT,CALgBE,EAAAJ,EAAA,eAOT,SAASK,EAAYJ,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C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,CAAK,UAAUD,CAAK,GAAI,OAAQ,CAAE,CAACA,CAAK,EAAG,IAAIK,CAAK,GAAI,CAAE,EAE/E,yBACE,MAAO,CAAE,MAAO,GAAGJ,CAAK,cAAcD,CAAK,GAAI,OAAQ,CAAE,CAACA,CAAK,EAAG,IAAIK,CAAK,GAAI,CAAE,EAEnF,eACE,OAAAC,EAAY,cAAeD,EAAO,CAAC,EAE5B,CAAE,MAAO,GAAGJ,CAAK,YAAYD,CAAK,IAAK,OAAQ,CAAE,CAACA,CAAK,EAAGK,CAAM,CAAE,EAE3E,mBACE,OAAAC,EAAY,kBAAmBD,EAAO,CAAC,EAEhC,CAAE,MAAO,GAAGJ,CAAK,gBAAgBD,CAAK,IAAK,OAAQ,CAAE,CAACA,CAAK,EAAGK,CAAM,CAAE,EAE/E,QACE,MAAM,IAAI,MAAM,qBAAqBD,CAAQ,GAAG,CACpD,CACF,CAEF","names":["Brackets","ensureArray","fieldName","data","minLength","__name","ensureFalsy","TypeormQueryAdapter","__name","qb","query","request","fullQuery","data","total","offset","limit","count","page","pageCount","paramsDefined","select","s","relations","relation","path","alias","ordering","order","where","or","params","addWhere","Brackets","wqb","item","param","field","name","iteration","operator","value","ensureArray","arr","ensureFalsy"]}
|
1
|
+
{"version":3,"sources":["../../../src/adapters/typeorm/typeorm.query-adapter.ts","../../../src/utils/functions.ts","../../../src/utils/field-path.ts"],"sourcesContent":["import { Brackets, ObjectLiteral, SelectQueryBuilder, WhereExpressionBuilder } from 'typeorm';\r\nimport { QueryAdapter } from '../../models/query-adapter';\r\nimport { CrudRequest, CrudRequestOrder, CrudRequestRelation, ParsedRequestSelect } from '../../models/crud-request';\r\nimport { CrudRequestWhere, CrudRequestWhereField, CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { GetManyResult } from '../../models/get-many-result';\r\nimport { ensureArray, ensureFalsy, isValid } from '../../utils/functions';\r\nimport { pathHasBase } from '../../utils/field-path';\r\n\r\nexport interface TypeOrmQueryAdapterOptions {\r\n /**\r\n * Whether it will use ILIKE for case-insensitive operations.\r\n *\r\n * If undefined, it will be enabled by default for postgres and aurora-postgres databases\r\n */\r\n ilike?: boolean;\r\n}\r\n\r\n/**\r\n * Adapts queries to TypeORM query builder object\r\n */\r\nexport class TypeOrmQueryAdapter implements QueryAdapter<SelectQueryBuilder<any>, ObjectLiteral> {\r\n\r\n constructor(\r\n private readonly options: TypeOrmQueryAdapterOptions = {},\r\n ) {}\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public build<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n qb = this.createBaseQuery(qb, query);\r\n qb = this.paginateQuery(qb, query);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getOne<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<E | null> {\r\n const query = this.createBaseQuery(qb, request);\r\n const entity = await query.getOne();\r\n\r\n return entity ?? null;\r\n }\r\n\r\n /**\r\n * @inheritDoc\r\n */\r\n public async getMany<E extends ObjectLiteral>(qb: SelectQueryBuilder<E | any>, request: CrudRequest): Promise<GetManyResult<E>> {\r\n const fullQuery = this.createBaseQuery(qb, request);\r\n const paginatedQuery = this.paginateQuery(fullQuery.clone(), request);\r\n\r\n const data = await paginatedQuery.getMany();\r\n const total = await fullQuery.getCount();\r\n\r\n const offset = request.offset ?? 0;\r\n const limit = request.limit ?? total;\r\n\r\n const count = data.length;\r\n const page = Math.floor(offset / limit) + 1;\r\n const pageCount = Math.ceil(total / limit);\r\n\r\n return {\r\n data,\r\n count,\r\n page,\r\n pageCount,\r\n total,\r\n };\r\n }\r\n\r\n /**\r\n * Creates a query filtered from the parsed request\r\n *\r\n * @param qb The base query builder\r\n * @param query The parsed query\r\n */\r\n protected createBaseQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n const paramsDefined: string[] = [];\r\n const isILikeEnabled = this.isILikeEnabled(qb);\r\n\r\n this.adaptSelect(qb, query.select);\r\n this.adaptRelations(qb, query.relations, query.select);\r\n this.adaptWhere(qb.alias, qb, query.where, false, paramsDefined, isILikeEnabled);\r\n this.adaptOrder(qb, query.order);\r\n\r\n return qb;\r\n }\r\n\r\n /**\r\n * Paginates a query based on the parsed request\r\n *\r\n * @param qb The query builder\r\n * @param query The parsed query\r\n */\r\n protected paginateQuery<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, query: CrudRequest): SelectQueryBuilder<E> {\r\n return qb.limit(query.limit).offset(query.offset);\r\n }\r\n\r\n /**\r\n * Adapts a select\r\n *\r\n * @param qb The query builder\r\n * @param select The parsed select fields\r\n */\r\n protected adaptSelect<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, select: ParsedRequestSelect): void {\r\n if (select.length === 0)\r\n return;\r\n\r\n // Only fields that are one level deep\r\n const fields = select.filter(f => f.field.length === 1);\r\n\r\n qb.select(fields.map(s => [qb.alias, ...s.field].join('.')));\r\n }\r\n\r\n /**\r\n * Adapts the join relation list\r\n *\r\n * @param qb The query builder\r\n * @param relations The parsed relation list\r\n * @param select The parsed select fields\r\n */\r\n protected adaptRelations<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, relations: CrudRequestRelation[], select: ParsedRequestSelect): void {\r\n for (const relation of relations) {\r\n const path = [qb.alias, ...relation.field].join('.');\r\n const alias = relation.alias || path.replace('.', '_');\r\n\r\n const fields = select.filter(f => pathHasBase(f.field, relation.field));\r\n\r\n if (fields.length === 0) {\r\n qb.leftJoinAndSelect(path, alias);\r\n } else {\r\n qb.leftJoin(path, alias);\r\n qb.addSelect(fields.map(f => [alias, f.field[f.field.length - 1]].join('.')));\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Adapts the order by list\r\n *\r\n * @param qb The query builder\r\n * @param ordering The parsed ordering\r\n */\r\n protected adaptOrder<E extends ObjectLiteral>(qb: SelectQueryBuilder<E>, ordering: CrudRequestOrder[]): void {\r\n for (const order of ordering) {\r\n const path = [qb.alias, ...order.field].join('.');\r\n\r\n qb.addOrderBy(path, order.order);\r\n }\r\n }\r\n\r\n /**\r\n * Adapts a where condition\r\n *\r\n * @param alias The query builder alias\r\n * @param qb The query builder\r\n * @param where The quere condition\r\n * @param or Whether this where condition is AND/OR\r\n * @param params The registered parameter name list\r\n * @param isILikeEnabled Whether the ILIKE operator can be used\r\n */\r\n protected adaptWhere(\r\n alias: string,\r\n qb: WhereExpressionBuilder,\r\n where: CrudRequestWhere,\r\n or: boolean,\r\n params: string[],\r\n isILikeEnabled: boolean,\r\n ): void {\r\n const addWhere = (or ? qb.orWhere : qb.andWhere).bind(qb);\r\n\r\n if (where.or && where.or.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.or!.forEach(item => this.adaptWhere(alias, wqb, item, true, params, isILikeEnabled))\r\n ));\r\n } else if (where.and && where.and.length > 0) {\r\n addWhere(new Brackets(\r\n wqb => where.and!.forEach(item => this.adaptWhere(alias, wqb, item, false, params, isILikeEnabled))\r\n ));\r\n } else if (where.field) {\r\n const param = this.createParam(params, where.field);\r\n const query = this.mapWhereOperators(alias, where as CrudRequestWhereField, param, isILikeEnabled);\r\n\r\n addWhere(query.where, query.params);\r\n }\r\n }\r\n\r\n /**\r\n * Creates a query parameter name based on a field\r\n *\r\n * @param paramsDefined The array the parameter will be registered onto\r\n * @param field The field path\r\n */\r\n protected createParam(paramsDefined: string[], field: string[]): string {\r\n const name = field.length > 0 ? field[field.length - 1] : '';\r\n let param: string;\r\n let iteration: number = 0;\r\n\r\n do {\r\n param = 'req_' + name + '_' + iteration;\r\n iteration++;\r\n } while (paramsDefined.includes(param));\r\n\r\n paramsDefined.push(param);\r\n return param;\r\n }\r\n\r\n /**\r\n * Maps where operators to a pseudo-SQL statement and a parameter map\r\n *\r\n * @param alias The query builder alias\r\n * @param where The where condition\r\n * @param param The parameter name\r\n * @param isILikeEnabled Whether the ILIKE operator can be used\r\n */\r\n protected mapWhereOperators(\r\n alias: string,\r\n where: CrudRequestWhereField,\r\n param: string,\r\n isILikeEnabled: boolean,\r\n ): { where: string, params: ObjectLiteral } {\r\n const field = [alias, ...where.field].join('.');\r\n const operator = where.operator;\r\n let value: unknown = where.value;\r\n\r\n switch (operator) {\r\n case CrudRequestWhereOperator.EQ:\r\n return { where: `${field} = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ:\r\n return { where: `${field} != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GT:\r\n return { where: `${field} > :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.GTE:\r\n return { where: `${field} >= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LT:\r\n return { where: `${field} < :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.LTE:\r\n return { where: `${field} <= :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS:\r\n return { where: `${field} LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS:\r\n return { where: `${field} NOT LIKE :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN:\r\n value = ensureArray('IN operator', value, 1);\r\n\r\n return { where: `${field} IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN:\r\n value = ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `${field} NOT IN (:...${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.BETWEEN:\r\n const arr = ensureArray('BETWEEN operator', value, 2);\r\n\r\n return {\r\n where: `${field} BETWEEN :${param}_start AND :${param}_end`,\r\n params: { [`${param}_start`]: arr[0], [`${param}_end`]: arr[1] },\r\n };\r\n\r\n case CrudRequestWhereOperator.IS_NULL:\r\n ensureFalsy('IS NULL operator', value);\r\n\r\n return { where: `${field} IS NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.NOT_NULL:\r\n ensureFalsy('NOT NULL operator', value);\r\n\r\n return { where: `${field} IS NOT NULL`, params: {} };\r\n\r\n case CrudRequestWhereOperator.EQ_LOWER:\r\n return { where: `LOWER(${field}) = :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NEQ_LOWER:\r\n return { where: `LOWER(${field}) != :${param}`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.STARTS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `${value}%` } };\r\n\r\n case CrudRequestWhereOperator.ENDS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `%${value}` } };\r\n\r\n case CrudRequestWhereOperator.CONTAINS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field)} :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.NOT_CONTAINS_LOWER:\r\n return { where: `${this.createLowerLike(isILikeEnabled, field, true)} :${param}`, params: { [param]: `%${value}%` } };\r\n\r\n case CrudRequestWhereOperator.IN_LOWER:\r\n ensureArray('IN operator', value, 1);\r\n\r\n return { where: `LOWER(${field}) IN (...:${param})`, params: { [param]: value } };\r\n\r\n case CrudRequestWhereOperator.NOT_IN_LOWER:\r\n ensureArray('NOT IN operator', value, 1);\r\n\r\n return { where: `LOWER(${field}) NOT IN (...:${param})`, params: { [param]: value } };\r\n\r\n default:\r\n throw new Error(`Unknown operator \"${operator}\"`);\r\n }\r\n }\r\n\r\n protected createLowerLike(isILikeEnabled: boolean, field: string, not: boolean = false): string {\r\n if (isILikeEnabled)\r\n return not ? `${field} NOT ILIKE` : `${field} ILIKE`;\r\n\r\n return not ? `LOWER(${field}) NOT LIKE` : `LOWER(${field}) LIKE`;\r\n }\r\n\r\n protected isILikeEnabled(qb: SelectQueryBuilder<any>): boolean {\r\n const ilikeEnabled = this.options.ilike;\r\n\r\n if (isValid(ilikeEnabled))\r\n return ilikeEnabled;\r\n\r\n const type = qb.connection.options.type;\r\n return type === 'postgres' || type === 'aurora-postgres';\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","/**\r\n * Checks whether two field paths are equal\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] is equal to [\"path\", \"to\", \"field\"] but not [\"something\", \"else\"]\r\n */\r\nexport function pathEquals(path1: string[], path2: string[]): boolean {\r\n if (path1.length !== path2.length)\r\n return false;\r\n\r\n return path1.every((p1, i) => path2[i] === p1);\r\n}\r\n\r\n/**\r\n * Checks whether a path starts with another path.\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] starts with [\"path\"] or [\"path\", \"to\"] but not [\"something\", \"else\"]\r\n */\r\nexport function pathStartsWith(path: string[], start: string[]): boolean {\r\n if (path.length < start.length)\r\n return false;\r\n\r\n return start.every((start, i) => path[i] === start);\r\n}\r\n\r\n/**\r\n * Checks whether the base of a path matches.\r\n *\r\n * E.g. [\"path\", \"to\", \"field\"] has a base of [\"path\", \"to\"] but not [\"path\"]\r\n */\r\nexport function pathHasBase(path: string[], base: string[]): boolean {\r\n if (path.length - 1 !== base.length)\r\n return false;\r\n\r\n return base.every((start, i) => path[i] === start);\r\n}\r\n"],"mappings":"+EAAA,OAAS,YAAAA,MAA2E,UC8B7E,SAASC,EAAeC,EAAmBC,EAAiBC,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQD,CAAI,GAAKA,EAAK,OAASC,EACxC,MAAM,IAAI,MAAM,GAAGF,CAAS,mCAAmCE,CAAS,QAAQ,EAElF,OAAOD,CACT,CALgBE,EAAAJ,EAAA,eAOT,SAASK,EAAYJ,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBG,EAAAC,EAAA,eAKT,SAASC,EAAWC,EAAyC,CAClE,OAAOA,GAAU,IACnB,CAFgBH,EAAAE,EAAA,WCbT,SAASE,EAAYC,EAAgBC,EAAyB,CACnE,OAAID,EAAK,OAAS,IAAMC,EAAK,OACpB,GAEFA,EAAK,MAAM,CAACC,EAAOC,IAAMH,EAAKG,CAAC,IAAMD,CAAK,CACnD,CALgBE,EAAAL,EAAA,eFTT,IAAMM,EAAN,KAA0F,CAE/F,YACmBC,EAAsC,CAAC,EACxD,CADiB,aAAAA,CAChB,CAxBL,MAoBiG,CAAAC,EAAA,4BASxF,MAA+BC,EAA2BC,EAA2C,CAC1G,OAAAD,EAAK,KAAK,gBAAgBA,EAAIC,CAAK,EACnCD,EAAK,KAAK,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Q,EAAM,SAASR,CAAK,aAAe,SAASA,CAAK,QAC1D,CAEU,eAAehC,EAAsC,CAC7D,IAAMyC,EAAe,KAAK,QAAQ,MAElC,GAAIC,EAAQD,CAAY,EACtB,OAAOA,EAET,IAAME,EAAO3C,EAAG,WAAW,QAAQ,KACnC,OAAO2C,IAAS,YAAcA,IAAS,iBACzC,CAEF","names":["Brackets","ensureArray","fieldName","data","minLength","__name","ensureFalsy","isValid","value","pathHasBase","path","base","start","i","__name","TypeOrmQueryAdapter","options","__name","qb","query","request","fullQuery","data","total","offset","limit","count","page","pageCount","paramsDefined","isILikeEnabled","select","fields","f","s","relations","relation","path","alias","pathHasBase","ordering","order","where","or","params","addWhere","Brackets","wqb","item","param","field","name","iteration","operator","value","ensureArray","arr","ensureFalsy","not","ilikeEnabled","isValid","type"]}
|
package/dist/{parsed-request-where.builder-DME55XmN.d.ts → crud-request-where.builder-D60W-QEd.d.ts}
RENAMED
@@ -3,18 +3,18 @@ import { e as CrudRequestWhereAND, f as CrudRequestWhereOR, i as CrudRequestWher
|
|
3
3
|
/**
|
4
4
|
* A helper class that makes it easier to create a CrudRequestWhere
|
5
5
|
*/
|
6
|
-
declare class
|
6
|
+
declare class CrudRequestWhereBuilder {
|
7
7
|
private readonly where;
|
8
8
|
private readonly parent?;
|
9
|
-
constructor(where?: CrudRequestWhereAND | CrudRequestWhereOR, parent?:
|
9
|
+
constructor(where?: CrudRequestWhereAND | CrudRequestWhereOR, parent?: CrudRequestWhereBuilder | undefined);
|
10
10
|
/**
|
11
11
|
* Adds an AND bracket
|
12
12
|
*/
|
13
|
-
addAnd():
|
13
|
+
addAnd(): CrudRequestWhereBuilder;
|
14
14
|
/**
|
15
15
|
* Adds an OR bracket
|
16
16
|
*/
|
17
|
-
addOr():
|
17
|
+
addOr(): CrudRequestWhereBuilder;
|
18
18
|
/**
|
19
19
|
* Adds a field comparison
|
20
20
|
*
|
@@ -22,11 +22,11 @@ declare class ParsedRequestWhereBuilder {
|
|
22
22
|
* @param operator The comparison operator
|
23
23
|
* @param value The value to compare
|
24
24
|
*/
|
25
|
-
addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType):
|
25
|
+
addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType | CrudRequestWhereValueType[]): CrudRequestWhereBuilder;
|
26
26
|
/**
|
27
27
|
* Constructs the final where condition
|
28
28
|
*/
|
29
29
|
build(): CrudRequestWhere;
|
30
30
|
}
|
31
31
|
|
32
|
-
export {
|
32
|
+
export { CrudRequestWhereBuilder as C };
|
@@ -3,18 +3,18 @@ import { e as CrudRequestWhereAND, f as CrudRequestWhereOR, i as CrudRequestWher
|
|
3
3
|
/**
|
4
4
|
* A helper class that makes it easier to create a CrudRequestWhere
|
5
5
|
*/
|
6
|
-
declare class
|
6
|
+
declare class CrudRequestWhereBuilder {
|
7
7
|
private readonly where;
|
8
8
|
private readonly parent?;
|
9
|
-
constructor(where?: CrudRequestWhereAND | CrudRequestWhereOR, parent?:
|
9
|
+
constructor(where?: CrudRequestWhereAND | CrudRequestWhereOR, parent?: CrudRequestWhereBuilder | undefined);
|
10
10
|
/**
|
11
11
|
* Adds an AND bracket
|
12
12
|
*/
|
13
|
-
addAnd():
|
13
|
+
addAnd(): CrudRequestWhereBuilder;
|
14
14
|
/**
|
15
15
|
* Adds an OR bracket
|
16
16
|
*/
|
17
|
-
addOr():
|
17
|
+
addOr(): CrudRequestWhereBuilder;
|
18
18
|
/**
|
19
19
|
* Adds a field comparison
|
20
20
|
*
|
@@ -22,11 +22,11 @@ declare class ParsedRequestWhereBuilder {
|
|
22
22
|
* @param operator The comparison operator
|
23
23
|
* @param value The value to compare
|
24
24
|
*/
|
25
|
-
addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType):
|
25
|
+
addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType | CrudRequestWhereValueType[]): CrudRequestWhereBuilder;
|
26
26
|
/**
|
27
27
|
* Constructs the final where condition
|
28
28
|
*/
|
29
29
|
build(): CrudRequestWhere;
|
30
30
|
}
|
31
31
|
|
32
|
-
export {
|
32
|
+
export { CrudRequestWhereBuilder as C };
|
package/dist/index.d.mts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
export { G as GetManyResult, Q as QueryAdapter } from './query-adapter-BliD9hJN.mjs';
|
2
2
|
export { R as RequestParamValue, a as RequestParser } from './request-parser-WaQBZsYG.mjs';
|
3
3
|
export { c as CrudRequest, C as CrudRequestFields, b as CrudRequestOrder, a as CrudRequestRelation, d as CrudRequestWhere, e as CrudRequestWhereAND, g as CrudRequestWhereField, f as CrudRequestWhereOR, i as CrudRequestWhereOperator, h as CrudRequestWhereValueType, F as FieldPath, P as ParsedRequestSelect } from './crud-request-CvDKp6Iy.mjs';
|
4
|
-
export {
|
4
|
+
export { C as CrudRequestWhereBuilder } from './crud-request-where.builder-mP8y-fQp.mjs';
|
package/dist/index.d.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
export { G as GetManyResult, Q as QueryAdapter } from './query-adapter-Vebxws3V.js';
|
2
2
|
export { R as RequestParamValue, a as RequestParser } from './request-parser-B-fK5GnP.js';
|
3
3
|
export { c as CrudRequest, C as CrudRequestFields, b as CrudRequestOrder, a as CrudRequestRelation, d as CrudRequestWhere, e as CrudRequestWhereAND, g as CrudRequestWhereField, f as CrudRequestWhereOR, i as CrudRequestWhereOperator, h as CrudRequestWhereValueType, F as FieldPath, P as ParsedRequestSelect } from './crud-request-CvDKp6Iy.js';
|
4
|
-
export {
|
4
|
+
export { C as CrudRequestWhereBuilder } from './crud-request-where.builder-D60W-QEd.js';
|
package/dist/index.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";var l=Object.defineProperty;var
|
1
|
+
"use strict";var l=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var o=(r,e)=>l(r,"name",{value:e,configurable:!0});var W=(r,e)=>{for(var n in e)l(r,n,{get:e[n],enumerable:!0})},V=(r,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let u of d(e))!E.call(r,u)&&u!==n&&l(r,u,{get:()=>e[u],enumerable:!(i=R(e,u))||i.enumerable});return r};var y=r=>V(l({},"__esModule",{value:!0}),r);var O={};W(O,{CrudRequestWhereBuilder:()=>p,CrudRequestWhereOperator:()=>a});module.exports=y(O);var a=(t=>(t.EQ="eq",t.NEQ="neq",t.GT="gt",t.GTE="gte",t.LT="lt",t.LTE="lte",t.STARTS="starts",t.ENDS="ends",t.CONTAINS="contains",t.NOT_CONTAINS="not_contains",t.IN="in",t.NOT_IN="not_in",t.BETWEEN="between",t.IS_NULL="is_null",t.NOT_NULL="not_null",t.EQ_LOWER="eq_lower",t.NEQ_LOWER="neq_lower",t.STARTS_LOWER="starts_lower",t.ENDS_LOWER="ends_lower",t.CONTAINS_LOWER="contains_lower",t.NOT_CONTAINS_LOWER="not_contains_lower",t.IN_LOWER="in_lower",t.NOT_IN_LOWER="not_in_lower",t))(a||{});function T(r,e){if(e!=null&&!(typeof e=="number"||typeof e=="string"||typeof e=="boolean")&&!(e instanceof Date))throw new Error(`${r} must be a string, number, boolean or null`)}o(T,"ensurePrimitive");function s(r,e,n=0){if(!Array.isArray(e)||e.length<n)throw new Error(`${r} must be an array with at least ${n} items`);return e}o(s,"ensureArray");function h(r,e){if(e)throw new Error(`${r} must be null`)}o(h,"ensureFalsy");var N={eq:"primitive",neq:"primitive",gt:"primitive",lt:"primitive",gte:"primitive",lte:"primitive",starts:"primitive",ends:"primitive",contains:"primitive",not_contains:"primitive",in:"array",not_in:"array",between:"array",is_null:"empty",not_null:"empty",eq_lower:"primitive",neq_lower:"primitive",starts_lower:"primitive",ends_lower:"primitive",contains_lower:"primitive",not_contains_lower:"primitive",in_lower:"primitive",not_in_lower:"primitive"};function I(r){let e=N[r.operator],n="The value of the operator "+r.operator;if(e==="primitive"){T(n,r.value);return}if(e==="array"){s(n,r.value).forEach(u=>T(n+" children",u));return}if(e==="empty"){h(n,r.value);return}}o(I,"validateWhereField");var p=class r{constructor(e={and:[]},n){this.where=e;this.parent=n}static{o(this,"CrudRequestWhereBuilder")}addAnd(){if(!this.where.or)return this;let e={and:[]},n=new r(e,this);return this.where.or.push(e),n}addOr(){if(!this.where.and)return this;let e={or:[]},n=new r(e,this);return this.where.and.push(e),n}addField(e,n,i){let u={field:e,operator:n,value:i};if(I(u),this.where.and)this.where.and.push(u);else if(this.where.or)this.where.or.push(u);else throw new Error("Invalid where");return this}build(){return this.parent?this.parent.build():this.where}};0&&(module.exports={CrudRequestWhereBuilder,CrudRequestWhereOperator});
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/models/crud-request-where.ts","../src/utils/parsed-request-where.builder.ts"],"sourcesContent":["// Models\r\nexport * from './models/query-adapter';\r\nexport * from './models/request-parser';\r\n\r\nexport * from './models/get-many-result';\r\nexport * from './models/field-path';\r\nexport * from './models/crud-request';\r\nexport * from './models/crud-request-where';\r\n\r\n// Utilities\r\nexport * from './utils/parsed-request-where.builder';\r\n\r\n","import { FieldPath } from './field-path';\r\n\r\nexport type CrudRequestWhere = CrudRequestWhereAND | CrudRequestWhereOR | CrudRequestWhereField;\r\n\r\nexport interface CrudRequestWhereAND {\r\n field?: never;\r\n or?: never;\r\n and: CrudRequestWhere[];\r\n}\r\n\r\nexport interface CrudRequestWhereOR {\r\n field?: never;\r\n or: CrudRequestWhere[];\r\n and?: never;\r\n}\r\n\r\nexport interface CrudRequestWhereField {\r\n\r\n /**\r\n * Field path\r\n *\r\n * For post.category.name, this would be [\"post\", \"category\", \"name\"]\r\n */\r\n field: FieldPath;\r\n\r\n /**\r\n * The operator of the comparison\r\n */\r\n operator: CrudRequestWhereOperator;\r\n\r\n /**\r\n * The value to compare\r\n */\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[];\r\n\r\n or?: never;\r\n and?: never;\r\n}\r\n\r\nexport type CrudRequestWhereValueType = string | number | boolean | Date | null | undefined;\r\n\r\nexport enum CrudRequestWhereOperator {\r\n EQ = 'eq',\r\n NEQ = 'neq',\r\n GT = 'gt',\r\n GTE = 'gte',\r\n LT = 'lt',\r\n LTE = 'lte',\r\n STARTS = 'starts',\r\n ENDS = 'ends',\r\n CONTAINS = 'contains',\r\n NOT_CONTAINS = 'not_contains',\r\n IN = 'in',\r\n NOT_IN = 'not_in',\r\n BETWEEN = 'between',\r\n IS_NULL = 'is_null',\r\n NOT_NULL = 'not_null',\r\n EQ_LOWER = 'eq_lower',\r\n NEQ_LOWER = 'neq_lower',\r\n STARTS_LOWER = 'starts_lower',\r\n ENDS_LOWER = 'ends_lower',\r\n CONTAINS_LOWER = 'contains_lower',\r\n NOT_CONTAINS_LOWER = 'not_contains_lower',\r\n IN_LOWER = 'in_lower',\r\n NOT_IN_LOWER = 'not_in_lower',\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class ParsedRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: ParsedRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): ParsedRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): ParsedRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType): ParsedRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,8BAAAE,EAAA,8BAAAC,IAAA,eAAAC,EAAAJ,GCyCO,IAAKK,OACVA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,OAAS,SACTA,EAAA,KAAO,OACPA,EAAA,SAAW,WACXA,EAAA,aAAe,eACfA,EAAA,GAAK,KACLA,EAAA,OAAS,SACTA,EAAA,QAAU,UACVA,EAAA,QAAU,UACVA,EAAA,SAAW,WACXA,EAAA,SAAW,WACXA,EAAA,UAAY,YACZA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,eAAiB,iBACjBA,EAAA,mBAAqB,qBACrBA,EAAA,SAAW,WACXA,EAAA,aAAe,eAvBLA,OAAA,IC7BL,IAAMC,EAAN,MAAMC,CAA0B,CAErC,YACmBC,EAAkD,CAAE,IAAK,CAAC,CAAE,EAC5DC,EACjB,CAFiB,WAAAD,EACA,YAAAC,CACf,CAjBN,MAYuC,CAAAC,EAAA,kCAU9B,QAAoC,CACzC,GAAI,CAAC,KAAK,MAAM,GACd,OAAO,KAGT,IAAMC,EAA8B,CAAE,IAAK,CAAC,CAAE,EACxCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,GAAG,KAAKA,CAAM,EAElBC,CACT,CAKO,OAAmC,CACxC,GAAI,CAAC,KAAK,MAAM,IACd,OAAO,KAGT,IAAMD,EAA6B,CAAE,GAAI,CAAC,CAAE,EACtCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,IAAI,KAAKA,CAAM,EAEnBC,CACT,CASO,SAASC,EAAiBC,EAAoCC,EAA6D,CAChI,IAAMC,EAAoC,CACxC,MAAAH,EACA,SAAAC,EACA,MAAAC,CACF,EAEA,GAAI,KAAK,MAAM,IACb,KAAK,MAAM,IAAI,KAAKC,CAAU,UACrB,KAAK,MAAM,GACpB,KAAK,MAAM,GAAG,KAAKA,CAAU,MAE7B,OAAM,IAAI,MAAM,eAAe,EAGjC,OAAO,IACT,CAKO,OAA0B,CAC/B,OAAI,KAAK,OACA,KAAK,OAAO,MAAM,EAGpB,KAAK,KACd,CAEF","names":["src_exports","__export","CrudRequestWhereOperator","ParsedRequestWhereBuilder","__toCommonJS","CrudRequestWhereOperator","ParsedRequestWhereBuilder","_ParsedRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField"]}
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/models/crud-request-where.ts","../src/utils/functions.ts","../src/utils/where.ts","../src/utils/crud-request-where.builder.ts"],"sourcesContent":["// Models\r\nexport * from './models/query-adapter';\r\nexport * from './models/request-parser';\r\n\r\nexport * from './models/get-many-result';\r\nexport * from './models/field-path';\r\nexport * from './models/crud-request';\r\nexport * from './models/crud-request-where';\r\n\r\n// Utilities\r\nexport * from './utils/crud-request-where.builder';\r\n\r\n","import { FieldPath } from './field-path';\r\n\r\nexport type CrudRequestWhere = CrudRequestWhereAND | CrudRequestWhereOR | CrudRequestWhereField;\r\n\r\nexport interface CrudRequestWhereAND {\r\n field?: never;\r\n or?: never;\r\n and: CrudRequestWhere[];\r\n}\r\n\r\nexport interface CrudRequestWhereOR {\r\n field?: never;\r\n or: CrudRequestWhere[];\r\n and?: never;\r\n}\r\n\r\nexport interface CrudRequestWhereField {\r\n\r\n /**\r\n * Field path\r\n *\r\n * For post.category.name, this would be [\"post\", \"category\", \"name\"]\r\n */\r\n field: FieldPath;\r\n\r\n /**\r\n * The operator of the comparison\r\n */\r\n operator: CrudRequestWhereOperator;\r\n\r\n /**\r\n * The value to compare\r\n */\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[];\r\n\r\n or?: never;\r\n and?: never;\r\n}\r\n\r\nexport type CrudRequestWhereValueType = string | number | boolean | Date | null | undefined;\r\n\r\nexport enum CrudRequestWhereOperator {\r\n EQ = 'eq',\r\n NEQ = 'neq',\r\n GT = 'gt',\r\n GTE = 'gte',\r\n LT = 'lt',\r\n LTE = 'lte',\r\n STARTS = 'starts',\r\n ENDS = 'ends',\r\n CONTAINS = 'contains',\r\n NOT_CONTAINS = 'not_contains',\r\n IN = 'in',\r\n NOT_IN = 'not_in',\r\n BETWEEN = 'between',\r\n IS_NULL = 'is_null',\r\n NOT_NULL = 'not_null',\r\n EQ_LOWER = 'eq_lower',\r\n NEQ_LOWER = 'neq_lower',\r\n STARTS_LOWER = 'starts_lower',\r\n ENDS_LOWER = 'ends_lower',\r\n CONTAINS_LOWER = 'contains_lower',\r\n NOT_CONTAINS_LOWER = 'not_contains_lower',\r\n IN_LOWER = 'in_lower',\r\n NOT_IN_LOWER = 'not_in_lower',\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { CrudRequestWhereField, CrudRequestWhereOperator } from '../models/crud-request-where';\r\nimport { ensureArray, ensureFalsy, ensurePrimitive } from './functions';\r\n\r\nexport enum WhereOperatorValueType {\r\n PRIMITIVE = 'primitive',\r\n ARRAY = 'array',\r\n EMPTY = 'empty'\r\n}\r\n\r\nconst operatorValueTypes: Record<CrudRequestWhereOperator, WhereOperatorValueType> = {\r\n [CrudRequestWhereOperator.EQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.NOT_IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.BETWEEN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.IS_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.NOT_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.EQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n};\r\n\r\nexport function getWhereOperatorValueType(op: CrudRequestWhereOperator): WhereOperatorValueType {\r\n return operatorValueTypes[op];\r\n}\r\n\r\nexport function validateWhereField(where: CrudRequestWhereField): void {\r\n const type = operatorValueTypes[where.operator];\r\n const name = 'The value of the operator ' + where.operator;\r\n\r\n if (type === WhereOperatorValueType.PRIMITIVE) {\r\n ensurePrimitive(name, where.value);\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.ARRAY) {\r\n const items = ensureArray(name, where.value);\r\n\r\n items.forEach(item => ensurePrimitive(name + ' children', item));\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.EMPTY) {\r\n ensureFalsy(name, where.value);\r\n\r\n return;\r\n }\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\nimport { validateWhereField } from './where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class CrudRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: CrudRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): CrudRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): CrudRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(\r\n field: string[],\r\n operator: CrudRequestWhereOperator,\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[],\r\n ): CrudRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n validateWhereField(whereField);\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,6BAAAE,EAAA,6BAAAC,IAAA,eAAAC,EAAAJ,GCyCO,IAAKK,OACVA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,OAAS,SACTA,EAAA,KAAO,OACPA,EAAA,SAAW,WACXA,EAAA,aAAe,eACfA,EAAA,GAAK,KACLA,EAAA,OAAS,SACTA,EAAA,QAAU,UACVA,EAAA,QAAU,UACVA,EAAA,SAAW,WACXA,EAAA,SAAW,WACXA,EAAA,UAAY,YACZA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,eAAiB,iBACjBA,EAAA,mBAAqB,qBACrBA,EAAA,SAAW,WACXA,EAAA,aAAe,eAvBLA,OAAA,ICxBL,SAASC,EAAgBC,EAAmBC,EAAW,CAC5D,GAAIA,GAAS,MAGT,SAAOA,GAAS,UAAY,OAAOA,GAAS,UAAY,OAAOA,GAAS,YAGxE,EAAAA,aAAgB,MAGpB,MAAM,IAAI,MAAM,GAAGD,CAAS,4CAA4C,CAC1E,CAXgBE,EAAAH,EAAA,mBAaT,SAASI,EAAeH,EAAmBC,EAAiBG,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQH,CAAI,GAAKA,EAAK,OAASG,EACxC,MAAM,IAAI,MAAM,GAAGJ,CAAS,mCAAmCI,CAAS,QAAQ,EAElF,OAAOH,CACT,CALgBC,EAAAC,EAAA,eAOT,SAASE,EAAYL,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBE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AAwBI,EAAQ,IAAI,EAExD,YAAK,MAAM,IAAI,KAAKA,CAAM,EAEnBC,CACT,CASO,SACLC,EACAC,EACAC,EACyB,CACzB,IAAMC,EAAoC,CACxC,MAAAH,EACA,SAAAC,EACA,MAAAC,CACF,EAIA,GAFAE,EAAmBD,CAAU,EAEzB,KAAK,MAAM,IACb,KAAK,MAAM,IAAI,KAAKA,CAAU,UACrB,KAAK,MAAM,GACpB,KAAK,MAAM,GAAG,KAAKA,CAAU,MAE7B,OAAM,IAAI,MAAM,eAAe,EAGjC,OAAO,IACT,CAKO,OAA0B,CAC/B,OAAI,KAAK,OACA,KAAK,OAAO,MAAM,EAGpB,KAAK,KACd,CAEF","names":["src_exports","__export","CrudRequestWhereBuilder","CrudRequestWhereOperator","__toCommonJS","CrudRequestWhereOperator","ensurePrimitive","fieldName","data","__name","ensureArray","minLength","ensureFalsy","operatorValueTypes","validateWhereField","where","type","operatorValueTypes","name","ensurePrimitive","ensureArray","item","ensureFalsy","__name","CrudRequestWhereBuilder","_CrudRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","validateWhereField"]}
|
package/dist/index.mjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
var
|
1
|
+
var I=Object.defineProperty;var u=(t,e)=>I(t,"name",{value:e,configurable:!0});var a=(r=>(r.EQ="eq",r.NEQ="neq",r.GT="gt",r.GTE="gte",r.LT="lt",r.LTE="lte",r.STARTS="starts",r.ENDS="ends",r.CONTAINS="contains",r.NOT_CONTAINS="not_contains",r.IN="in",r.NOT_IN="not_in",r.BETWEEN="between",r.IS_NULL="is_null",r.NOT_NULL="not_null",r.EQ_LOWER="eq_lower",r.NEQ_LOWER="neq_lower",r.STARTS_LOWER="starts_lower",r.ENDS_LOWER="ends_lower",r.CONTAINS_LOWER="contains_lower",r.NOT_CONTAINS_LOWER="not_contains_lower",r.IN_LOWER="in_lower",r.NOT_IN_LOWER="not_in_lower",r))(a||{});function i(t,e){if(e!=null&&!(typeof e=="number"||typeof e=="string"||typeof e=="boolean")&&!(e instanceof Date))throw new Error(`${t} must be a string, number, boolean or null`)}u(i,"ensurePrimitive");function T(t,e,n=0){if(!Array.isArray(e)||e.length<n)throw new Error(`${t} must be an array with at least ${n} items`);return e}u(T,"ensureArray");function p(t,e){if(e)throw new Error(`${t} must be null`)}u(p,"ensureFalsy");var R={eq:"primitive",neq:"primitive",gt:"primitive",lt:"primitive",gte:"primitive",lte:"primitive",starts:"primitive",ends:"primitive",contains:"primitive",not_contains:"primitive",in:"array",not_in:"array",between:"array",is_null:"empty",not_null:"empty",eq_lower:"primitive",neq_lower:"primitive",starts_lower:"primitive",ends_lower:"primitive",contains_lower:"primitive",not_contains_lower:"primitive",in_lower:"primitive",not_in_lower:"primitive"};function s(t){let e=R[t.operator],n="The value of the operator "+t.operator;if(e==="primitive"){i(n,t.value);return}if(e==="array"){T(n,t.value).forEach(o=>i(n+" children",o));return}if(e==="empty"){p(n,t.value);return}}u(s,"validateWhereField");var h=class t{constructor(e={and:[]},n){this.where=e;this.parent=n}static{u(this,"CrudRequestWhereBuilder")}addAnd(){if(!this.where.or)return this;let e={and:[]},n=new t(e,this);return this.where.or.push(e),n}addOr(){if(!this.where.and)return this;let e={or:[]},n=new t(e,this);return this.where.and.push(e),n}addField(e,n,l){let o={field:e,operator:n,value:l};if(s(o),this.where.and)this.where.and.push(o);else if(this.where.or)this.where.or.push(o);else throw new Error("Invalid where");return this}build(){return this.parent?this.parent.build():this.where}};export{h as CrudRequestWhereBuilder,a as CrudRequestWhereOperator};
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/models/crud-request-where.ts","../src/utils/parsed-request-where.builder.ts"],"sourcesContent":["import { FieldPath } from './field-path';\r\n\r\nexport type CrudRequestWhere = CrudRequestWhereAND | CrudRequestWhereOR | CrudRequestWhereField;\r\n\r\nexport interface CrudRequestWhereAND {\r\n field?: never;\r\n or?: never;\r\n and: CrudRequestWhere[];\r\n}\r\n\r\nexport interface CrudRequestWhereOR {\r\n field?: never;\r\n or: CrudRequestWhere[];\r\n and?: never;\r\n}\r\n\r\nexport interface CrudRequestWhereField {\r\n\r\n /**\r\n * Field path\r\n *\r\n * For post.category.name, this would be [\"post\", \"category\", \"name\"]\r\n */\r\n field: FieldPath;\r\n\r\n /**\r\n * The operator of the comparison\r\n */\r\n operator: CrudRequestWhereOperator;\r\n\r\n /**\r\n * The value to compare\r\n */\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[];\r\n\r\n or?: never;\r\n and?: never;\r\n}\r\n\r\nexport type CrudRequestWhereValueType = string | number | boolean | Date | null | undefined;\r\n\r\nexport enum CrudRequestWhereOperator {\r\n EQ = 'eq',\r\n NEQ = 'neq',\r\n GT = 'gt',\r\n GTE = 'gte',\r\n LT = 'lt',\r\n LTE = 'lte',\r\n STARTS = 'starts',\r\n ENDS = 'ends',\r\n CONTAINS = 'contains',\r\n NOT_CONTAINS = 'not_contains',\r\n IN = 'in',\r\n NOT_IN = 'not_in',\r\n BETWEEN = 'between',\r\n IS_NULL = 'is_null',\r\n NOT_NULL = 'not_null',\r\n EQ_LOWER = 'eq_lower',\r\n NEQ_LOWER = 'neq_lower',\r\n STARTS_LOWER = 'starts_lower',\r\n ENDS_LOWER = 'ends_lower',\r\n CONTAINS_LOWER = 'contains_lower',\r\n NOT_CONTAINS_LOWER = 'not_contains_lower',\r\n IN_LOWER = 'in_lower',\r\n NOT_IN_LOWER = 'not_in_lower',\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class ParsedRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: ParsedRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): ParsedRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): ParsedRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType): ParsedRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n"],"mappings":"+EAyCO,IAAKA,OACVA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,OAAS,SACTA,EAAA,KAAO,OACPA,EAAA,SAAW,WACXA,EAAA,aAAe,eACfA,EAAA,GAAK,KACLA,EAAA,OAAS,SACTA,EAAA,QAAU,UACVA,EAAA,QAAU,UACVA,EAAA,SAAW,WACXA,EAAA,SAAW,WACXA,EAAA,UAAY,YACZA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,eAAiB,iBACjBA,EAAA,mBAAqB,qBACrBA,EAAA,SAAW,WACXA,EAAA,aAAe,eAvBLA,OAAA,IC7BL,IAAMC,EAAN,MAAMC,CAA0B,CAErC,YACmBC,EAAkD,CAAE,IAAK,CAAC,CAAE,EAC5DC,EACjB,CAFiB,WAAAD,EACA,YAAAC,CACf,CAjBN,MAYuC,CAAAC,EAAA,kCAU9B,QAAoC,CACzC,GAAI,CAAC,KAAK,MAAM,GACd,OAAO,KAGT,IAAMC,EAA8B,CAAE,IAAK,CAAC,CAAE,EACxCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,GAAG,KAAKA,CAAM,EAElBC,CACT,CAKO,OAAmC,CACxC,GAAI,CAAC,KAAK,MAAM,IACd,OAAO,KAGT,IAAMD,EAA6B,CAAE,GAAI,CAAC,CAAE,EACtCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,IAAI,KAAKA,CAAM,EAEnBC,CACT,CASO,SAASC,EAAiBC,EAAoCC,EAA6D,CAChI,IAAMC,EAAoC,CACxC,MAAAH,EACA,SAAAC,EACA,MAAAC,CACF,EAEA,GAAI,KAAK,MAAM,IACb,KAAK,MAAM,IAAI,KAAKC,CAAU,UACrB,KAAK,MAAM,GACpB,KAAK,MAAM,GAAG,KAAKA,CAAU,MAE7B,OAAM,IAAI,MAAM,eAAe,EAGjC,OAAO,IACT,CAKO,OAA0B,CAC/B,OAAI,KAAK,OACA,KAAK,OAAO,MAAM,EAGpB,KAAK,KACd,CAEF","names":["CrudRequestWhereOperator","ParsedRequestWhereBuilder","_ParsedRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField"]}
|
1
|
+
{"version":3,"sources":["../src/models/crud-request-where.ts","../src/utils/functions.ts","../src/utils/where.ts","../src/utils/crud-request-where.builder.ts"],"sourcesContent":["import { FieldPath } from './field-path';\r\n\r\nexport type CrudRequestWhere = CrudRequestWhereAND | CrudRequestWhereOR | CrudRequestWhereField;\r\n\r\nexport interface CrudRequestWhereAND {\r\n field?: never;\r\n or?: never;\r\n and: CrudRequestWhere[];\r\n}\r\n\r\nexport interface CrudRequestWhereOR {\r\n field?: never;\r\n or: CrudRequestWhere[];\r\n and?: never;\r\n}\r\n\r\nexport interface CrudRequestWhereField {\r\n\r\n /**\r\n * Field path\r\n *\r\n * For post.category.name, this would be [\"post\", \"category\", \"name\"]\r\n */\r\n field: FieldPath;\r\n\r\n /**\r\n * The operator of the comparison\r\n */\r\n operator: CrudRequestWhereOperator;\r\n\r\n /**\r\n * The value to compare\r\n */\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[];\r\n\r\n or?: never;\r\n and?: never;\r\n}\r\n\r\nexport type CrudRequestWhereValueType = string | number | boolean | Date | null | undefined;\r\n\r\nexport enum CrudRequestWhereOperator {\r\n EQ = 'eq',\r\n NEQ = 'neq',\r\n GT = 'gt',\r\n GTE = 'gte',\r\n LT = 'lt',\r\n LTE = 'lte',\r\n STARTS = 'starts',\r\n ENDS = 'ends',\r\n CONTAINS = 'contains',\r\n NOT_CONTAINS = 'not_contains',\r\n IN = 'in',\r\n NOT_IN = 'not_in',\r\n BETWEEN = 'between',\r\n IS_NULL = 'is_null',\r\n NOT_NULL = 'not_null',\r\n EQ_LOWER = 'eq_lower',\r\n NEQ_LOWER = 'neq_lower',\r\n STARTS_LOWER = 'starts_lower',\r\n ENDS_LOWER = 'ends_lower',\r\n CONTAINS_LOWER = 'contains_lower',\r\n NOT_CONTAINS_LOWER = 'not_contains_lower',\r\n IN_LOWER = 'in_lower',\r\n NOT_IN_LOWER = 'not_in_lower',\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { CrudRequestWhereField, CrudRequestWhereOperator } from '../models/crud-request-where';\r\nimport { ensureArray, ensureFalsy, ensurePrimitive } from './functions';\r\n\r\nexport enum WhereOperatorValueType {\r\n PRIMITIVE = 'primitive',\r\n ARRAY = 'array',\r\n EMPTY = 'empty'\r\n}\r\n\r\nconst operatorValueTypes: Record<CrudRequestWhereOperator, WhereOperatorValueType> = {\r\n [CrudRequestWhereOperator.EQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.NOT_IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.BETWEEN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.IS_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.NOT_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.EQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n};\r\n\r\nexport function getWhereOperatorValueType(op: CrudRequestWhereOperator): WhereOperatorValueType {\r\n return operatorValueTypes[op];\r\n}\r\n\r\nexport function validateWhereField(where: CrudRequestWhereField): void {\r\n const type = operatorValueTypes[where.operator];\r\n const name = 'The value of the operator ' + where.operator;\r\n\r\n if (type === WhereOperatorValueType.PRIMITIVE) {\r\n ensurePrimitive(name, where.value);\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.ARRAY) {\r\n const items = ensureArray(name, where.value);\r\n\r\n items.forEach(item => ensurePrimitive(name + ' children', item));\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.EMPTY) {\r\n ensureFalsy(name, where.value);\r\n\r\n return;\r\n }\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\nimport { validateWhereField } from './where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class CrudRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: CrudRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): CrudRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): CrudRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(\r\n field: string[],\r\n operator: CrudRequestWhereOperator,\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[],\r\n ): CrudRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n validateWhereField(whereField);\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n"],"mappings":"+EAyCO,IAAKA,OACVA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,GAAK,KACLA,EAAA,IAAM,MACNA,EAAA,OAAS,SACTA,EAAA,KAAO,OACPA,EAAA,SAAW,WACXA,EAAA,aAAe,eACfA,EAAA,GAAK,KACLA,EAAA,OAAS,SACTA,EAAA,QAAU,UACVA,EAAA,QAAU,UACVA,EAAA,SAAW,WACXA,EAAA,SAAW,WACXA,EAAA,UAAY,YACZA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,eAAiB,iBACjBA,EAAA,mBAAqB,qBACrBA,EAAA,SAAW,WACXA,EAAA,aAAe,eAvBLA,OAAA,ICxBL,SAASC,EAAgBC,EAAmBC,EAAW,CAC5D,GAAIA,GAAS,MAGT,SAAOA,GAAS,UAAY,OAAOA,GAAS,UAAY,OAAOA,GAAS,YAGxE,EAAAA,aAAgB,MAGpB,MAAM,IAAI,MAAM,GAAGD,CAAS,4CAA4C,CAC1E,CAXgBE,EAAAH,EAAA,mBAaT,SAASI,EAAeH,EAAmBC,EAAiBG,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQH,CAAI,GAAKA,EAAK,OAASG,EACxC,MAAM,IAAI,MAAM,GAAGJ,CAAS,mCAAmCI,CAAS,QAAQ,EAElF,OAAOH,CACT,CALgBC,EAAAC,EAAA,eAOT,SAASE,EAAYL,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBE,EAAAG,EAAA,eC5BhB,IAAMC,EAA+E,CAClF,GAA8B,YA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AAM,EAEnBC,CACT,CASO,SACLC,EACAC,EACAC,EACyB,CACzB,IAAMC,EAAoC,CACxC,MAAAH,EACA,SAAAC,EACA,MAAAC,CACF,EAIA,GAFAE,EAAmBD,CAAU,EAEzB,KAAK,MAAM,IACb,KAAK,MAAM,IAAI,KAAKA,CAAU,UACrB,KAAK,MAAM,GACpB,KAAK,MAAM,GAAG,KAAKA,CAAU,MAE7B,OAAM,IAAI,MAAM,eAAe,EAGjC,OAAO,IACT,CAKO,OAA0B,CAC/B,OAAI,KAAK,OACA,KAAK,OAAO,MAAM,EAGpB,KAAK,KACd,CAEF","names":["CrudRequestWhereOperator","ensurePrimitive","fieldName","data","__name","ensureArray","minLength","ensureFalsy","operatorValueTypes","validateWhereField","where","type","operatorValueTypes","name","ensurePrimitive","ensureArray","item","ensureFalsy","__name","CrudRequestWhereBuilder","_CrudRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","validateWhereField"]}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { a as RequestParser, O as OpenAPIParameter, R as RequestParamValue } from '../../request-parser-WaQBZsYG.mjs';
|
2
2
|
import { c as CrudRequest, P as ParsedRequestSelect, a as CrudRequestRelation, b as CrudRequestOrder } from '../../crud-request-CvDKp6Iy.mjs';
|
3
|
-
import {
|
3
|
+
import { C as CrudRequestWhereBuilder } from '../../crud-request-where.builder-mP8y-fQp.mjs';
|
4
4
|
|
5
5
|
/**
|
6
6
|
* Parses a request based on the @nestjsx/crud format.
|
@@ -16,7 +16,8 @@ declare class CrudRequestParser implements RequestParser {
|
|
16
16
|
limit: number | undefined;
|
17
17
|
offset: number | undefined;
|
18
18
|
};
|
19
|
-
protected parseSearch(builder:
|
19
|
+
protected parseSearch(builder: CrudRequestWhereBuilder, rawSearch: RequestParamValue, rawFilter: RequestParamValue, rawOr: RequestParamValue): void;
|
20
|
+
protected parseFilter(builder: CrudRequestWhereBuilder, rawFilter: RequestParamValue, rawOr: RequestParamValue): void;
|
20
21
|
}
|
21
22
|
|
22
23
|
export { CrudRequestParser };
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { a as RequestParser, O as OpenAPIParameter, R as RequestParamValue } from '../../request-parser-B-fK5GnP.js';
|
2
2
|
import { c as CrudRequest, P as ParsedRequestSelect, a as CrudRequestRelation, b as CrudRequestOrder } from '../../crud-request-CvDKp6Iy.js';
|
3
|
-
import {
|
3
|
+
import { C as CrudRequestWhereBuilder } from '../../crud-request-where.builder-D60W-QEd.js';
|
4
4
|
|
5
5
|
/**
|
6
6
|
* Parses a request based on the @nestjsx/crud format.
|
@@ -16,7 +16,8 @@ declare class CrudRequestParser implements RequestParser {
|
|
16
16
|
limit: number | undefined;
|
17
17
|
offset: number | undefined;
|
18
18
|
};
|
19
|
-
protected parseSearch(builder:
|
19
|
+
protected parseSearch(builder: CrudRequestWhereBuilder, rawSearch: RequestParamValue, rawFilter: RequestParamValue, rawOr: RequestParamValue): void;
|
20
|
+
protected parseFilter(builder: CrudRequestWhereBuilder, rawFilter: RequestParamValue, rawOr: RequestParamValue): void;
|
20
21
|
}
|
21
22
|
|
22
23
|
export { CrudRequestParser };
|
@@ -1,2 +1,2 @@
|
|
1
|
-
"use strict";var
|
1
|
+
"use strict";var p=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var S=Object.prototype.hasOwnProperty;var o=(t,e)=>p(t,"name",{value:e,configurable:!0});var P=(t,e)=>{for(var r in e)p(t,r,{get:e[r],enumerable:!0})},_=(t,e,r,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of C(e))!S.call(t,i)&&i!==r&&p(t,i,{get:()=>e[i],enumerable:!(s=A(e,i))||s.enumerable});return t};var q=t=>_(p({},"__esModule",{value:!0}),t);var $={};P($,{CrudRequestParser:()=>O});module.exports=q($);function T(t,e){if(e!=null&&!(typeof e=="number"||typeof e=="string"||typeof e=="boolean")&&!(e instanceof Date))throw new Error(`${t} must be a string, number, boolean or null`)}o(T,"ensurePrimitive");function I(t,e,r=0){if(!Array.isArray(e)||e.length<r)throw new Error(`${t} must be an array with at least ${r} items`);return e}o(I,"ensureArray");function y(t,e){if(e)throw new Error(`${t} must be null`)}o(y,"ensureFalsy");function h(t){return t!=null}o(h,"isValid");var g={eq:"primitive",neq:"primitive",gt:"primitive",lt:"primitive",gte:"primitive",lte:"primitive",starts:"primitive",ends:"primitive",contains:"primitive",not_contains:"primitive",in:"array",not_in:"array",between:"array",is_null:"empty",not_null:"empty",eq_lower:"primitive",neq_lower:"primitive",starts_lower:"primitive",ends_lower:"primitive",contains_lower:"primitive",not_contains_lower:"primitive",in_lower:"primitive",not_in_lower:"primitive"};function N(t){let e=g[t.operator],r="The value of the operator "+t.operator;if(e==="primitive"){T(r,t.value);return}if(e==="array"){I(r,t.value).forEach(i=>T(r+" children",i));return}if(e==="empty"){y(r,t.value);return}}o(N,"validateWhereField");var f=class t{constructor(e={and:[]},r){this.where=e;this.parent=r}static{o(this,"CrudRequestWhereBuilder")}addAnd(){if(!this.where.or)return this;let e={and:[]},r=new t(e,this);return this.where.or.push(e),r}addOr(){if(!this.where.and)return this;let e={or:[]},r=new t(e,this);return this.where.and.push(e),r}addField(e,r,s){let i={field:e,operator:r,value:s};if(N(i),this.where.and)this.where.and.push(i);else if(this.where.or)this.where.or.push(i);else throw new Error("Invalid where");return this}build(){return this.parent?this.parent.build():this.where}};var E={$eq:"eq",$ne:"neq",$gt:"gt",$lt:"lt",$gte:"gte",$lte:"lte",$starts:"starts",$ends:"ends",$cont:"contains",$excl:"not_contains",$in:"in",$notin:"not_in",$between:"between",$isnull:"is_null",$notnull:"not_null",$eqL:"eq_lower",$neL:"neq_lower",$startsL:"starts_lower",$endsL:"ends_lower",$contL:"contains_lower",$exclL:"not_contains_lower",$inL:"in_lower",$notinL:"not_in_lower"};function d(t,e,r=[]){if(typeof e!="object")return;let s=Object.keys(e);if(s.length===0)return;let{$or:i,$and:n,...a}=e;if(n){let u=n.length>1||s.length>1?t.addAnd():t;n.forEach(l=>d(u,l,r)),m(u,a,r);return}if(i&&s.length===1){let u=i.length>1?t.addOr():t;i.forEach(l=>d(u,l,r));return}if(i){let u=t.addAnd(),l=i.length>1?u.addOr():u;m(u,a,r),i.forEach(R=>d(l,R,r));return}s.length>1&&(t=t.addAnd()),m(t,a,r)}o(d,"parseCrudSearch");function m(t,e,r){for(let s of Object.keys(e)){let i=e[s];if(!h(i))continue;let n=[...r,...s.split(".")];if(Array.isArray(i)){i.forEach(a=>d(t,a,n));continue}W(t,n,i)}}o(m,"parseCrudSearchFields");function W(t,e,r){if(typeof r!="object"){t.addField(e,"eq",r);return}let s=Object.keys(r);for(let i of s)E[i]&&t.addField(e,E[i],r[i]);r.$or&&W(t.addOr(),e,r.$or)}o(W,"parseCrudSearchField");function V(t,e,r){if(e.length>0&&r.length>0){let s=t.addOr();c(s.addAnd(),e),c(s.addAnd(),r);return}if(r.length>0){c(t.addOr(),r);return}if(e.length>0){c(t.addAnd(),e);return}}o(V,"parseCrudFilters");function c(t,e){for(let r of e){let[s,i,n]=r.split("||",3),a=E[i];a&&t.addField(s.split("."),a,n)}}o(c,"parseCrudFilter");var O=class{static{o(this,"CrudRequestParser")}getOpenAPIParameters(){let e={type:"array",items:{type:"string"}};return[{name:"fields",description:"Selects resource fields",required:!1,schema:e,style:"form",explode:!1},{name:"s",description:"Search condition",required:!1,schema:{type:"string"}},{name:"sort",description:"Sorting",required:!1,schema:e,style:"form",explode:!0},{name:"join",description:"Relations",required:!1,schema:e,style:"form",explode:!0},{name:"limit",description:"Page limit",required:!1,schema:{type:"integer"}},{name:"offset",description:"Offset amount",required:!1,schema:{type:"integer"}},{name:"page",description:"Page number",required:!1,schema:{type:"integer"}}]}parse(e){let r=[],s=[],i=[],n=new f;this.parseSelect(r,e.fields||e.select),this.parseJoin(r,s,e.join),this.parseOrder(i,e.sort),this.parseSearch(n,e.s,e.filter,e.or);let{limit:a,offset:u}=this.parseLimits(e.limit||e.per_page,e.offset,e.page);return{select:r,relations:s,order:i,where:n.build(),limit:a,offset:u}}parseSelect(e,r){if(!r)return;(Array.isArray(r)?r:r.split(",")).forEach(i=>{e.push({field:i.split(".")})})}parseJoin(e,r,s){if(!s)return;if(Array.isArray(s)){s.forEach(l=>this.parseJoin(e,r,l));return}let i=s.toString(),[n,a]=i.split("||",2),u=n.split(".");r.push({field:u}),a&&a.split(",").forEach(R=>{e.push({field:[...u,R]})})}parseOrder(e,r){if(!r)return;if(Array.isArray(r)){r.forEach(n=>this.parseOrder(e,n));return}let[s,i]=r.split(",");e.push({field:s.split("."),order:i?.toUpperCase()==="DESC"?"DESC":"ASC"})}parseNumber(e){if(Array.isArray(e)&&(e=e.length>0?e[0]:void 0),!h(e))return;let r=+e;return isNaN(r)?void 0:r}parseLimits(e,r,s){let i=this.parseNumber(e),n=this.parseNumber(r);if(i&&!n){let a=this.parseNumber(s);a&&(n=i*a)}return{limit:i,offset:n}}parseSearch(e,r,s,i){if(Array.isArray(r)&&(r=r.length>0?r[0]:void 0),!r){this.parseFilter(e,s,i);return}let n=JSON.parse(r);d(e,n)}parseFilter(e,r,s){let i=[],n=[];r&&(i=Array.isArray(r)?r:[r]),s&&(n=Array.isArray(s)?s:[s]),V(e,i,n)}};0&&(module.exports={CrudRequestParser});
|
2
2
|
//# sourceMappingURL=index.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/parsers/crud/index.ts","../../../src/utils/parsed-request-where.builder.ts","../../../src/utils/functions.ts","../../../src/parsers/crud/parseCrudSearch.ts","../../../src/parsers/crud/crud-request.parser.ts"],"sourcesContent":["\r\nexport * from './crud-request.parser';\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class ParsedRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: ParsedRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): ParsedRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): ParsedRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType): ParsedRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid(value: any): value is object {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { ParsedRequestWhereBuilder } from '../../utils/parsed-request-where.builder';\r\nimport { CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { SCondition, SField, SFieldOperator, SFields } from './types';\r\nimport { isValid } from '../../utils/functions';\r\n\r\n/**\r\n * Parses a crud request condition and inserts into a ParsedRequestWhereBuilder\r\n *\r\n * @param builder The builder that the condition will be inserted to\r\n * @param cond The condition that must be parsed\r\n * @param context The field context\r\n */\r\nexport function parseCrudSearch(builder: ParsedRequestWhereBuilder, cond: SCondition, context: string[] = []) {\r\n if (typeof cond !== 'object')\r\n return;\r\n\r\n const keys = Object.keys(cond);\r\n\r\n if (keys.length === 0)\r\n return;\r\n\r\n const { $or, $and, ...innerFields } = cond;\r\n\r\n // { $and: [..., ...] }\r\n if ($and) {\r\n const andBuilder = $and.length > 1 || keys.length > 1 ? builder.addAnd() : builder;\r\n\r\n $and.forEach(c => parseCrudSearch(andBuilder, c, context));\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n return;\r\n }\r\n\r\n // { $or: [..., ...] }\r\n if ($or && keys.length === 1) {\r\n const orBuilder = $or.length > 1 ? builder.addOr() : builder;\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n // { $or: [...], field1: ..., field2: ... }\r\n if ($or) {\r\n const andBuilder = builder.addAnd();\r\n const orBuilder = $or.length > 1 ? andBuilder.addOr() : andBuilder;\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n if (keys.length > 1)\r\n builder = builder.addAnd();\r\n\r\n parseCrudSearchFields(builder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n}\r\n\r\n/**\r\n * Parses an object from the crud request full of fields\r\n *\r\n * @param builder The builder\r\n * @param fields The fields object\r\n * @param context The context\r\n */\r\nfunction parseCrudSearchFields(builder: ParsedRequestWhereBuilder, fields: Omit<Omit<SFields, '$or'>, '$and'>, context: string[]): void {\r\n // { name: 'John', age: { $gte: 18 }, 'posts.name': { $cont: 'Greetings' } }\r\n for (const name of Object.keys(fields)) {\r\n const field = fields[name];\r\n\r\n if (!isValid(field))\r\n continue;\r\n\r\n const fieldPath = [...context, ...name.split('.')];\r\n\r\n // { name: [{ $or: [...] }] }\r\n if (Array.isArray(field)) {\r\n field.forEach(f => parseCrudSearch(builder, f, fieldPath));\r\n continue;\r\n }\r\n\r\n parseCrudSearchField(builder, fieldPath, field);\r\n }\r\n}\r\n\r\n/**\r\n * Parses a single field\r\n *\r\n * @param builder The builder\r\n * @param name The name path\r\n * @param field The field value or operator\r\n */\r\nfunction parseCrudSearchField(builder: ParsedRequestWhereBuilder, name: string[], field: SField): void {\r\n // Primitive Value\r\n // { field: 'text', field2: 12, field3: true }\r\n if (typeof field !== 'object') {\r\n builder.addField(name, CrudRequestWhereOperator.EQ, field);\r\n return;\r\n }\r\n\r\n type OperatorType = Exclude<Exclude<keyof SFieldOperator, '$or'>, '$and'>;\r\n\r\n // Operator\r\n // { field: { $gte: 10 } }\r\n const operatorMap: Record<OperatorType, CrudRequestWhereOperator> = {\r\n $eq: CrudRequestWhereOperator.EQ,\r\n $ne: CrudRequestWhereOperator.NEQ,\r\n $gt: CrudRequestWhereOperator.GT,\r\n $lt: CrudRequestWhereOperator.LT,\r\n $gte: CrudRequestWhereOperator.GTE,\r\n $lte: CrudRequestWhereOperator.LTE,\r\n $starts: CrudRequestWhereOperator.STARTS,\r\n $ends: CrudRequestWhereOperator.ENDS,\r\n $cont: CrudRequestWhereOperator.CONTAINS,\r\n $excl: CrudRequestWhereOperator.NOT_CONTAINS,\r\n $in: CrudRequestWhereOperator.IN,\r\n $notin: CrudRequestWhereOperator.NOT_IN,\r\n $between: CrudRequestWhereOperator.BETWEEN,\r\n $isnull: CrudRequestWhereOperator.IS_NULL,\r\n $notnull: CrudRequestWhereOperator.NOT_NULL,\r\n $eqL: CrudRequestWhereOperator.EQ_LOWER,\r\n $neL: CrudRequestWhereOperator.NEQ_LOWER,\r\n $startsL: CrudRequestWhereOperator.STARTS_LOWER,\r\n $endsL: CrudRequestWhereOperator.ENDS_LOWER,\r\n $contL: CrudRequestWhereOperator.CONTAINS_LOWER,\r\n $exclL: CrudRequestWhereOperator.NOT_CONTAINS_LOWER,\r\n $inL: CrudRequestWhereOperator.IN_LOWER,\r\n $notinL: CrudRequestWhereOperator.NOT_IN_LOWER,\r\n };\r\n\r\n const keys = Object.keys(field) as OperatorType[];\r\n\r\n for (let key of keys) {\r\n if (!operatorMap[key])\r\n continue;\r\n\r\n const value = field[key];\r\n\r\n if (Array.isArray(value))\r\n value.forEach(val => builder.addField(name, operatorMap[key], val));\r\n else\r\n builder.addField(name, operatorMap[key], value);\r\n }\r\n\r\n if (field.$or) {\r\n parseCrudSearchField(builder.addOr(), name, field.$or);\r\n }\r\n}\r\n","import { RequestParamValue, RequestParser } from '../../models/request-parser';\r\nimport {\r\n CrudRequest,\r\n CrudRequestOrder,\r\n CrudRequestRelation,\r\n ParsedRequestSelect\r\n} from '../../models/crud-request';\r\nimport { OpenAPIParameter } from '../../models/openapi-parameter';\r\nimport { ParsedRequestWhereBuilder } from '../../utils/parsed-request-where.builder';\r\nimport { parseCrudSearch } from './parseCrudSearch';\r\nimport { isValid } from '../../utils/functions';\r\nimport { SCondition } from './types';\r\n\r\n/**\r\n * Parses a request based on the @nestjsx/crud format.\r\n */\r\nexport class CrudRequestParser implements RequestParser {\r\n\r\n public getOpenAPIParameters(): OpenAPIParameter[] {\r\n const arraySchema = {\r\n type: 'array',\r\n items: {\r\n type: 'string',\r\n },\r\n };\r\n\r\n return [\r\n {\r\n name: 'fields',\r\n description: 'Selects resource fields',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: false,\r\n },\r\n {\r\n name: 's',\r\n description: 'Search condition',\r\n required: false,\r\n schema: {\r\n type: 'string',\r\n },\r\n },\r\n {\r\n name: 'sort',\r\n description: 'Search condition',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'join',\r\n description: 'Relations',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'limit',\r\n description: 'Page limit',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'offset',\r\n description: 'Offset amount',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'page',\r\n description: 'Page number',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n ];\r\n }\r\n\r\n public parse(query: Record<string, RequestParamValue>): CrudRequest {\r\n const select: ParsedRequestSelect = [];\r\n const relations: CrudRequestRelation[] = [];\r\n const ordering: CrudRequestOrder[] = [];\r\n const where = new ParsedRequestWhereBuilder();\r\n\r\n this.parseSelect(select, query['fields'] || query['select']);\r\n this.parseJoin(select, relations, query['join']);\r\n this.parseOrder(ordering, query['sort']);\r\n this.parseSearch(where, query['s']);\r\n\r\n const { limit, offset } = this.parseLimits(query['limit'], query['offset'], query['page']);\r\n\r\n return {\r\n select,\r\n relations,\r\n order: ordering,\r\n where: where.build(),\r\n limit,\r\n offset,\r\n };\r\n }\r\n\r\n protected parseSelect(select: ParsedRequestSelect, rawSelect: RequestParamValue): void {\r\n if (!rawSelect)\r\n return;\r\n\r\n const selectFields = Array.isArray(rawSelect) ? rawSelect : rawSelect.split(',');\r\n\r\n selectFields.forEach(field => {\r\n select.push({\r\n field: field.split('.'),\r\n });\r\n });\r\n }\r\n\r\n protected parseJoin(requestFields: ParsedRequestSelect, relations: CrudRequestRelation[], rawJoin: RequestParamValue): void {\r\n if (!rawJoin)\r\n return;\r\n\r\n if (Array.isArray(rawJoin)) {\r\n rawJoin.forEach(value => this.parseJoin(requestFields, relations, value));\r\n return;\r\n }\r\n\r\n const join = rawJoin.toString();\r\n\r\n const [field, select] = join.split('||', 2);\r\n const fieldPath = field.split('.');\r\n\r\n relations.push({\r\n field: fieldPath,\r\n });\r\n\r\n if (select) {\r\n const selectFields = select.split(',');\r\n\r\n selectFields.forEach(f => {\r\n requestFields.push({\r\n field: [...fieldPath, f],\r\n });\r\n });\r\n }\r\n }\r\n\r\n protected parseOrder(ordering: CrudRequestOrder[], rawOrder: RequestParamValue): void {\r\n if (!rawOrder)\r\n return;\r\n\r\n if (Array.isArray(rawOrder)) {\r\n rawOrder.forEach(value => this.parseOrder(ordering, value));\r\n return;\r\n }\r\n\r\n const [field, direction] = rawOrder.split(',');\r\n\r\n ordering.push({\r\n field: field.split('.'),\r\n order: direction?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC',\r\n });\r\n }\r\n\r\n protected parseNumber(raw: RequestParamValue): number | undefined {\r\n if (Array.isArray(raw))\r\n raw = raw.length > 0 ? raw[0] : undefined;\r\n\r\n if (!isValid(raw))\r\n return undefined;\r\n\r\n const num = +raw;\r\n\r\n return isNaN(num) ? undefined : num;\r\n }\r\n\r\n protected parseLimits(rawLimit: RequestParamValue, rawOffset: RequestParamValue, rawPage: RequestParamValue) {\r\n const limit = this.parseNumber(rawLimit);\r\n let offset = this.parseNumber(rawOffset);\r\n\r\n if (limit && !offset) {\r\n const page = this.parseNumber(rawPage);\r\n\r\n if (page)\r\n offset = limit * page;\r\n }\r\n\r\n return { limit, offset };\r\n }\r\n\r\n protected parseSearch(builder: ParsedRequestWhereBuilder, rawSearch: RequestParamValue): void {\r\n if (Array.isArray(rawSearch))\r\n rawSearch = rawSearch.length > 0 ? rawSearch[0] : undefined;\r\n\r\n if (!rawSearch)\r\n return;\r\n\r\n const search: SCondition = JSON.parse(rawSearch);\r\n\r\n parseCrudSearch(builder, search);\r\n }\r\n\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,IAAA,eAAAC,EAAAH,GCYO,IAAMI,EAAN,MAAMC,CAA0B,CAErC,YACmBC,EAAkD,CAAE,IAAK,CAAC,CAAE,EAC5DC,EACjB,CAFiB,WAAAD,EACA,YAAAC,CACf,CAjBN,MAYuC,CAAAC,EAAA,kCAU9B,QAAoC,CACzC,GAAI,CAAC,KAAK,MAAM,GACd,OAAO,KAGT,IAAMC,EAA8B,CAAE,IAAK,CAAC,CAAE,EACxCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,GAAG,KAAKA,CAAM,EAElBC,CACT,CAKO,OAAmC,CACxC,GAAI,CAAC,KAAK,MAAM,IACd,OAAO,KAGT,IAAMD,EAA6B,CAAE,GAAI,CAAC,CAAE,EACtCC,EAAU,IAAIL,EAA0BI,EAAQ,IAAI,EAE1D,YAAK,MAAM,IAAI,KAAKA,CAAM,EAEnBC,CACT,CASO,SAASC,EAAiBC,EAAoCC,EAA6D,CAChI,IAAMC,EAAoC,CACxC,MAAAH,EACA,SAAAC,EACA,MAAAC,CACF,EAEA,GAAI,KAAK,MAAM,IACb,KAAK,MAAM,IAAI,KAAKC,CAAU,UACrB,KAAK,MAAM,GACpB,KAAK,MAAM,GAAG,KAAKA,CAAU,MAE7B,OAAM,IAAI,MAAM,eAAe,EAGjC,OAAO,IACT,CAKO,OAA0B,CAC/B,OAAI,KAAK,OACA,KAAK,OAAO,MAAM,EAGpB,KAAK,KACd,CAEF,EC1DO,SAASC,EAAQC,EAA6B,CACnD,OAAOA,GAAU,IACnB,CAFgBC,EAAAF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EAASE,CAAM,CACjC,CAEF","names":["crud_exports","__export","CrudRequestParser","__toCommonJS","ParsedRequestWhereBuilder","_ParsedRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","isValid","value","__name","parseCrudSearch","builder","cond","context","keys","$or","$and","innerFields","andBuilder","c","parseCrudSearchFields","orBuilder","__name","fields","name","field","isValid","fieldPath","f","parseCrudSearchField","operatorMap","key","value","val","CrudRequestParser","__name","arraySchema","query","select","relations","ordering","where","ParsedRequestWhereBuilder","limit","offset","rawSelect","field","requestFields","rawJoin","value","join","fieldPath","f","rawOrder","direction","raw","isValid","num","rawLimit","rawOffset","rawPage","page","builder","rawSearch","search","parseCrudSearch"]}
|
1
|
+
{"version":3,"sources":["../../../src/parsers/crud/index.ts","../../../src/utils/functions.ts","../../../src/utils/where.ts","../../../src/utils/crud-request-where.builder.ts","../../../src/parsers/crud/parseCrudSearch.ts","../../../src/parsers/crud/crud-request.parser.ts"],"sourcesContent":["\r\nexport * from './crud-request.parser';\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { CrudRequestWhereField, CrudRequestWhereOperator } from '../models/crud-request-where';\r\nimport { ensureArray, ensureFalsy, ensurePrimitive } from './functions';\r\n\r\nexport enum WhereOperatorValueType {\r\n PRIMITIVE = 'primitive',\r\n ARRAY = 'array',\r\n EMPTY = 'empty'\r\n}\r\n\r\nconst operatorValueTypes: Record<CrudRequestWhereOperator, WhereOperatorValueType> = {\r\n [CrudRequestWhereOperator.EQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.NOT_IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.BETWEEN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.IS_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.NOT_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.EQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n};\r\n\r\nexport function getWhereOperatorValueType(op: CrudRequestWhereOperator): WhereOperatorValueType {\r\n return operatorValueTypes[op];\r\n}\r\n\r\nexport function validateWhereField(where: CrudRequestWhereField): void {\r\n const type = operatorValueTypes[where.operator];\r\n const name = 'The value of the operator ' + where.operator;\r\n\r\n if (type === WhereOperatorValueType.PRIMITIVE) {\r\n ensurePrimitive(name, where.value);\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.ARRAY) {\r\n const items = ensureArray(name, where.value);\r\n\r\n items.forEach(item => ensurePrimitive(name + ' children', item));\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.EMPTY) {\r\n ensureFalsy(name, where.value);\r\n\r\n return;\r\n }\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\nimport { validateWhereField } from './where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class CrudRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: CrudRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): CrudRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): CrudRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(\r\n field: string[],\r\n operator: CrudRequestWhereOperator,\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[],\r\n ): CrudRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n validateWhereField(whereField);\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n","import { CrudRequestWhereBuilder } from '../../utils/crud-request-where.builder';\r\nimport { CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { SCondition, SField, SFieldOperator, SFields } from './types';\r\nimport { isValid } from '../../utils/functions';\r\n\r\ntype OperatorType = Exclude<Exclude<keyof SFieldOperator, '$or'>, '$and'>;\r\n\r\n// Operator\r\n// { field: { $gte: 10 } }\r\nconst operatorMap: Record<OperatorType, CrudRequestWhereOperator> = {\r\n $eq: CrudRequestWhereOperator.EQ,\r\n $ne: CrudRequestWhereOperator.NEQ,\r\n $gt: CrudRequestWhereOperator.GT,\r\n $lt: CrudRequestWhereOperator.LT,\r\n $gte: CrudRequestWhereOperator.GTE,\r\n $lte: CrudRequestWhereOperator.LTE,\r\n $starts: CrudRequestWhereOperator.STARTS,\r\n $ends: CrudRequestWhereOperator.ENDS,\r\n $cont: CrudRequestWhereOperator.CONTAINS,\r\n $excl: CrudRequestWhereOperator.NOT_CONTAINS,\r\n $in: CrudRequestWhereOperator.IN,\r\n $notin: CrudRequestWhereOperator.NOT_IN,\r\n $between: CrudRequestWhereOperator.BETWEEN,\r\n $isnull: CrudRequestWhereOperator.IS_NULL,\r\n $notnull: CrudRequestWhereOperator.NOT_NULL,\r\n $eqL: CrudRequestWhereOperator.EQ_LOWER,\r\n $neL: CrudRequestWhereOperator.NEQ_LOWER,\r\n $startsL: CrudRequestWhereOperator.STARTS_LOWER,\r\n $endsL: CrudRequestWhereOperator.ENDS_LOWER,\r\n $contL: CrudRequestWhereOperator.CONTAINS_LOWER,\r\n $exclL: CrudRequestWhereOperator.NOT_CONTAINS_LOWER,\r\n $inL: CrudRequestWhereOperator.IN_LOWER,\r\n $notinL: CrudRequestWhereOperator.NOT_IN_LOWER,\r\n};\r\n\r\n/**\r\n * Parses a crud request condition and inserts into a CrudRequestWhereBuilder\r\n *\r\n * @param builder The builder that the condition will be inserted to\r\n * @param cond The condition that must be parsed\r\n * @param context The field context\r\n */\r\nexport function parseCrudSearch(builder: CrudRequestWhereBuilder, cond: SCondition, context: string[] = []) {\r\n if (typeof cond !== 'object')\r\n return;\r\n\r\n const keys = Object.keys(cond);\r\n\r\n if (keys.length === 0)\r\n return;\r\n\r\n const { $or, $and, ...innerFields } = cond;\r\n\r\n // { $and: [..., ...] }\r\n if ($and) {\r\n const andBuilder = $and.length > 1 || keys.length > 1 ? builder.addAnd() : builder;\r\n\r\n $and.forEach(c => parseCrudSearch(andBuilder, c, context));\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n return;\r\n }\r\n\r\n // { $or: [..., ...] }\r\n if ($or && keys.length === 1) {\r\n const orBuilder = $or.length > 1 ? builder.addOr() : builder;\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n // { $or: [...], field1: ..., field2: ... }\r\n if ($or) {\r\n const andBuilder = builder.addAnd();\r\n const orBuilder = $or.length > 1 ? andBuilder.addOr() : andBuilder;\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n if (keys.length > 1)\r\n builder = builder.addAnd();\r\n\r\n parseCrudSearchFields(builder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n}\r\n\r\n/**\r\n * Parses an object from the crud request full of fields\r\n *\r\n * @param builder The builder\r\n * @param fields The fields object\r\n * @param context The context\r\n */\r\nfunction parseCrudSearchFields(builder: CrudRequestWhereBuilder, fields: Omit<Omit<SFields, '$or'>, '$and'>, context: string[]): void {\r\n // { name: 'John', age: { $gte: 18 }, 'posts.name': { $cont: 'Greetings' } }\r\n for (const name of Object.keys(fields)) {\r\n const field = fields[name];\r\n\r\n if (!isValid(field))\r\n continue;\r\n\r\n const fieldPath = [...context, ...name.split('.')];\r\n\r\n // { name: [{ $or: [...] }] }\r\n if (Array.isArray(field)) {\r\n field.forEach(f => parseCrudSearch(builder, f, fieldPath));\r\n continue;\r\n }\r\n\r\n parseCrudSearchField(builder, fieldPath, field);\r\n }\r\n}\r\n\r\n/**\r\n * Parses a single field\r\n *\r\n * @param builder The builder\r\n * @param name The name path\r\n * @param field The field value or operator\r\n */\r\nfunction parseCrudSearchField(builder: CrudRequestWhereBuilder, name: string[], field: SField): void {\r\n // Primitive Value\r\n // { field: 'text', field2: 12, field3: true }\r\n if (typeof field !== 'object') {\r\n builder.addField(name, CrudRequestWhereOperator.EQ, field);\r\n return;\r\n }\r\n\r\n const keys = Object.keys(field) as OperatorType[];\r\n\r\n for (let key of keys) {\r\n if (!operatorMap[key])\r\n continue;\r\n\r\n builder.addField(name, operatorMap[key], field[key]);\r\n }\r\n\r\n if (field.$or) {\r\n parseCrudSearchField(builder.addOr(), name, field.$or);\r\n }\r\n}\r\n\r\nexport function parseCrudFilters(builder: CrudRequestWhereBuilder, andFilters: string[], orFilters: string[]): void {\r\n // Based on rules from https://github.com/nestjsx/crud/wiki/Requests#or\r\n // \"If present both or and filter in any amount (one or miltiple each) then both interpreted\r\n // as a combitation of AND conditions and compared with each other by OR condition\"\r\n // \"If there are one or and one filter then it will be interpreted as OR condition\"\r\n if (andFilters.length > 0 && orFilters.length > 0) {\r\n const or = builder.addOr();\r\n\r\n parseCrudFilter(or.addAnd(), andFilters);\r\n parseCrudFilter(or.addAnd(), orFilters);\r\n\r\n return;\r\n }\r\n\r\n // \"If there are multiple or present (without filter) then it will be interpreted as a compination of OR conditions\"\r\n if (orFilters.length > 0) {\r\n parseCrudFilter(builder.addOr(), orFilters);\r\n\r\n return;\r\n }\r\n\r\n if (andFilters.length > 0) {\r\n parseCrudFilter(builder.addAnd(), andFilters);\r\n\r\n return;\r\n }\r\n}\r\n\r\nfunction parseCrudFilter(builder: CrudRequestWhereBuilder, rawFilters: string[]): void {\r\n for (const rawFilter of rawFilters) {\r\n const [name, op, value] = rawFilter.split('||', 3);\r\n const operator = operatorMap[op as OperatorType];\r\n\r\n if (!operator)\r\n continue;\r\n\r\n builder.addField(name.split('.'), operator, value);\r\n }\r\n}\r\n","import { RequestParamValue, RequestParser } from '../../models/request-parser';\r\nimport { CrudRequest, CrudRequestOrder, CrudRequestRelation, ParsedRequestSelect } from '../../models/crud-request';\r\nimport { OpenAPIParameter } from '../../models/openapi-parameter';\r\nimport { CrudRequestWhereBuilder } from '../../utils/crud-request-where.builder';\r\nimport { parseCrudFilters, parseCrudSearch } from './parseCrudSearch';\r\nimport { isValid } from '../../utils/functions';\r\nimport { SCondition } from './types';\r\n\r\n/**\r\n * Parses a request based on the @nestjsx/crud format.\r\n */\r\nexport class CrudRequestParser implements RequestParser {\r\n\r\n public getOpenAPIParameters(): OpenAPIParameter[] {\r\n // TODO improve docs\r\n\r\n const arraySchema = {\r\n type: 'array',\r\n items: {\r\n type: 'string',\r\n },\r\n };\r\n\r\n return [\r\n {\r\n name: 'fields',\r\n description: 'Selects resource fields',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: false,\r\n },\r\n {\r\n name: 's',\r\n description: 'Search condition',\r\n required: false,\r\n schema: {\r\n type: 'string',\r\n },\r\n },\r\n {\r\n name: 'sort',\r\n description: 'Sorting',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'join',\r\n description: 'Relations',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'limit',\r\n description: 'Page limit',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'offset',\r\n description: 'Offset amount',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'page',\r\n description: 'Page number',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n ];\r\n }\r\n\r\n public parse(query: Record<string, RequestParamValue>): CrudRequest {\r\n const select: ParsedRequestSelect = [];\r\n const relations: CrudRequestRelation[] = [];\r\n const ordering: CrudRequestOrder[] = [];\r\n const where = new CrudRequestWhereBuilder();\r\n\r\n this.parseSelect(select, query['fields'] || query['select']);\r\n this.parseJoin(select, relations, query['join']);\r\n this.parseOrder(ordering, query['sort']);\r\n this.parseSearch(where, query['s'], query['filter'], query['or']);\r\n\r\n const { limit, offset } = this.parseLimits(query['limit'] || query['per_page'], query['offset'], query['page']);\r\n\r\n return {\r\n select,\r\n relations,\r\n order: ordering,\r\n where: where.build(),\r\n limit,\r\n offset,\r\n };\r\n }\r\n\r\n protected parseSelect(select: ParsedRequestSelect, rawSelect: RequestParamValue): void {\r\n if (!rawSelect)\r\n return;\r\n\r\n const selectFields = Array.isArray(rawSelect) ? rawSelect : rawSelect.split(',');\r\n\r\n selectFields.forEach(field => {\r\n select.push({\r\n field: field.split('.'),\r\n });\r\n });\r\n }\r\n\r\n protected parseJoin(requestFields: ParsedRequestSelect, relations: CrudRequestRelation[], rawJoin: RequestParamValue): void {\r\n if (!rawJoin)\r\n return;\r\n\r\n if (Array.isArray(rawJoin)) {\r\n rawJoin.forEach(value => this.parseJoin(requestFields, relations, value));\r\n return;\r\n }\r\n\r\n const join = rawJoin.toString();\r\n\r\n const [field, select] = join.split('||', 2);\r\n const fieldPath = field.split('.');\r\n\r\n relations.push({\r\n field: fieldPath,\r\n });\r\n\r\n if (select) {\r\n const selectFields = select.split(',');\r\n\r\n selectFields.forEach(f => {\r\n requestFields.push({\r\n field: [...fieldPath, f],\r\n });\r\n });\r\n }\r\n }\r\n\r\n protected parseOrder(ordering: CrudRequestOrder[], rawOrder: RequestParamValue): void {\r\n if (!rawOrder)\r\n return;\r\n\r\n if (Array.isArray(rawOrder)) {\r\n rawOrder.forEach(value => this.parseOrder(ordering, value));\r\n return;\r\n }\r\n\r\n const [field, direction] = rawOrder.split(',');\r\n\r\n ordering.push({\r\n field: field.split('.'),\r\n order: direction?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC',\r\n });\r\n }\r\n\r\n protected parseNumber(raw: RequestParamValue): number | undefined {\r\n if (Array.isArray(raw))\r\n raw = raw.length > 0 ? raw[0] : undefined;\r\n\r\n if (!isValid(raw))\r\n return undefined;\r\n\r\n const num = +raw;\r\n\r\n return isNaN(num) ? undefined : num;\r\n }\r\n\r\n protected parseLimits(rawLimit: RequestParamValue, rawOffset: RequestParamValue, rawPage: RequestParamValue) {\r\n const limit = this.parseNumber(rawLimit);\r\n let offset = this.parseNumber(rawOffset);\r\n\r\n if (limit && !offset) {\r\n const page = this.parseNumber(rawPage);\r\n\r\n if (page)\r\n offset = limit * page;\r\n }\r\n\r\n return { limit, offset };\r\n }\r\n\r\n protected parseSearch(builder: CrudRequestWhereBuilder, rawSearch: RequestParamValue, rawFilter: RequestParamValue, rawOr: RequestParamValue): void {\r\n if (Array.isArray(rawSearch))\r\n rawSearch = rawSearch.length > 0 ? rawSearch[0] : undefined;\r\n\r\n if (!rawSearch) {\r\n // In case the search is not defined, we'll read the filter and or parameters\r\n this.parseFilter(builder, rawFilter, rawOr);\r\n return;\r\n }\r\n\r\n const search: SCondition = JSON.parse(rawSearch);\r\n\r\n parseCrudSearch(builder, search);\r\n }\r\n\r\n protected parseFilter(builder: CrudRequestWhereBuilder, rawFilter: RequestParamValue, rawOr: RequestParamValue): void {\r\n let andFilters: string[] = [];\r\n let orFilters: string[] = [];\r\n\r\n if (rawFilter)\r\n andFilters = Array.isArray(rawFilter) ? rawFilter : [rawFilter];\r\n\r\n if (rawOr)\r\n orFilters = Array.isArray(rawOr) ? rawOr : [rawOr];\r\n\r\n parseCrudFilters(builder, andFilters, orFilters);\r\n }\r\n\r\n}\r\n"],"mappings":"4dAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,uBAAAE,IAAA,eAAAC,EAAAH,GCiBO,SAASI,EAAgBC,EAAmBC,EAAW,CAC5D,GAAIA,GAAS,MAGT,SAAOA,GAAS,UAAY,OAAOA,GAAS,UAAY,OAAOA,GAAS,YAGxE,EAAAA,aAAgB,MAGpB,MAAM,IAAI,MAAM,GAAGD,CAAS,4CAA4C,CAC1E,CAXgBE,EAAAH,EAAA,mBAaT,SAASI,EAAeH,EAAmBC,EAAiBG,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQH,CAAI,GAAKA,EAAK,OAASG,EACxC,MAAM,IAAI,MAAM,GAAGJ,CAAS,mCAAmCI,CAAS,QAAQ,EAElF,OAAOH,CACT,CALgBC,EAAAC,EAAA,eAOT,SA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,EAE/CI,EAAgBL,EAASI,CAAM,CACjC,CAEU,YAAYJ,EAAkCE,EAA8BC,EAAgC,CACpH,IAAIG,EAAuB,CAAC,EACxBC,EAAsB,CAAC,EAEvBL,IACFI,EAAa,MAAM,QAAQJ,CAAS,EAAIA,EAAY,CAACA,CAAS,GAE5DC,IACFI,EAAY,MAAM,QAAQJ,CAAK,EAAIA,EAAQ,CAACA,CAAK,GAEnDK,EAAiBR,EAASM,EAAYC,CAAS,CACjD,CAEF","names":["crud_exports","__export","CrudRequestParser","__toCommonJS","ensurePrimitive","fieldName","data","__name","ensureArray","minLength","ensureFalsy","isValid","value","operatorValueTypes","validateWhereField","where","type","operatorValueTypes","name","ensurePrimitive","ensureArray","item","ensureFalsy","__name","CrudRequestWhereBuilder","_CrudRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","validateWhereField","operatorMap","parseCrudSearch","builder","cond","context","keys","$or","$and","innerFields","andBuilder","c","parseCrudSearchFields","orBuilder","__name","fields","name","field","isValid","fieldPath","f","parseCrudSearchField","key","parseCrudFilters","andFilters","orFilters","or","parseCrudFilter","rawFilters","rawFilter","op","value","operator","CrudRequestParser","__name","arraySchema","query","select","relations","ordering","where","CrudRequestWhereBuilder","limit","offset","rawSelect","field","requestFields","rawJoin","value","join","fieldPath","f","rawOrder","direction","raw","isValid","num","rawLimit","rawOffset","rawPage","page","builder","rawSearch","rawFilter","rawOr","search","parseCrudSearch","andFilters","orFilters","parseCrudFilters"]}
|
@@ -1,2 +1,2 @@
|
|
1
|
-
var
|
1
|
+
var V=Object.defineProperty;var o=(t,e)=>V(t,"name",{value:e,configurable:!0});function R(t,e){if(e!=null&&!(typeof e=="number"||typeof e=="string"||typeof e=="boolean")&&!(e instanceof Date))throw new Error(`${t} must be a string, number, boolean or null`)}o(R,"ensurePrimitive");function E(t,e,r=0){if(!Array.isArray(e)||e.length<r)throw new Error(`${t} must be an array with at least ${r} items`);return e}o(E,"ensureArray");function O(t,e){if(e)throw new Error(`${t} must be null`)}o(O,"ensureFalsy");function p(t){return t!=null}o(p,"isValid");var C={eq:"primitive",neq:"primitive",gt:"primitive",lt:"primitive",gte:"primitive",lte:"primitive",starts:"primitive",ends:"primitive",contains:"primitive",not_contains:"primitive",in:"array",not_in:"array",between:"array",is_null:"empty",not_null:"empty",eq_lower:"primitive",neq_lower:"primitive",starts_lower:"primitive",ends_lower:"primitive",contains_lower:"primitive",not_contains_lower:"primitive",in_lower:"primitive",not_in_lower:"primitive"};function I(t){let e=C[t.operator],r="The value of the operator "+t.operator;if(e==="primitive"){R(r,t.value);return}if(e==="array"){E(r,t.value).forEach(i=>R(r+" children",i));return}if(e==="empty"){O(r,t.value);return}}o(I,"validateWhereField");var h=class t{constructor(e={and:[]},r){this.where=e;this.parent=r}static{o(this,"CrudRequestWhereBuilder")}addAnd(){if(!this.where.or)return this;let e={and:[]},r=new t(e,this);return this.where.or.push(e),r}addOr(){if(!this.where.and)return this;let e={or:[]},r=new t(e,this);return this.where.and.push(e),r}addField(e,r,s){let i={field:e,operator:r,value:s};if(I(i),this.where.and)this.where.and.push(i);else if(this.where.or)this.where.or.push(i);else throw new Error("Invalid where");return this}build(){return this.parent?this.parent.build():this.where}};var m={$eq:"eq",$ne:"neq",$gt:"gt",$lt:"lt",$gte:"gte",$lte:"lte",$starts:"starts",$ends:"ends",$cont:"contains",$excl:"not_contains",$in:"in",$notin:"not_in",$between:"between",$isnull:"is_null",$notnull:"not_null",$eqL:"eq_lower",$neL:"neq_lower",$startsL:"starts_lower",$endsL:"ends_lower",$contL:"contains_lower",$exclL:"not_contains_lower",$inL:"in_lower",$notinL:"not_in_lower"};function d(t,e,r=[]){if(typeof e!="object")return;let s=Object.keys(e);if(s.length===0)return;let{$or:i,$and:n,...a}=e;if(n){let u=n.length>1||s.length>1?t.addAnd():t;n.forEach(l=>d(u,l,r)),T(u,a,r);return}if(i&&s.length===1){let u=i.length>1?t.addOr():t;i.forEach(l=>d(u,l,r));return}if(i){let u=t.addAnd(),l=i.length>1?u.addOr():u;T(u,a,r),i.forEach(c=>d(l,c,r));return}s.length>1&&(t=t.addAnd()),T(t,a,r)}o(d,"parseCrudSearch");function T(t,e,r){for(let s of Object.keys(e)){let i=e[s];if(!p(i))continue;let n=[...r,...s.split(".")];if(Array.isArray(i)){i.forEach(a=>d(t,a,n));continue}y(t,n,i)}}o(T,"parseCrudSearchFields");function y(t,e,r){if(typeof r!="object"){t.addField(e,"eq",r);return}let s=Object.keys(r);for(let i of s)m[i]&&t.addField(e,m[i],r[i]);r.$or&&y(t.addOr(),e,r.$or)}o(y,"parseCrudSearchField");function N(t,e,r){if(e.length>0&&r.length>0){let s=t.addOr();f(s.addAnd(),e),f(s.addAnd(),r);return}if(r.length>0){f(t.addOr(),r);return}if(e.length>0){f(t.addAnd(),e);return}}o(N,"parseCrudFilters");function f(t,e){for(let r of e){let[s,i,n]=r.split("||",3),a=m[i];a&&t.addField(s.split("."),a,n)}}o(f,"parseCrudFilter");var W=class{static{o(this,"CrudRequestParser")}getOpenAPIParameters(){let e={type:"array",items:{type:"string"}};return[{name:"fields",description:"Selects resource fields",required:!1,schema:e,style:"form",explode:!1},{name:"s",description:"Search condition",required:!1,schema:{type:"string"}},{name:"sort",description:"Sorting",required:!1,schema:e,style:"form",explode:!0},{name:"join",description:"Relations",required:!1,schema:e,style:"form",explode:!0},{name:"limit",description:"Page limit",required:!1,schema:{type:"integer"}},{name:"offset",description:"Offset amount",required:!1,schema:{type:"integer"}},{name:"page",description:"Page number",required:!1,schema:{type:"integer"}}]}parse(e){let r=[],s=[],i=[],n=new h;this.parseSelect(r,e.fields||e.select),this.parseJoin(r,s,e.join),this.parseOrder(i,e.sort),this.parseSearch(n,e.s,e.filter,e.or);let{limit:a,offset:u}=this.parseLimits(e.limit||e.per_page,e.offset,e.page);return{select:r,relations:s,order:i,where:n.build(),limit:a,offset:u}}parseSelect(e,r){if(!r)return;(Array.isArray(r)?r:r.split(",")).forEach(i=>{e.push({field:i.split(".")})})}parseJoin(e,r,s){if(!s)return;if(Array.isArray(s)){s.forEach(l=>this.parseJoin(e,r,l));return}let i=s.toString(),[n,a]=i.split("||",2),u=n.split(".");r.push({field:u}),a&&a.split(",").forEach(c=>{e.push({field:[...u,c]})})}parseOrder(e,r){if(!r)return;if(Array.isArray(r)){r.forEach(n=>this.parseOrder(e,n));return}let[s,i]=r.split(",");e.push({field:s.split("."),order:i?.toUpperCase()==="DESC"?"DESC":"ASC"})}parseNumber(e){if(Array.isArray(e)&&(e=e.length>0?e[0]:void 0),!p(e))return;let r=+e;return isNaN(r)?void 0:r}parseLimits(e,r,s){let i=this.parseNumber(e),n=this.parseNumber(r);if(i&&!n){let a=this.parseNumber(s);a&&(n=i*a)}return{limit:i,offset:n}}parseSearch(e,r,s,i){if(Array.isArray(r)&&(r=r.length>0?r[0]:void 0),!r){this.parseFilter(e,s,i);return}let n=JSON.parse(r);d(e,n)}parseFilter(e,r,s){let i=[],n=[];r&&(i=Array.isArray(r)?r:[r]),s&&(n=Array.isArray(s)?s:[s]),N(e,i,n)}};export{W as CrudRequestParser};
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/parsed-request-where.builder.ts","../../../src/utils/functions.ts","../../../src/parsers/crud/parseCrudSearch.ts","../../../src/parsers/crud/crud-request.parser.ts"],"sourcesContent":["import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class ParsedRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: ParsedRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): ParsedRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): ParsedRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new ParsedRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(field: string[], operator: CrudRequestWhereOperator, value: CrudRequestWhereValueType): ParsedRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n","import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid(value: any): value is object {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { ParsedRequestWhereBuilder } from '../../utils/parsed-request-where.builder';\r\nimport { CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { SCondition, SField, SFieldOperator, SFields } from './types';\r\nimport { isValid } from '../../utils/functions';\r\n\r\n/**\r\n * Parses a crud request condition and inserts into a ParsedRequestWhereBuilder\r\n *\r\n * @param builder The builder that the condition will be inserted to\r\n * @param cond The condition that must be parsed\r\n * @param context The field context\r\n */\r\nexport function parseCrudSearch(builder: ParsedRequestWhereBuilder, cond: SCondition, context: string[] = []) {\r\n if (typeof cond !== 'object')\r\n return;\r\n\r\n const keys = Object.keys(cond);\r\n\r\n if (keys.length === 0)\r\n return;\r\n\r\n const { $or, $and, ...innerFields } = cond;\r\n\r\n // { $and: [..., ...] }\r\n if ($and) {\r\n const andBuilder = $and.length > 1 || keys.length > 1 ? builder.addAnd() : builder;\r\n\r\n $and.forEach(c => parseCrudSearch(andBuilder, c, context));\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n return;\r\n }\r\n\r\n // { $or: [..., ...] }\r\n if ($or && keys.length === 1) {\r\n const orBuilder = $or.length > 1 ? builder.addOr() : builder;\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n // { $or: [...], field1: ..., field2: ... }\r\n if ($or) {\r\n const andBuilder = builder.addAnd();\r\n const orBuilder = $or.length > 1 ? andBuilder.addOr() : andBuilder;\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n if (keys.length > 1)\r\n builder = builder.addAnd();\r\n\r\n parseCrudSearchFields(builder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n}\r\n\r\n/**\r\n * Parses an object from the crud request full of fields\r\n *\r\n * @param builder The builder\r\n * @param fields The fields object\r\n * @param context The context\r\n */\r\nfunction parseCrudSearchFields(builder: ParsedRequestWhereBuilder, fields: Omit<Omit<SFields, '$or'>, '$and'>, context: string[]): void {\r\n // { name: 'John', age: { $gte: 18 }, 'posts.name': { $cont: 'Greetings' } }\r\n for (const name of Object.keys(fields)) {\r\n const field = fields[name];\r\n\r\n if (!isValid(field))\r\n continue;\r\n\r\n const fieldPath = [...context, ...name.split('.')];\r\n\r\n // { name: [{ $or: [...] }] }\r\n if (Array.isArray(field)) {\r\n field.forEach(f => parseCrudSearch(builder, f, fieldPath));\r\n continue;\r\n }\r\n\r\n parseCrudSearchField(builder, fieldPath, field);\r\n }\r\n}\r\n\r\n/**\r\n * Parses a single field\r\n *\r\n * @param builder The builder\r\n * @param name The name path\r\n * @param field The field value or operator\r\n */\r\nfunction parseCrudSearchField(builder: ParsedRequestWhereBuilder, name: string[], field: SField): void {\r\n // Primitive Value\r\n // { field: 'text', field2: 12, field3: true }\r\n if (typeof field !== 'object') {\r\n builder.addField(name, CrudRequestWhereOperator.EQ, field);\r\n return;\r\n }\r\n\r\n type OperatorType = Exclude<Exclude<keyof SFieldOperator, '$or'>, '$and'>;\r\n\r\n // Operator\r\n // { field: { $gte: 10 } }\r\n const operatorMap: Record<OperatorType, CrudRequestWhereOperator> = {\r\n $eq: CrudRequestWhereOperator.EQ,\r\n $ne: CrudRequestWhereOperator.NEQ,\r\n $gt: CrudRequestWhereOperator.GT,\r\n $lt: CrudRequestWhereOperator.LT,\r\n $gte: CrudRequestWhereOperator.GTE,\r\n $lte: CrudRequestWhereOperator.LTE,\r\n $starts: CrudRequestWhereOperator.STARTS,\r\n $ends: CrudRequestWhereOperator.ENDS,\r\n $cont: CrudRequestWhereOperator.CONTAINS,\r\n $excl: CrudRequestWhereOperator.NOT_CONTAINS,\r\n $in: CrudRequestWhereOperator.IN,\r\n $notin: CrudRequestWhereOperator.NOT_IN,\r\n $between: CrudRequestWhereOperator.BETWEEN,\r\n $isnull: CrudRequestWhereOperator.IS_NULL,\r\n $notnull: CrudRequestWhereOperator.NOT_NULL,\r\n $eqL: CrudRequestWhereOperator.EQ_LOWER,\r\n $neL: CrudRequestWhereOperator.NEQ_LOWER,\r\n $startsL: CrudRequestWhereOperator.STARTS_LOWER,\r\n $endsL: CrudRequestWhereOperator.ENDS_LOWER,\r\n $contL: CrudRequestWhereOperator.CONTAINS_LOWER,\r\n $exclL: CrudRequestWhereOperator.NOT_CONTAINS_LOWER,\r\n $inL: CrudRequestWhereOperator.IN_LOWER,\r\n $notinL: CrudRequestWhereOperator.NOT_IN_LOWER,\r\n };\r\n\r\n const keys = Object.keys(field) as OperatorType[];\r\n\r\n for (let key of keys) {\r\n if (!operatorMap[key])\r\n continue;\r\n\r\n const value = field[key];\r\n\r\n if (Array.isArray(value))\r\n value.forEach(val => builder.addField(name, operatorMap[key], val));\r\n else\r\n builder.addField(name, operatorMap[key], value);\r\n }\r\n\r\n if (field.$or) {\r\n parseCrudSearchField(builder.addOr(), name, field.$or);\r\n }\r\n}\r\n","import { RequestParamValue, RequestParser } from '../../models/request-parser';\r\nimport {\r\n CrudRequest,\r\n CrudRequestOrder,\r\n CrudRequestRelation,\r\n ParsedRequestSelect\r\n} from '../../models/crud-request';\r\nimport { OpenAPIParameter } from '../../models/openapi-parameter';\r\nimport { ParsedRequestWhereBuilder } from '../../utils/parsed-request-where.builder';\r\nimport { parseCrudSearch } from './parseCrudSearch';\r\nimport { isValid } from '../../utils/functions';\r\nimport { SCondition } from './types';\r\n\r\n/**\r\n * Parses a request based on the @nestjsx/crud format.\r\n */\r\nexport class CrudRequestParser implements RequestParser {\r\n\r\n public getOpenAPIParameters(): OpenAPIParameter[] {\r\n const arraySchema = {\r\n type: 'array',\r\n items: {\r\n type: 'string',\r\n },\r\n };\r\n\r\n return [\r\n {\r\n name: 'fields',\r\n description: 'Selects resource fields',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: false,\r\n },\r\n {\r\n name: 's',\r\n description: 'Search condition',\r\n required: false,\r\n schema: {\r\n type: 'string',\r\n },\r\n },\r\n {\r\n name: 'sort',\r\n description: 'Search condition',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'join',\r\n description: 'Relations',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'limit',\r\n description: 'Page limit',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'offset',\r\n description: 'Offset amount',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'page',\r\n description: 'Page number',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n ];\r\n }\r\n\r\n public parse(query: Record<string, RequestParamValue>): CrudRequest {\r\n const select: ParsedRequestSelect = [];\r\n const relations: CrudRequestRelation[] = [];\r\n const ordering: CrudRequestOrder[] = [];\r\n const where = new ParsedRequestWhereBuilder();\r\n\r\n this.parseSelect(select, query['fields'] || query['select']);\r\n this.parseJoin(select, relations, query['join']);\r\n this.parseOrder(ordering, query['sort']);\r\n this.parseSearch(where, query['s']);\r\n\r\n const { limit, offset } = this.parseLimits(query['limit'], query['offset'], query['page']);\r\n\r\n return {\r\n select,\r\n relations,\r\n order: ordering,\r\n where: where.build(),\r\n limit,\r\n offset,\r\n };\r\n }\r\n\r\n protected parseSelect(select: ParsedRequestSelect, rawSelect: RequestParamValue): void {\r\n if (!rawSelect)\r\n return;\r\n\r\n const selectFields = Array.isArray(rawSelect) ? rawSelect : rawSelect.split(',');\r\n\r\n selectFields.forEach(field => {\r\n select.push({\r\n field: field.split('.'),\r\n });\r\n });\r\n }\r\n\r\n protected parseJoin(requestFields: ParsedRequestSelect, relations: CrudRequestRelation[], rawJoin: RequestParamValue): void {\r\n if (!rawJoin)\r\n return;\r\n\r\n if (Array.isArray(rawJoin)) {\r\n rawJoin.forEach(value => this.parseJoin(requestFields, relations, value));\r\n return;\r\n }\r\n\r\n const join = rawJoin.toString();\r\n\r\n const [field, select] = join.split('||', 2);\r\n const fieldPath = field.split('.');\r\n\r\n relations.push({\r\n field: fieldPath,\r\n });\r\n\r\n if (select) {\r\n const selectFields = select.split(',');\r\n\r\n selectFields.forEach(f => {\r\n requestFields.push({\r\n field: [...fieldPath, f],\r\n });\r\n });\r\n }\r\n }\r\n\r\n protected parseOrder(ordering: CrudRequestOrder[], rawOrder: RequestParamValue): void {\r\n if (!rawOrder)\r\n return;\r\n\r\n if (Array.isArray(rawOrder)) {\r\n rawOrder.forEach(value => this.parseOrder(ordering, value));\r\n return;\r\n }\r\n\r\n const [field, direction] = rawOrder.split(',');\r\n\r\n ordering.push({\r\n field: field.split('.'),\r\n order: direction?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC',\r\n });\r\n }\r\n\r\n protected parseNumber(raw: RequestParamValue): number | undefined {\r\n if (Array.isArray(raw))\r\n raw = raw.length > 0 ? raw[0] : undefined;\r\n\r\n if (!isValid(raw))\r\n return undefined;\r\n\r\n const num = +raw;\r\n\r\n return isNaN(num) ? undefined : num;\r\n }\r\n\r\n protected parseLimits(rawLimit: RequestParamValue, rawOffset: RequestParamValue, rawPage: RequestParamValue) {\r\n const limit = this.parseNumber(rawLimit);\r\n let offset = this.parseNumber(rawOffset);\r\n\r\n if (limit && !offset) {\r\n const page = this.parseNumber(rawPage);\r\n\r\n if (page)\r\n offset = limit * page;\r\n }\r\n\r\n return { limit, offset };\r\n }\r\n\r\n protected parseSearch(builder: ParsedRequestWhereBuilder, rawSearch: RequestParamValue): void {\r\n if (Array.isArray(rawSearch))\r\n rawSearch = rawSearch.length > 0 ? rawSearch[0] : undefined;\r\n\r\n if (!rawSearch)\r\n return;\r\n\r\n const search: SCondition = JSON.parse(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,CAEL,CAEU,WAAWX,EAA8BY,EAAmC,CACpF,GAAI,CAACA,EACH,OAEF,GAAI,MAAM,QAAQA,CAAQ,EAAG,CAC3BA,EAAS,QAAQH,GAAS,KAAK,WAAWT,EAAUS,CAAK,CAAC,EAC1D,MACF,CAEA,GAAM,CAACH,EAAOO,CAAS,EAAID,EAAS,MAAM,GAAG,EAE7CZ,EAAS,KAAK,CACZ,MAAOM,EAAM,MAAM,GAAG,EACtB,MAAOO,GAAW,YAAY,IAAM,OAAS,OAAS,KACxD,CAAC,CACH,CAEU,YAAYC,EAA4C,CAIhE,GAHI,MAAM,QAAQA,CAAG,IACnBA,EAAMA,EAAI,OAAS,EAAIA,EAAI,CAAC,EAAI,QAE9B,CAACC,EAAQD,CAAG,EACd,OAEF,IAAME,EAAM,CAACF,EAEb,OAAO,MAAME,CAAG,EAAI,OAAYA,CAClC,CAEU,YAAYC,EAA6BC,EAA8BC,EAA4B,CAC3G,IAAMhB,EAAQ,KAAK,YAAYc,CAAQ,EACnCb,EAAS,KAAK,YAAYc,CAAS,EAEvC,GAAIf,GAAS,CAACC,EAAQ,CACpB,IAAMgB,EAAO,KAAK,YAAYD,CAAO,EAEjCC,IACFhB,EAASD,EAAQiB,EACrB,CAEA,MAAO,CAAE,MAAAjB,EAAO,OAAAC,CAAO,CACzB,CAEU,YAAYiB,EAAoCC,EAAoC,CAI5F,GAHI,MAAM,QAAQA,CAAS,IACzBA,EAAYA,EAAU,OAAS,EAAIA,EAAU,CAAC,EAAI,QAEhD,CAACA,EACH,OAEF,IAAMC,EAAqB,KAAK,MAAMD,CAAS,EAE/CE,EAAgBH,EAASE,CAAM,CACjC,CAEF","names":["ParsedRequestWhereBuilder","_ParsedRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","isValid","value","__name","parseCrudSearch","builder","cond","context","keys","$or","$and","innerFields","andBuilder","c","parseCrudSearchFields","orBuilder","__name","fields","name","field","isValid","fieldPath","f","parseCrudSearchField","operatorMap","key","value","val","CrudRequestParser","__name","arraySchema","query","select","relations","ordering","where","ParsedRequestWhereBuilder","limit","offset","rawSelect","field","requestFields","rawJoin","value","join","fieldPath","rawOrder","direction","raw","isValid","num","rawLimit","rawOffset","rawPage","page","builder","rawSearch","search","parseCrudSearch"]}
|
1
|
+
{"version":3,"sources":["../../../src/utils/functions.ts","../../../src/utils/where.ts","../../../src/utils/crud-request-where.builder.ts","../../../src/parsers/crud/parseCrudSearch.ts","../../../src/parsers/crud/crud-request.parser.ts"],"sourcesContent":["import { CrudRequestFields } from '../models/crud-request';\r\n\r\n/*export function setFieldByPath<T>(obj: ParsedRequestFields<T>, field: string, value: T): void {\r\n const parts = field.split('.');\r\n\r\n while (parts.length > 1) {\r\n const name = parts.shift();\r\n\r\n if (!Array.isArray(obj[name]))\r\n obj[name] = {};\r\n\r\n obj = obj[name] as ParsedRequestFields<T>;\r\n }\r\n\r\n obj[parts.shift()] = value;\r\n}*/\r\n\r\nexport function ensurePrimitive(fieldName: string, data: any) {\r\n if (data === null || data === undefined)\r\n return;\r\n\r\n if (typeof data === 'number' || typeof data === 'string' || typeof data === 'boolean')\r\n return;\r\n\r\n if (data instanceof Date)\r\n return;\r\n\r\n throw new Error(`${fieldName} must be a string, number, boolean or null`);\r\n}\r\n\r\nexport function ensureArray<T>(fieldName: string, data: T[] | any, minLength: number = 0): T[] {\r\n if (!Array.isArray(data) || data.length < minLength)\r\n throw new Error(`${fieldName} must be an array with at least ${minLength} items`);\r\n\r\n return data;\r\n}\r\n\r\nexport function ensureFalsy(fieldName: string, data: any) {\r\n if (data)\r\n throw new Error(`${fieldName} must be null`);\r\n}\r\n\r\nexport function isValid<T>(value: T | undefined | null): value is T {\r\n return value !== null && value !== undefined;\r\n}\r\n","import { CrudRequestWhereField, CrudRequestWhereOperator } from '../models/crud-request-where';\r\nimport { ensureArray, ensureFalsy, ensurePrimitive } from './functions';\r\n\r\nexport enum WhereOperatorValueType {\r\n PRIMITIVE = 'primitive',\r\n ARRAY = 'array',\r\n EMPTY = 'empty'\r\n}\r\n\r\nconst operatorValueTypes: Record<CrudRequestWhereOperator, WhereOperatorValueType> = {\r\n [CrudRequestWhereOperator.EQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LT]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.GTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.LTE]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.NOT_IN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.BETWEEN]: WhereOperatorValueType.ARRAY,\r\n [CrudRequestWhereOperator.IS_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.NOT_NULL]: WhereOperatorValueType.EMPTY,\r\n [CrudRequestWhereOperator.EQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NEQ_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.STARTS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.ENDS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_CONTAINS_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n [CrudRequestWhereOperator.NOT_IN_LOWER]: WhereOperatorValueType.PRIMITIVE,\r\n};\r\n\r\nexport function getWhereOperatorValueType(op: CrudRequestWhereOperator): WhereOperatorValueType {\r\n return operatorValueTypes[op];\r\n}\r\n\r\nexport function validateWhereField(where: CrudRequestWhereField): void {\r\n const type = operatorValueTypes[where.operator];\r\n const name = 'The value of the operator ' + where.operator;\r\n\r\n if (type === WhereOperatorValueType.PRIMITIVE) {\r\n ensurePrimitive(name, where.value);\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.ARRAY) {\r\n const items = ensureArray(name, where.value);\r\n\r\n items.forEach(item => ensurePrimitive(name + ' children', item));\r\n\r\n return;\r\n }\r\n\r\n if (type === WhereOperatorValueType.EMPTY) {\r\n ensureFalsy(name, where.value);\r\n\r\n return;\r\n }\r\n}\r\n","import {\r\n CrudRequestWhere,\r\n CrudRequestWhereAND,\r\n CrudRequestWhereField,\r\n CrudRequestWhereOperator,\r\n CrudRequestWhereOR,\r\n CrudRequestWhereValueType\r\n} from '../models/crud-request-where';\r\nimport { validateWhereField } from './where';\r\n\r\n/**\r\n * A helper class that makes it easier to create a CrudRequestWhere\r\n */\r\nexport class CrudRequestWhereBuilder {\r\n\r\n constructor(\r\n private readonly where: CrudRequestWhereAND | CrudRequestWhereOR = { and: [] },\r\n private readonly parent?: CrudRequestWhereBuilder,\r\n ) { }\r\n\r\n /**\r\n * Adds an AND bracket\r\n */\r\n public addAnd(): CrudRequestWhereBuilder {\r\n if (!this.where.or) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereAND = { and: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.or.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds an OR bracket\r\n */\r\n public addOr(): CrudRequestWhereBuilder {\r\n if (!this.where.and) {\r\n return this;\r\n }\r\n\r\n const inside: CrudRequestWhereOR = { or: [] };\r\n const builder = new CrudRequestWhereBuilder(inside, this);\r\n\r\n this.where.and.push(inside);\r\n\r\n return builder;\r\n }\r\n\r\n /**\r\n * Adds a field comparison\r\n *\r\n * @param field The field path\r\n * @param operator The comparison operator\r\n * @param value The value to compare\r\n */\r\n public addField(\r\n field: string[],\r\n operator: CrudRequestWhereOperator,\r\n value: CrudRequestWhereValueType | CrudRequestWhereValueType[],\r\n ): CrudRequestWhereBuilder {\r\n const whereField: CrudRequestWhereField = {\r\n field,\r\n operator,\r\n value,\r\n };\r\n\r\n validateWhereField(whereField);\r\n\r\n if (this.where.and) {\r\n this.where.and.push(whereField);\r\n } else if (this.where.or) {\r\n this.where.or.push(whereField);\r\n } else {\r\n throw new Error('Invalid where');\r\n }\r\n\r\n return this;\r\n }\r\n\r\n /**\r\n * Constructs the final where condition\r\n */\r\n public build(): CrudRequestWhere {\r\n if (this.parent) {\r\n return this.parent.build();\r\n }\r\n\r\n return this.where;\r\n }\r\n\r\n}\r\n","import { CrudRequestWhereBuilder } from '../../utils/crud-request-where.builder';\r\nimport { CrudRequestWhereOperator } from '../../models/crud-request-where';\r\nimport { SCondition, SField, SFieldOperator, SFields } from './types';\r\nimport { isValid } from '../../utils/functions';\r\n\r\ntype OperatorType = Exclude<Exclude<keyof SFieldOperator, '$or'>, '$and'>;\r\n\r\n// Operator\r\n// { field: { $gte: 10 } }\r\nconst operatorMap: Record<OperatorType, CrudRequestWhereOperator> = {\r\n $eq: CrudRequestWhereOperator.EQ,\r\n $ne: CrudRequestWhereOperator.NEQ,\r\n $gt: CrudRequestWhereOperator.GT,\r\n $lt: CrudRequestWhereOperator.LT,\r\n $gte: CrudRequestWhereOperator.GTE,\r\n $lte: CrudRequestWhereOperator.LTE,\r\n $starts: CrudRequestWhereOperator.STARTS,\r\n $ends: CrudRequestWhereOperator.ENDS,\r\n $cont: CrudRequestWhereOperator.CONTAINS,\r\n $excl: CrudRequestWhereOperator.NOT_CONTAINS,\r\n $in: CrudRequestWhereOperator.IN,\r\n $notin: CrudRequestWhereOperator.NOT_IN,\r\n $between: CrudRequestWhereOperator.BETWEEN,\r\n $isnull: CrudRequestWhereOperator.IS_NULL,\r\n $notnull: CrudRequestWhereOperator.NOT_NULL,\r\n $eqL: CrudRequestWhereOperator.EQ_LOWER,\r\n $neL: CrudRequestWhereOperator.NEQ_LOWER,\r\n $startsL: CrudRequestWhereOperator.STARTS_LOWER,\r\n $endsL: CrudRequestWhereOperator.ENDS_LOWER,\r\n $contL: CrudRequestWhereOperator.CONTAINS_LOWER,\r\n $exclL: CrudRequestWhereOperator.NOT_CONTAINS_LOWER,\r\n $inL: CrudRequestWhereOperator.IN_LOWER,\r\n $notinL: CrudRequestWhereOperator.NOT_IN_LOWER,\r\n};\r\n\r\n/**\r\n * Parses a crud request condition and inserts into a CrudRequestWhereBuilder\r\n *\r\n * @param builder The builder that the condition will be inserted to\r\n * @param cond The condition that must be parsed\r\n * @param context The field context\r\n */\r\nexport function parseCrudSearch(builder: CrudRequestWhereBuilder, cond: SCondition, context: string[] = []) {\r\n if (typeof cond !== 'object')\r\n return;\r\n\r\n const keys = Object.keys(cond);\r\n\r\n if (keys.length === 0)\r\n return;\r\n\r\n const { $or, $and, ...innerFields } = cond;\r\n\r\n // { $and: [..., ...] }\r\n if ($and) {\r\n const andBuilder = $and.length > 1 || keys.length > 1 ? builder.addAnd() : builder;\r\n\r\n $and.forEach(c => parseCrudSearch(andBuilder, c, context));\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n return;\r\n }\r\n\r\n // { $or: [..., ...] }\r\n if ($or && keys.length === 1) {\r\n const orBuilder = $or.length > 1 ? builder.addOr() : builder;\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n // { $or: [...], field1: ..., field2: ... }\r\n if ($or) {\r\n const andBuilder = builder.addAnd();\r\n const orBuilder = $or.length > 1 ? andBuilder.addOr() : andBuilder;\r\n\r\n parseCrudSearchFields(andBuilder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n\r\n $or.forEach(c => parseCrudSearch(orBuilder, c, context));\r\n\r\n return;\r\n }\r\n\r\n if (keys.length > 1)\r\n builder = builder.addAnd();\r\n\r\n parseCrudSearchFields(builder, innerFields as Omit<Omit<SFields, '$or'>, '$and'>, context);\r\n}\r\n\r\n/**\r\n * Parses an object from the crud request full of fields\r\n *\r\n * @param builder The builder\r\n * @param fields The fields object\r\n * @param context The context\r\n */\r\nfunction parseCrudSearchFields(builder: CrudRequestWhereBuilder, fields: Omit<Omit<SFields, '$or'>, '$and'>, context: string[]): void {\r\n // { name: 'John', age: { $gte: 18 }, 'posts.name': { $cont: 'Greetings' } }\r\n for (const name of Object.keys(fields)) {\r\n const field = fields[name];\r\n\r\n if (!isValid(field))\r\n continue;\r\n\r\n const fieldPath = [...context, ...name.split('.')];\r\n\r\n // { name: [{ $or: [...] }] }\r\n if (Array.isArray(field)) {\r\n field.forEach(f => parseCrudSearch(builder, f, fieldPath));\r\n continue;\r\n }\r\n\r\n parseCrudSearchField(builder, fieldPath, field);\r\n }\r\n}\r\n\r\n/**\r\n * Parses a single field\r\n *\r\n * @param builder The builder\r\n * @param name The name path\r\n * @param field The field value or operator\r\n */\r\nfunction parseCrudSearchField(builder: CrudRequestWhereBuilder, name: string[], field: SField): void {\r\n // Primitive Value\r\n // { field: 'text', field2: 12, field3: true }\r\n if (typeof field !== 'object') {\r\n builder.addField(name, CrudRequestWhereOperator.EQ, field);\r\n return;\r\n }\r\n\r\n const keys = Object.keys(field) as OperatorType[];\r\n\r\n for (let key of keys) {\r\n if (!operatorMap[key])\r\n continue;\r\n\r\n builder.addField(name, operatorMap[key], field[key]);\r\n }\r\n\r\n if (field.$or) {\r\n parseCrudSearchField(builder.addOr(), name, field.$or);\r\n }\r\n}\r\n\r\nexport function parseCrudFilters(builder: CrudRequestWhereBuilder, andFilters: string[], orFilters: string[]): void {\r\n // Based on rules from https://github.com/nestjsx/crud/wiki/Requests#or\r\n // \"If present both or and filter in any amount (one or miltiple each) then both interpreted\r\n // as a combitation of AND conditions and compared with each other by OR condition\"\r\n // \"If there are one or and one filter then it will be interpreted as OR condition\"\r\n if (andFilters.length > 0 && orFilters.length > 0) {\r\n const or = builder.addOr();\r\n\r\n parseCrudFilter(or.addAnd(), andFilters);\r\n parseCrudFilter(or.addAnd(), orFilters);\r\n\r\n return;\r\n }\r\n\r\n // \"If there are multiple or present (without filter) then it will be interpreted as a compination of OR conditions\"\r\n if (orFilters.length > 0) {\r\n parseCrudFilter(builder.addOr(), orFilters);\r\n\r\n return;\r\n }\r\n\r\n if (andFilters.length > 0) {\r\n parseCrudFilter(builder.addAnd(), andFilters);\r\n\r\n return;\r\n }\r\n}\r\n\r\nfunction parseCrudFilter(builder: CrudRequestWhereBuilder, rawFilters: string[]): void {\r\n for (const rawFilter of rawFilters) {\r\n const [name, op, value] = rawFilter.split('||', 3);\r\n const operator = operatorMap[op as OperatorType];\r\n\r\n if (!operator)\r\n continue;\r\n\r\n builder.addField(name.split('.'), operator, value);\r\n }\r\n}\r\n","import { RequestParamValue, RequestParser } from '../../models/request-parser';\r\nimport { CrudRequest, CrudRequestOrder, CrudRequestRelation, ParsedRequestSelect } from '../../models/crud-request';\r\nimport { OpenAPIParameter } from '../../models/openapi-parameter';\r\nimport { CrudRequestWhereBuilder } from '../../utils/crud-request-where.builder';\r\nimport { parseCrudFilters, parseCrudSearch } from './parseCrudSearch';\r\nimport { isValid } from '../../utils/functions';\r\nimport { SCondition } from './types';\r\n\r\n/**\r\n * Parses a request based on the @nestjsx/crud format.\r\n */\r\nexport class CrudRequestParser implements RequestParser {\r\n\r\n public getOpenAPIParameters(): OpenAPIParameter[] {\r\n // TODO improve docs\r\n\r\n const arraySchema = {\r\n type: 'array',\r\n items: {\r\n type: 'string',\r\n },\r\n };\r\n\r\n return [\r\n {\r\n name: 'fields',\r\n description: 'Selects resource fields',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: false,\r\n },\r\n {\r\n name: 's',\r\n description: 'Search condition',\r\n required: false,\r\n schema: {\r\n type: 'string',\r\n },\r\n },\r\n {\r\n name: 'sort',\r\n description: 'Sorting',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'join',\r\n description: 'Relations',\r\n required: false,\r\n schema: arraySchema,\r\n style: 'form',\r\n explode: true,\r\n },\r\n {\r\n name: 'limit',\r\n description: 'Page limit',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'offset',\r\n description: 'Offset amount',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n {\r\n name: 'page',\r\n description: 'Page number',\r\n required: false,\r\n schema: {\r\n type: 'integer',\r\n },\r\n },\r\n ];\r\n }\r\n\r\n public parse(query: Record<string, RequestParamValue>): CrudRequest {\r\n const select: ParsedRequestSelect = [];\r\n const relations: CrudRequestRelation[] = [];\r\n const ordering: CrudRequestOrder[] = [];\r\n const where = new CrudRequestWhereBuilder();\r\n\r\n this.parseSelect(select, query['fields'] || query['select']);\r\n this.parseJoin(select, relations, query['join']);\r\n this.parseOrder(ordering, query['sort']);\r\n this.parseSearch(where, query['s'], query['filter'], query['or']);\r\n\r\n const { limit, offset } = this.parseLimits(query['limit'] || query['per_page'], query['offset'], query['page']);\r\n\r\n return {\r\n select,\r\n relations,\r\n order: ordering,\r\n where: where.build(),\r\n limit,\r\n offset,\r\n };\r\n }\r\n\r\n protected parseSelect(select: ParsedRequestSelect, rawSelect: RequestParamValue): void {\r\n if (!rawSelect)\r\n return;\r\n\r\n const selectFields = Array.isArray(rawSelect) ? rawSelect : rawSelect.split(',');\r\n\r\n selectFields.forEach(field => {\r\n select.push({\r\n field: field.split('.'),\r\n });\r\n });\r\n }\r\n\r\n protected parseJoin(requestFields: ParsedRequestSelect, relations: CrudRequestRelation[], rawJoin: RequestParamValue): void {\r\n if (!rawJoin)\r\n return;\r\n\r\n if (Array.isArray(rawJoin)) {\r\n rawJoin.forEach(value => this.parseJoin(requestFields, relations, value));\r\n return;\r\n }\r\n\r\n const join = rawJoin.toString();\r\n\r\n const [field, select] = join.split('||', 2);\r\n const fieldPath = field.split('.');\r\n\r\n relations.push({\r\n field: fieldPath,\r\n });\r\n\r\n if (select) {\r\n const selectFields = select.split(',');\r\n\r\n selectFields.forEach(f => {\r\n requestFields.push({\r\n field: [...fieldPath, f],\r\n });\r\n });\r\n }\r\n }\r\n\r\n protected parseOrder(ordering: CrudRequestOrder[], rawOrder: RequestParamValue): void {\r\n if (!rawOrder)\r\n return;\r\n\r\n if (Array.isArray(rawOrder)) {\r\n rawOrder.forEach(value => this.parseOrder(ordering, value));\r\n return;\r\n }\r\n\r\n const [field, direction] = rawOrder.split(',');\r\n\r\n ordering.push({\r\n field: field.split('.'),\r\n order: direction?.toUpperCase() === 'DESC' ? 'DESC' : 'ASC',\r\n });\r\n }\r\n\r\n protected parseNumber(raw: RequestParamValue): number | undefined {\r\n if (Array.isArray(raw))\r\n raw = raw.length > 0 ? raw[0] : undefined;\r\n\r\n if (!isValid(raw))\r\n return undefined;\r\n\r\n const num = +raw;\r\n\r\n return isNaN(num) ? undefined : num;\r\n }\r\n\r\n protected parseLimits(rawLimit: RequestParamValue, rawOffset: RequestParamValue, rawPage: RequestParamValue) {\r\n const limit = this.parseNumber(rawLimit);\r\n let offset = this.parseNumber(rawOffset);\r\n\r\n if (limit && !offset) {\r\n const page = this.parseNumber(rawPage);\r\n\r\n if (page)\r\n offset = limit * page;\r\n }\r\n\r\n return { limit, offset };\r\n }\r\n\r\n protected parseSearch(builder: CrudRequestWhereBuilder, rawSearch: RequestParamValue, rawFilter: RequestParamValue, rawOr: RequestParamValue): void {\r\n if (Array.isArray(rawSearch))\r\n rawSearch = rawSearch.length > 0 ? rawSearch[0] : undefined;\r\n\r\n if (!rawSearch) {\r\n // In case the search is not defined, we'll read the filter and or parameters\r\n this.parseFilter(builder, rawFilter, rawOr);\r\n return;\r\n }\r\n\r\n const search: SCondition = JSON.parse(rawSearch);\r\n\r\n parseCrudSearch(builder, search);\r\n }\r\n\r\n protected parseFilter(builder: CrudRequestWhereBuilder, rawFilter: RequestParamValue, rawOr: RequestParamValue): void {\r\n let andFilters: string[] = [];\r\n let orFilters: string[] = [];\r\n\r\n if (rawFilter)\r\n andFilters = Array.isArray(rawFilter) ? rawFilter : [rawFilter];\r\n\r\n if (rawOr)\r\n orFilters = Array.isArray(rawOr) ? rawOr : [rawOr];\r\n\r\n parseCrudFilters(builder, andFilters, orFilters);\r\n }\r\n\r\n}\r\n"],"mappings":"+EAiBO,SAASA,EAAgBC,EAAmBC,EAAW,CAC5D,GAAIA,GAAS,MAGT,SAAOA,GAAS,UAAY,OAAOA,GAAS,UAAY,OAAOA,GAAS,YAGxE,EAAAA,aAAgB,MAGpB,MAAM,IAAI,MAAM,GAAGD,CAAS,4CAA4C,CAC1E,CAXgBE,EAAAH,EAAA,mBAaT,SAASI,EAAeH,EAAmBC,EAAiBG,EAAoB,EAAQ,CAC7F,GAAI,CAAC,MAAM,QAAQH,CAAI,GAAKA,EAAK,OAASG,EACxC,MAAM,IAAI,MAAM,GAAGJ,CAAS,mCAAmCI,CAAS,QAAQ,EAElF,OAAOH,CACT,CALgBC,EAAAC,EAAA,eAOT,SAASE,EAAYL,EAAmBC,EAAW,CACxD,GAAIA,EACF,MAAM,IAAI,MAAM,GAAGD,CAAS,eAAe,CAC/C,CAHgBE,EAAAG,EAAA,eAKT,SAASC,EAAWC,EAAyC,CAClE,OAAOA,GAAU,IACnB,CAFgBL,EAAAI,EAAA,WCjChB,IAAME,EAA+E,CAClF,GAA8B,YAC9B,IAA+B,YAC/B,GAA8B,YAC9B,GAA8B,YAC9B,IAA+B,YAC/B,IAA+B,YAC/B,OAAkC,YAClC,KAAgC,YAChC,SAAoC,YACpC,aAAwC,YACxC,GAA8B,QAC9B,OAAkC,QAClC,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ID,EAAS,MAAM,GAAG,EAE7Cb,EAAS,KAAK,CACZ,MAAOM,EAAM,MAAM,GAAG,EACtB,MAAOQ,GAAW,YAAY,IAAM,OAAS,OAAS,KACxD,CAAC,CACH,CAEU,YAAYC,EAA4C,CAIhE,GAHI,MAAM,QAAQA,CAAG,IACnBA,EAAMA,EAAI,OAAS,EAAIA,EAAI,CAAC,EAAI,QAE9B,CAACC,EAAQD,CAAG,EACd,OAEF,IAAME,EAAM,CAACF,EAEb,OAAO,MAAME,CAAG,EAAI,OAAYA,CAClC,CAEU,YAAYC,EAA6BC,EAA8BC,EAA4B,CAC3G,IAAMjB,EAAQ,KAAK,YAAYe,CAAQ,EACnCd,EAAS,KAAK,YAAYe,CAAS,EAEvC,GAAIhB,GAAS,CAACC,EAAQ,CACpB,IAAMiB,EAAO,KAAK,YAAYD,CAAO,EAEjCC,IACFjB,EAASD,EAAQkB,EACrB,CAEA,MAAO,CAAE,MAAAlB,EAAO,OAAAC,CAAO,CACzB,CAEU,YAAYkB,EAAkCC,EAA8BC,EAA8BC,EAAgC,CAIlJ,GAHI,MAAM,QAAQF,CAAS,IACzBA,EAAYA,EAAU,OAAS,EAAIA,EAAU,CAAC,EAAI,QAEhD,CAACA,EAAW,CAEd,KAAK,YAAYD,EAASE,EAAWC,CAAK,EAC1C,MACF,CAEA,IAAMC,EAAqB,KAAK,MAAMH,CAAS,EAE/CI,EAAgBL,EAASI,CAAM,CACjC,CAEU,YAAYJ,EAAkCE,EAA8BC,EAAgC,CACpH,IAAIG,EAAuB,CAAC,EACxBC,EAAsB,CAAC,EAEvBL,IACFI,EAAa,MAAM,QAAQJ,CAAS,EAAIA,EAAY,CAACA,CAAS,GAE5DC,IACFI,EAAY,MAAM,QAAQJ,CAAK,EAAIA,EAAQ,CAACA,CAAK,GAEnDK,EAAiBR,EAASM,EAAYC,CAAS,CACjD,CAEF","names":["ensurePrimitive","fieldName","data","__name","ensureArray","minLength","ensureFalsy","isValid","value","operatorValueTypes","validateWhereField","where","type","operatorValueTypes","name","ensurePrimitive","ensureArray","item","ensureFalsy","__name","CrudRequestWhereBuilder","_CrudRequestWhereBuilder","where","parent","__name","inside","builder","field","operator","value","whereField","validateWhereField","operatorMap","parseCrudSearch","builder","cond","context","keys","$or","$and","innerFields","andBuilder","c","parseCrudSearchFields","orBuilder","__name","fields","name","field","isValid","fieldPath","f","parseCrudSearchField","key","parseCrudFilters","andFilters","orFilters","or","parseCrudFilter","rawFilters","rawFilter","op","value","operator","CrudRequestParser","__name","arraySchema","query","select","relations","ordering","where","CrudRequestWhereBuilder","limit","offset","rawSelect","field","requestFields","rawJoin","value","join","fieldPath","f","rawOrder","direction","raw","isValid","num","rawLimit","rawOffset","rawPage","page","builder","rawSearch","rawFilter","rawOr","search","parseCrudSearch","andFilters","orFilters","parseCrudFilters"]}
|
package/package.json
CHANGED
@@ -1,12 +1,26 @@
|
|
1
1
|
{
|
2
2
|
"name": "crud-query-parser",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.3",
|
4
4
|
"description": "Parses HTTP requests and converts them into database queries",
|
5
|
+
"keywords": [
|
6
|
+
"crud",
|
7
|
+
"http",
|
8
|
+
"query",
|
9
|
+
"request",
|
10
|
+
"parser",
|
11
|
+
"typeorm",
|
12
|
+
"nestjs"
|
13
|
+
],
|
5
14
|
"license": "MIT",
|
6
15
|
"author": "Guilherme Chaguri",
|
16
|
+
"repository": {
|
17
|
+
"type": "git",
|
18
|
+
"url": "https://github.com/Guichaguri/crud-query-parser.git"
|
19
|
+
},
|
7
20
|
"scripts": {
|
8
21
|
"build": "tsup",
|
9
|
-
"prepublishOnly": "npm run build"
|
22
|
+
"prepublishOnly": "npm run build",
|
23
|
+
"sample:typeorm": "ts-node ./samples/typeorm/index.ts"
|
10
24
|
},
|
11
25
|
"tsup": {
|
12
26
|
"entry": [
|
@@ -111,6 +125,7 @@
|
|
111
125
|
"@types/node": "^17.0.23",
|
112
126
|
"reflect-metadata": "^0.2.2",
|
113
127
|
"sqlite3": "^5.1.7",
|
128
|
+
"ts-node": "^10.9.2",
|
114
129
|
"tsup": "^8.1.0",
|
115
130
|
"typeorm": "^0.3.20",
|
116
131
|
"typescript": "^5.5.2"
|