@ackplus/nest-crud-request 0.1.32 → 0.1.34

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,10 +1,19 @@
1
1
  {
2
2
  "name": "@ackplus/nest-crud-request",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "type": "commonjs",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
7
7
  "license": "MIT",
8
+ "description": "Request parsing and query building utilities for @ackplus/nest-crud",
9
+ "keywords": [
10
+ "nestjs",
11
+ "crud",
12
+ "query",
13
+ "request",
14
+ "parser",
15
+ "builder"
16
+ ],
8
17
  "repository": {
9
18
  "type": "git",
10
19
  "url": "https://github.com/ack-solutions/packages.git"
@@ -21,12 +21,12 @@ export declare class QueryBuilder {
21
21
  setWithDeleted(withDeleted: boolean): this;
22
22
  setOnlyDeleted(onlyDeleted: boolean): this;
23
23
  set(key: string, value: any): this;
24
- toObject(): {
25
- [x: string]: any;
26
- select?: string[];
27
- relations?: import("./types").RelationOptions;
28
- where?: import("./types").WhereOptions;
29
- order?: Record<string, OrderDirectionEnum>;
24
+ toObject(constrainToNestedObject?: boolean): {
25
+ [key: string]: any;
26
+ select?: string[] | string;
27
+ relations?: import("./types").RelationOptions | string;
28
+ where?: import("./types").WhereOptions | string;
29
+ order?: Record<string, OrderDirectionEnum> | string;
30
30
  skip?: number;
31
31
  take?: number;
32
32
  withDeleted?: boolean;
@@ -31,7 +31,7 @@ class QueryBuilder {
31
31
  return this;
32
32
  }
33
33
  addSelect(fields) {
34
- if (!this.options.select) {
34
+ if (!this.options.select || typeof this.options.select === 'string') {
35
35
  this.options.select = [];
36
36
  }
37
37
  if (Array.isArray(fields)) {
@@ -43,7 +43,7 @@ class QueryBuilder {
43
43
  return this;
44
44
  }
45
45
  removeSelect(fields) {
46
- if (this.options.select) {
46
+ if (this.options.select && Array.isArray(this.options.select)) {
47
47
  if (Array.isArray(fields)) {
48
48
  this.options.select = this.options.select.filter(field => !fields.includes(field));
49
49
  }
@@ -106,24 +106,56 @@ class QueryBuilder {
106
106
  this.options[key] = value;
107
107
  return this;
108
108
  }
109
- toObject() {
109
+ toObject(constrainToNestedObject = false) {
110
110
  const options = Object.assign({}, this.options);
111
111
  if (this.whereBuilder.hasConditions()) {
112
- options.where = this.whereBuilder.toObject();
112
+ if (constrainToNestedObject) {
113
+ options.where = this.whereBuilder.toObject();
114
+ }
115
+ else {
116
+ options.where = JSON.stringify(this.whereBuilder.toObject());
117
+ }
113
118
  }
114
119
  else {
115
120
  delete options.where;
116
121
  }
117
122
  if (this.relationBuilder.hasRelations()) {
118
- options.relations = this.relationBuilder.toObject();
123
+ if (constrainToNestedObject) {
124
+ options.relations = this.relationBuilder.toObject();
125
+ }
126
+ else {
127
+ options.relations = JSON.stringify(this.relationBuilder.toObject());
128
+ }
119
129
  }
120
130
  else {
121
131
  delete options.relations;
122
132
  }
133
+ if (options.order && Object.keys(options.order).length > 0) {
134
+ if (constrainToNestedObject) {
135
+ options.order = options.order;
136
+ }
137
+ else {
138
+ options.order = JSON.stringify(options.order);
139
+ }
140
+ }
141
+ else {
142
+ delete options.order;
143
+ }
144
+ if (options.select && options.select.length > 0) {
145
+ if (constrainToNestedObject) {
146
+ options.select = options.select;
147
+ }
148
+ else {
149
+ options.select = JSON.stringify(options.select);
150
+ }
151
+ }
152
+ else {
153
+ delete options.select;
154
+ }
123
155
  return options;
124
156
  }
125
157
  toJson() {
126
- const obj = this.toObject();
158
+ const obj = this.toObject(true);
127
159
  return JSON.stringify(obj);
128
160
  }
129
161
  }
@@ -1,7 +1,7 @@
1
1
  import { RelationObject, RelationOptions } from './types';
2
2
  export declare class RelationBuilder {
3
3
  private relations;
4
- constructor(relations?: RelationOptions);
4
+ constructor(relations?: RelationOptions | string);
5
5
  setRelations(relations: RelationOptions): this;
6
6
  clear(): this;
7
7
  add(relation: string, select?: string[], where?: Record<string, any>): this;
@@ -5,7 +5,8 @@ class RelationBuilder {
5
5
  constructor(relations) {
6
6
  this.relations = {};
7
7
  if (relations) {
8
- this.setRelations(relations);
8
+ const relationOptions = typeof relations === 'string' ? JSON.parse(relations) : relations;
9
+ this.setRelations(relationOptions);
9
10
  }
10
11
  }
11
12
  setRelations(relations) {
@@ -1,9 +1,9 @@
1
1
  export interface QueryBuilderOptions {
2
2
  [key: string]: any;
3
- select?: string[];
4
- relations?: RelationOptions;
5
- where?: WhereOptions;
6
- order?: Record<string, OrderDirectionEnum>;
3
+ select?: string[] | string;
4
+ relations?: RelationOptions | string;
5
+ where?: WhereOptions | string;
6
+ order?: Record<string, OrderDirectionEnum> | string;
7
7
  skip?: number;
8
8
  take?: number;
9
9
  withDeleted?: boolean;
@@ -2,7 +2,7 @@ import { WhereOperatorEnum } from './types';
2
2
  export type WhereBuilderCondition = [string, any] | [string, WhereOperatorEnum, any] | [Record<string, any>] | [(builder: WhereBuilder) => void];
3
3
  export declare class WhereBuilder {
4
4
  private whereObject;
5
- constructor(where?: Record<string, any>);
5
+ constructor(where?: Record<string, any> | string);
6
6
  private isOperator;
7
7
  clear(): this;
8
8
  where(...args: WhereBuilderCondition): this;
@@ -5,7 +5,7 @@ const types_1 = require("./types");
5
5
  class WhereBuilder {
6
6
  constructor(where) {
7
7
  this.whereObject = {};
8
- this.whereObject = where || {};
8
+ this.whereObject = typeof where === 'string' ? JSON.parse(where) : where || {};
9
9
  }
10
10
  isOperator(value) {
11
11
  return `${value}`.startsWith('$');
@@ -1 +1 @@
1
- {"version":"5.7.3"}
1
+ {"version":"5.8.3"}