@ackplus/nest-crud-request 0.1.17 → 0.1.19

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ackplus/nest-crud-request",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -4,7 +4,7 @@ export declare class QueryBuilder {
4
4
  private options;
5
5
  private whereBuilder;
6
6
  private relationBuilder;
7
- constructor(options: QueryBuilderOptions);
7
+ constructor(options?: QueryBuilderOptions);
8
8
  setOptions(options: QueryBuilderOptions): this;
9
9
  mergeOptions(options: QueryBuilderOptions, deep?: boolean): this;
10
10
  addSelect(fields: string | string[]): this;
@@ -24,8 +24,8 @@ export declare class QueryBuilder {
24
24
  toObject(): {
25
25
  [x: string]: any;
26
26
  select?: string[];
27
- relations?: string[] | import("./types").Relation[];
28
- where?: Record<string, any>;
27
+ relations?: import("./types").RelationOptions;
28
+ where?: import("./types").WhereOptions;
29
29
  order?: Record<string, OrderDirectionEnum>;
30
30
  skip?: number;
31
31
  take?: number;
@@ -9,8 +9,9 @@ class QueryBuilder {
9
9
  this.options = {};
10
10
  this.whereBuilder = new where_builder_1.WhereBuilder();
11
11
  this.relationBuilder = new relation_builder_1.RelationBuilder();
12
- this.options = options;
13
- this.setOptions(options);
12
+ if (options) {
13
+ this.setOptions(options);
14
+ }
14
15
  }
15
16
  setOptions(options) {
16
17
  this.options = options;
@@ -1,12 +1,12 @@
1
- import { Relation } from './types';
1
+ import { RelationObject, RelationOptions } from './types';
2
2
  export declare class RelationBuilder {
3
3
  private relations;
4
- constructor(relations?: Relation[] | string[] | string);
5
- setRelations(relations: Relation[] | string[] | string): this;
4
+ constructor(relations?: RelationOptions);
5
+ setRelations(relations: RelationOptions): this;
6
6
  clear(): this;
7
7
  add(relation: string, select?: string[], where?: Record<string, any>): this;
8
8
  remove(relation: string): this;
9
9
  hasRelations(): boolean;
10
- toObject(): Relation[];
10
+ toObject(): RelationObject;
11
11
  toJson(): string;
12
12
  }
@@ -12,12 +12,7 @@ class RelationBuilder {
12
12
  if (relations) {
13
13
  if (Array.isArray(relations)) {
14
14
  relations.forEach(relation => {
15
- if (typeof relation === 'string') {
16
- this.add(relation);
17
- }
18
- else {
19
- this.add(relation.relation, relation.select, relation.where);
20
- }
15
+ this.add(relation);
21
16
  });
22
17
  }
23
18
  else if (typeof relations === 'string') {
@@ -34,11 +29,19 @@ class RelationBuilder {
34
29
  return this;
35
30
  }
36
31
  add(relation, select, where) {
37
- this.relations[relation] = {
38
- relation,
39
- select,
40
- where,
41
- };
32
+ if (!select && !where) {
33
+ this.relations[relation] = true;
34
+ }
35
+ else {
36
+ const obj = {};
37
+ if (select) {
38
+ obj.select = select;
39
+ }
40
+ if (where) {
41
+ obj.where = where;
42
+ }
43
+ this.relations[relation] = obj;
44
+ }
42
45
  return this;
43
46
  }
44
47
  remove(relation) {
@@ -49,7 +52,7 @@ class RelationBuilder {
49
52
  return Object.keys(this.relations).length > 0;
50
53
  }
51
54
  toObject() {
52
- return Object.values(this.relations);
55
+ return this.relations;
53
56
  }
54
57
  toJson() {
55
58
  return JSON.stringify(this.relations);
@@ -1,8 +1,8 @@
1
1
  export interface QueryBuilderOptions {
2
2
  [key: string]: any;
3
3
  select?: string[];
4
- relations?: string[] | Relation[];
5
- where?: Record<string, any>;
4
+ relations?: RelationOptions;
5
+ where?: WhereOptions;
6
6
  order?: Record<string, OrderDirectionEnum>;
7
7
  skip?: number;
8
8
  take?: number;
@@ -39,8 +39,16 @@ export declare enum OrderDirectionEnum {
39
39
  ASC = "ASC",
40
40
  DESC = "DESC"
41
41
  }
42
- export interface Relation {
43
- relation: string;
42
+ export type WhereObject = {
43
+ [key: string]: any;
44
+ $and?: WhereObject | WhereObject[];
45
+ $or?: WhereObject | WhereObject[];
46
+ };
47
+ export type WhereOptions = WhereObject | WhereObject[];
48
+ export type RelationObjectValue = {
44
49
  select?: string[];
45
- where?: Record<string, any>;
46
- }
50
+ where?: WhereObject | WhereObject[];
51
+ joinType?: 'left' | 'inner';
52
+ };
53
+ export type RelationObject = Record<string, RelationObjectValue | boolean>;
54
+ export type RelationOptions = string | string[] | RelationObject;
@@ -3,6 +3,7 @@ export type WhereBuilderCondition = [string, any] | [string, WhereOperatorEnum,
3
3
  export declare class WhereBuilder {
4
4
  private whereObject;
5
5
  constructor(where?: Record<string, any>);
6
+ private isOperator;
6
7
  clear(): this;
7
8
  where(...args: WhereBuilderCondition): this;
8
9
  andWhere(...args: WhereBuilderCondition): this;
@@ -13,4 +14,5 @@ export declare class WhereBuilder {
13
14
  toJson(): string;
14
15
  private parseCondition;
15
16
  private updateCondition;
17
+ private mergeConditions;
16
18
  }
@@ -7,6 +7,9 @@ class WhereBuilder {
7
7
  this.whereObject = {};
8
8
  this.whereObject = where || {};
9
9
  }
10
+ isOperator(value) {
11
+ return `${value}`.startsWith('$');
12
+ }
10
13
  clear() {
11
14
  this.whereObject = {};
12
15
  return this;
@@ -77,6 +80,9 @@ class WhereBuilder {
77
80
  this.updateCondition({ [field]: { [types_1.WhereOperatorEnum.EQ]: value } }, type);
78
81
  }
79
82
  }
83
+ else if (this.isOperator(value)) {
84
+ this.updateCondition({ [field]: { [value]: true } }, type);
85
+ }
80
86
  else {
81
87
  this.updateCondition({ [field]: { [types_1.WhereOperatorEnum.EQ]: value } }, type);
82
88
  }
@@ -92,11 +98,32 @@ class WhereBuilder {
92
98
  }
93
99
  updateCondition(condition, type) {
94
100
  if (type === null) {
95
- this.whereObject = Object.assign(Object.assign({}, this.whereObject), condition);
101
+ this.mergeConditions(this.whereObject, condition);
96
102
  }
97
103
  else {
98
104
  this.whereObject[type] = [...(this.whereObject[type] || []), condition].filter(Boolean);
99
105
  }
100
106
  }
107
+ mergeConditions(target, source) {
108
+ for (const key in source) {
109
+ const sourceValue = source[key];
110
+ if (key === '$and' || key === '$or') {
111
+ if (target[key]) {
112
+ target[key] = [...(target[key] || []), ...(Array.isArray(sourceValue) ? sourceValue : [sourceValue])];
113
+ }
114
+ else {
115
+ target[key] = Array.isArray(sourceValue) ? sourceValue : [sourceValue];
116
+ }
117
+ }
118
+ else {
119
+ if (target[key] && typeof target[key] === 'object' && typeof sourceValue === 'object') {
120
+ target[key] = Object.assign(Object.assign({}, target[key]), sourceValue);
121
+ }
122
+ else {
123
+ target[key] = sourceValue;
124
+ }
125
+ }
126
+ }
127
+ }
101
128
  }
102
129
  exports.WhereBuilder = WhereBuilder;