@ackplus/nest-crud-request 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # nest-crud-request
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build nest-crud-request` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test nest-crud-request` to execute the unit tests via [Jest](https://jestjs.io).
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib/query-builder';
2
+ export * from './lib/where-builder';
3
+ export * from './lib/types';
package/index.js ADDED
@@ -0,0 +1,137 @@
1
+ var w = (i, e) => () => (e || i((e = { exports: {} }).exports, e), e.exports);
2
+ var B = w((r) => {
3
+ Object.defineProperty(r, "__esModule", { value: !0 });
4
+ r.QueryBuilder = void 0;
5
+ const c = require("./relation-builder"), b = require("./utils"), a = require("./where-builder");
6
+ class j {
7
+ constructor(e) {
8
+ this.options = {}, this.whereBuilder = new a.WhereBuilder(), this.relationBuilder = new c.RelationBuilder(), this.options = e, this.setOptions(e);
9
+ }
10
+ setOptions(e) {
11
+ return this.options = e, this.whereBuilder = new a.WhereBuilder(e.where), this.relationBuilder = new c.RelationBuilder(e.relations), this;
12
+ }
13
+ mergeOptions(e, t = !1) {
14
+ let s = {};
15
+ return t ? s = (0, b.deepMerge)(this.options, e) : s = {
16
+ ...this.options,
17
+ ...e
18
+ }, this.setOptions(s), this;
19
+ }
20
+ addSelect(e) {
21
+ return this.options.select || (this.options.select = []), Array.isArray(e) ? this.options.select.push(...e) : this.options.select.push(e), this;
22
+ }
23
+ removeSelect(e) {
24
+ return this.options.select && (Array.isArray(e) ? this.options.select = this.options.select.filter((t) => !e.includes(t)) : this.options.select = this.options.select.filter((t) => t !== e)), this;
25
+ }
26
+ addRelation(e, t, s) {
27
+ return this.relationBuilder.add(e, t, s), this;
28
+ }
29
+ removeRelation(e) {
30
+ return this.relationBuilder.remove(e), this;
31
+ }
32
+ where(...e) {
33
+ return this.whereBuilder.where(...e), this;
34
+ }
35
+ andWhere(...e) {
36
+ return this.whereBuilder.andWhere(...e), this;
37
+ }
38
+ orWhere(...e) {
39
+ return this.whereBuilder.orWhere(...e), this;
40
+ }
41
+ addOrder(e, t) {
42
+ return this.options.order || (this.options.order = {}), this.options.order[e] = t, this;
43
+ }
44
+ removeOrder(e) {
45
+ return this.options.order && delete this.options.order[e], this;
46
+ }
47
+ setSkip(e) {
48
+ return this.options.skip = e, this;
49
+ }
50
+ setTake(e) {
51
+ return this.options.take = e, this;
52
+ }
53
+ toObject() {
54
+ const e = {
55
+ ...this.options
56
+ };
57
+ return this.whereBuilder.hasConditions() ? e.where = this.whereBuilder.toObject() : delete e.where, this.relationBuilder.hasRelations() ? e.relations = this.relationBuilder.toObject() : delete e.relations, e;
58
+ }
59
+ toJson() {
60
+ const e = this.toObject();
61
+ return JSON.stringify(e);
62
+ }
63
+ }
64
+ r.QueryBuilder = j;
65
+ Object.defineProperty(r, "__esModule", { value: !0 });
66
+ r.WhereBuilder = void 0;
67
+ const l = require("./types");
68
+ class u {
69
+ constructor(e) {
70
+ this.whereObject = {}, this.whereObject = e || {};
71
+ }
72
+ clear() {
73
+ return this.whereObject = {}, this;
74
+ }
75
+ where(...e) {
76
+ return this.parseCondition(null, ...e), this;
77
+ }
78
+ andWhere(...e) {
79
+ return this.parseCondition(l.WhereLogicalOperatorEnum.AND, ...e), this;
80
+ }
81
+ orWhere(...e) {
82
+ return this.parseCondition(l.WhereLogicalOperatorEnum.OR, ...e), this;
83
+ }
84
+ removeWhere(e) {
85
+ const t = e.split(".");
86
+ let s = this.whereObject;
87
+ for (let n = 0; n < t.length - 1; n++) {
88
+ if (!s[t[n]])
89
+ return this;
90
+ s = s[t[n]];
91
+ }
92
+ return delete s[t[t.length - 1]], this;
93
+ }
94
+ hasConditions() {
95
+ return Object.keys(this.whereObject).length > 0;
96
+ }
97
+ toObject() {
98
+ return this.whereObject;
99
+ }
100
+ toJson() {
101
+ return JSON.stringify(this.whereObject);
102
+ }
103
+ parseCondition(e, ...t) {
104
+ let s, n, o;
105
+ if (t.length !== 0) if (t.length === 1) {
106
+ const h = t[0];
107
+ if (typeof h == "function") {
108
+ const d = new u();
109
+ h(d), this.updateCondition(d.toObject(), e);
110
+ } else h instanceof u ? this.updateCondition(h.toObject(), e) : this.updateCondition(h, e);
111
+ } else (t.length === 2 || t.length === 3) && (t.length === 2 ? (s = t[0], o = t[1], typeof o == "object" ? Object.keys(o)[0].startsWith("$") ? this.updateCondition({ [s]: o }, e) : this.updateCondition({ [s]: { [l.WhereOperatorEnum.EQ]: o } }, e) : this.updateCondition({ [s]: { [l.WhereOperatorEnum.EQ]: o } }, e)) : (s = t[0], n = t[1], o = t[2], this.updateCondition({ [s]: { [n]: o } }, e)));
112
+ return this;
113
+ }
114
+ updateCondition(e, t) {
115
+ t === null ? this.whereObject = {
116
+ ...this.whereObject,
117
+ ...e
118
+ } : this.whereObject[t] = [...this.whereObject[t] || [], e].filter(Boolean);
119
+ }
120
+ }
121
+ r.WhereBuilder = u;
122
+ Object.defineProperty(r, "__esModule", { value: !0 });
123
+ r.OrderDirectionEnum = r.WhereOperatorEnum = r.WhereLogicalOperatorEnum = void 0;
124
+ var f;
125
+ (function(i) {
126
+ i.AND = "$and", i.OR = "$or";
127
+ })(f || (r.WhereLogicalOperatorEnum = f = {}));
128
+ var O;
129
+ (function(i) {
130
+ i.EQ = "$eq", i.NOT_EQ = "$ne", i.GT = "$gt", i.GT_OR_EQ = "$gte", i.LT = "$lt", i.LT_OR_EQ = "$lte", i.IN = "$in", i.NOT_IN = "$notIn", i.LIKE = "$like", i.NOT_LIKE = "$notLike", i.ILIKE = "$iLike", i.NOT_ILIKE = "$notIlike", i.IS_NULL = "$isNull", i.IS_NOT_NULL = "$isNotNull", i.BETWEEN = "$between", i.NOT_BETWEEN = "$notBetween", i.NULL = "$null", i.NOT_NULL = "$notNull", i.IS_TRUE = "$isTrue", i.IS_FALSE = "$isFalse";
131
+ })(O || (r.WhereOperatorEnum = O = {}));
132
+ var p;
133
+ (function(i) {
134
+ i.ASC = "ASC", i.DESC = "DESC";
135
+ })(p || (r.OrderDirectionEnum = p = {}));
136
+ });
137
+ export default B();
@@ -0,0 +1,30 @@
1
+ import { QueryBuilderOptions, OrderDirectionEnum } from './types';
2
+ import { WhereBuilderCondition } from './where-builder';
3
+ export declare class QueryBuilder {
4
+ private options;
5
+ private whereBuilder;
6
+ private relationBuilder;
7
+ constructor(options: QueryBuilderOptions);
8
+ setOptions(options: QueryBuilderOptions): this;
9
+ mergeOptions(options: QueryBuilderOptions, deep?: boolean): this;
10
+ addSelect(fields: string | string[]): this;
11
+ removeSelect(fields: string | string[]): this;
12
+ addRelation(relation: string, select?: string[], where?: Record<string, any>): this;
13
+ removeRelation(relation: string): this;
14
+ where(...args: WhereBuilderCondition): this;
15
+ andWhere(...args: WhereBuilderCondition): this;
16
+ orWhere(...args: WhereBuilderCondition): this;
17
+ addOrder(orderBy: string, order: OrderDirectionEnum): this;
18
+ removeOrder(orderBy: string): this;
19
+ setSkip(skip: number): this;
20
+ setTake(take: number): this;
21
+ toObject(): {
22
+ select?: string[];
23
+ relations?: string[] | import('./types').Relation[];
24
+ where?: Record<string, any>;
25
+ order?: Record<string, OrderDirectionEnum>;
26
+ skip?: number;
27
+ take?: number;
28
+ };
29
+ toJson(): string;
30
+ }
@@ -0,0 +1,12 @@
1
+ import { Relation } from './types';
2
+ export declare class RelationBuilder {
3
+ private relations;
4
+ constructor(relations?: Relation[] | string[] | string);
5
+ setRelations(relations: Relation[] | string[] | string): this;
6
+ clear(): this;
7
+ add(relation: string, select?: string[], where?: Record<string, any>): this;
8
+ remove(relation: string): this;
9
+ hasRelations(): boolean;
10
+ toObject(): Relation[];
11
+ toJson(): string;
12
+ }
package/lib/types.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ export interface QueryBuilderOptions {
2
+ select?: string[];
3
+ relations?: string[] | Relation[];
4
+ where?: Record<string, any>;
5
+ order?: Record<string, OrderDirectionEnum>;
6
+ skip?: number;
7
+ take?: number;
8
+ }
9
+ export declare enum WhereLogicalOperatorEnum {
10
+ AND = "$and",
11
+ OR = "$or"
12
+ }
13
+ export declare enum WhereOperatorEnum {
14
+ EQ = "$eq",
15
+ NOT_EQ = "$ne",
16
+ GT = "$gt",
17
+ GT_OR_EQ = "$gte",
18
+ LT = "$lt",
19
+ LT_OR_EQ = "$lte",
20
+ IN = "$in",
21
+ NOT_IN = "$notIn",
22
+ LIKE = "$like",
23
+ NOT_LIKE = "$notLike",
24
+ ILIKE = "$iLike",
25
+ NOT_ILIKE = "$notIlike",
26
+ IS_NULL = "$isNull",
27
+ IS_NOT_NULL = "$isNotNull",
28
+ BETWEEN = "$between",
29
+ NOT_BETWEEN = "$notBetween",
30
+ NULL = "$null",
31
+ NOT_NULL = "$notNull",
32
+ IS_TRUE = "$isTrue",
33
+ IS_FALSE = "$isFalse"
34
+ }
35
+ export declare enum OrderDirectionEnum {
36
+ ASC = "ASC",
37
+ DESC = "DESC"
38
+ }
39
+ export interface Relation {
40
+ relation: string;
41
+ select?: string[];
42
+ where?: Record<string, any>;
43
+ }
package/lib/utils.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function deepMerge(target: any, source: any): any;
@@ -0,0 +1,16 @@
1
+ import { WhereOperatorEnum } from './types';
2
+ export type WhereBuilderCondition = [string, any] | [string, WhereOperatorEnum, any] | [Record<string, any>] | [(builder: WhereBuilder) => void];
3
+ export declare class WhereBuilder {
4
+ private whereObject;
5
+ constructor(where?: Record<string, any>);
6
+ clear(): this;
7
+ where(...args: WhereBuilderCondition): this;
8
+ andWhere(...args: WhereBuilderCondition): this;
9
+ orWhere(...args: WhereBuilderCondition): this;
10
+ removeWhere(field: string): this;
11
+ hasConditions(): boolean;
12
+ toObject(): Record<string, any>;
13
+ toJson(): string;
14
+ private parseCondition;
15
+ private updateCondition;
16
+ }
package/package.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "name": "@ackplus/nest-crud-request",
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "types": "./index.d.ts",
7
+ "dependencies": {
8
+ "tslib": "^2.3.0"
9
+ }
10
+ }