@ackplus/nest-crud-request 0.0.1 → 0.0.6

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ack Solutions
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,11 +1,135 @@
1
- # nest-crud-request
1
+ # @ackplus/nest-crud-request
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
3
+ A frontend utility package for building and formatting requests compatible with @ackplus/nest-crud backend. This package provides a type-safe way to construct CRUD requests with proper query parameters, filters, sorting, and pagination.
4
4
 
5
- ## Building
5
+ ## Features
6
6
 
7
- Run `nx build nest-crud-request` to build the library.
7
+ - Type-safe request builder
8
+ - Query parameter construction
9
+ - Filter builder
10
+ - Sort builder
11
+ - Search builder
12
+ - Pagination builder
13
+ - Request validation
14
+ - Automatic URL parameter encoding
8
15
 
9
- ## Running unit tests
16
+ ## Installation
10
17
 
11
- Run `nx test nest-crud-request` to execute the unit tests via [Jest](https://jestjs.io).
18
+ ```bash
19
+ npm install @ackplus/nest-crud-request
20
+ # or
21
+ yarn add @ackplus/nest-crud-request
22
+ ```
23
+
24
+ ## Quick Start
25
+
26
+ 1. Import the request builder:
27
+
28
+ ```typescript
29
+ import { CrudRequestBuilder } from '@ackplus/nest-crud-request';
30
+ ```
31
+
32
+ 2. Create and use the request builder:
33
+
34
+ ```typescript
35
+ // Create a new request builder
36
+ const request = new CrudRequestBuilder();
37
+
38
+ // Add pagination
39
+ request.paginate(1, 10);
40
+
41
+ // Add sorting
42
+ request.sort('name', 'asc');
43
+
44
+ // Add filters
45
+ request.filter('status', 'active');
46
+
47
+ // Add search
48
+ request.search('john');
49
+
50
+ // Build the request
51
+ const queryParams = request.build();
52
+ // Result: ?page=1&limit=10&sort=name&order=asc&filter[status]=active&search=john
53
+
54
+ // Use with your HTTP client
55
+ const response = await axios.get(`/api/users${queryParams}`);
56
+ ```
57
+
58
+ ## API Documentation
59
+
60
+ ### CrudRequestBuilder
61
+
62
+ The main class for building CRUD requests.
63
+
64
+ #### Methods
65
+
66
+ - `paginate(page: number, limit: number)` - Set pagination parameters
67
+ - `sort(field: string, order: 'asc' | 'desc')` - Add sort parameters
68
+ - `filter(field: string, value: any)` - Add filter conditions
69
+ - `search(term: string)` - Add search term
70
+ - `build()` - Build the request and return query string
71
+
72
+ ### Types
73
+
74
+ - `CrudRequest` - Type definition for the request structure
75
+ - `Filter` - Type for filter conditions
76
+ - `Sort` - Type for sort conditions
77
+ - `Pagination` - Type for pagination parameters
78
+
79
+ ## Examples
80
+
81
+ ### Basic Usage
82
+
83
+ ```typescript
84
+ const request = new CrudRequestBuilder()
85
+ .paginate(1, 10)
86
+ .sort('createdAt', 'desc')
87
+ .filter('status', 'active')
88
+ .search('john')
89
+ .build();
90
+
91
+ // Use with fetch
92
+ const response = await fetch(`/api/users${request}`);
93
+ ```
94
+
95
+ ### Advanced Filtering
96
+
97
+ ```typescript
98
+ const request = new CrudRequestBuilder()
99
+ .filter('age', { $gt: 18 })
100
+ .filter('status', { $in: ['active', 'pending'] })
101
+ .build();
102
+
103
+ // Result: ?filter[age][$gt]=18&filter[status][$in]=active,pending
104
+ ```
105
+
106
+ ### Multiple Sorts
107
+
108
+ ```typescript
109
+ const request = new CrudRequestBuilder()
110
+ .sort('name', 'asc')
111
+ .sort('age', 'desc')
112
+ .build();
113
+
114
+ // Result: ?sort=name,age&order=asc,desc
115
+ ```
116
+
117
+ ### Complex Search
118
+
119
+ ```typescript
120
+ const request = new CrudRequestBuilder()
121
+ .search('john doe')
122
+ .filter('department', 'IT')
123
+ .paginate(1, 20)
124
+ .build();
125
+
126
+ // Result: ?search=john%20doe&filter[department]=IT&page=1&limit=20
127
+ ```
128
+
129
+ ## Contributing
130
+
131
+ Contributions are welcome! Please feel free to submit a Pull Request.
132
+
133
+ ## License
134
+
135
+ This project is licensed under the terms of the MIT license.
@@ -0,0 +1,22 @@
1
+ const baseConfig = require('../../eslint.base.config.js');
2
+
3
+ module.exports = [
4
+ ...baseConfig,
5
+ {
6
+ files: ['**/*.json'],
7
+ rules: {
8
+ '@nx/dependency-checks': [
9
+ 'error',
10
+ {
11
+ ignoredFiles: [
12
+ '{projectRoot}/eslint.config.{js,cjs,mjs}',
13
+ '{projectRoot}/vite.config.{js,ts,mjs,mts}',
14
+ ],
15
+ },
16
+ ],
17
+ },
18
+ languageOptions: {
19
+ parser: require('jsonc-eslint-parser'),
20
+ },
21
+ },
22
+ ];
package/jest.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ export default {
2
+ displayName: 'nest-crud-request',
3
+ preset: '../../jest.preset.js',
4
+ testEnvironment: 'node',
5
+ transform: {
6
+ '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7
+ },
8
+ moduleFileExtensions: ['ts', 'js', 'html'],
9
+ coverageDirectory: '../../coverage/packages/nest-crud-request',
10
+ };
package/package.json CHANGED
@@ -1,10 +1,16 @@
1
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
- }
2
+ "name": "@ackplus/nest-crud-request",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
+ "main": "./index.js",
6
+ "types": "./index.d.ts",
7
+ "license": "MIT",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/ack-solutions/packages.git"
11
+ },
12
+ "homepage": "https://github.com/ack-solutions/packages.git#readme",
13
+ "dependencies": {
14
+ "tslib": "^2.3.0"
15
+ }
10
16
  }
package/project.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "name": "nest-crud-request",
3
+ "$schema": "../../node_modules/nx/schemas/project-schema.json",
4
+ "sourceRoot": "packages/nest-crud-request/src",
5
+ "projectType": "library",
6
+ "tags": [],
7
+ "// targets": "to see all targets run: nx show project nest-crud-request --web",
8
+ "targets": {}
9
+ }
package/src/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./lib/query-builder"), exports);
5
+ tslib_1.__exportStar(require("./lib/where-builder"), exports);
6
+ tslib_1.__exportStar(require("./lib/types"), exports);
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;AAAA,8DAAoC;AACpC,8DAAoC;AACpC,sDAA4B"}
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib/query-builder';
2
+ export * from './lib/where-builder';
3
+ export * from './lib/types';
@@ -20,7 +20,7 @@ export declare class QueryBuilder {
20
20
  setTake(take: number): this;
21
21
  toObject(): {
22
22
  select?: string[];
23
- relations?: string[] | import('./types').Relation[];
23
+ relations?: string[] | import("./types").Relation[];
24
24
  where?: Record<string, any>;
25
25
  order?: Record<string, OrderDirectionEnum>;
26
26
  skip?: number;
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryBuilder = void 0;
4
+ const relation_builder_1 = require("./relation-builder");
5
+ const utils_1 = require("./utils");
6
+ const where_builder_1 = require("./where-builder");
7
+ class QueryBuilder {
8
+ constructor(options) {
9
+ this.options = {};
10
+ this.whereBuilder = new where_builder_1.WhereBuilder();
11
+ this.relationBuilder = new relation_builder_1.RelationBuilder();
12
+ this.options = options;
13
+ this.setOptions(options);
14
+ }
15
+ setOptions(options) {
16
+ this.options = options;
17
+ this.whereBuilder = new where_builder_1.WhereBuilder(options.where);
18
+ this.relationBuilder = new relation_builder_1.RelationBuilder(options.relations);
19
+ return this;
20
+ }
21
+ mergeOptions(options, deep = false) {
22
+ let updatedOptions = {};
23
+ if (deep) {
24
+ updatedOptions = (0, utils_1.deepMerge)(this.options, options);
25
+ }
26
+ else {
27
+ updatedOptions = {
28
+ ...this.options,
29
+ ...options,
30
+ };
31
+ }
32
+ this.setOptions(updatedOptions);
33
+ return this;
34
+ }
35
+ addSelect(fields) {
36
+ if (!this.options.select) {
37
+ this.options.select = [];
38
+ }
39
+ if (Array.isArray(fields)) {
40
+ this.options.select.push(...fields);
41
+ }
42
+ else {
43
+ this.options.select.push(fields);
44
+ }
45
+ return this;
46
+ }
47
+ removeSelect(fields) {
48
+ if (this.options.select) {
49
+ if (Array.isArray(fields)) {
50
+ this.options.select = this.options.select.filter(field => !fields.includes(field));
51
+ }
52
+ else {
53
+ this.options.select = this.options.select.filter(field => field !== fields);
54
+ }
55
+ }
56
+ return this;
57
+ }
58
+ addRelation(relation, select, where) {
59
+ this.relationBuilder.add(relation, select, where);
60
+ return this;
61
+ }
62
+ removeRelation(relation) {
63
+ this.relationBuilder.remove(relation);
64
+ return this;
65
+ }
66
+ where(...args) {
67
+ this.whereBuilder.where(...args);
68
+ return this;
69
+ }
70
+ andWhere(...args) {
71
+ this.whereBuilder.andWhere(...args);
72
+ return this;
73
+ }
74
+ orWhere(...args) {
75
+ this.whereBuilder.orWhere(...args);
76
+ return this;
77
+ }
78
+ addOrder(orderBy, order) {
79
+ if (!this.options.order) {
80
+ this.options.order = {};
81
+ }
82
+ this.options.order[orderBy] = order;
83
+ return this;
84
+ }
85
+ removeOrder(orderBy) {
86
+ if (this.options.order) {
87
+ delete this.options.order[orderBy];
88
+ }
89
+ return this;
90
+ }
91
+ setSkip(skip) {
92
+ this.options.skip = skip;
93
+ return this;
94
+ }
95
+ setTake(take) {
96
+ this.options.take = take;
97
+ return this;
98
+ }
99
+ toObject() {
100
+ const options = {
101
+ ...this.options,
102
+ };
103
+ if (this.whereBuilder.hasConditions()) {
104
+ options.where = this.whereBuilder.toObject();
105
+ }
106
+ else {
107
+ delete options.where;
108
+ }
109
+ if (this.relationBuilder.hasRelations()) {
110
+ options.relations = this.relationBuilder.toObject();
111
+ }
112
+ else {
113
+ delete options.relations;
114
+ }
115
+ return options;
116
+ }
117
+ toJson() {
118
+ const obj = this.toObject();
119
+ return JSON.stringify(obj);
120
+ }
121
+ }
122
+ exports.QueryBuilder = QueryBuilder;
123
+ //# sourceMappingURL=query-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query-builder.js","sourceRoot":"","sources":["query-builder.ts"],"names":[],"mappings":";;;AAAA,yDAAqD;AAErD,mCAAoC;AACpC,mDAAsE;AAGtE,MAAa,YAAY;IAQrB,YAAY,OAA4B;QANhC,YAAO,GAAwB,EAAE,CAAC;QAElC,iBAAY,GAAiB,IAAI,4BAAY,EAAE,CAAC;QAEhD,oBAAe,GAAoB,IAAI,kCAAe,EAAE,CAAC;QAG7D,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;IAC7B,CAAC;IAED,UAAU,CAA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{ RelationBuilder } from './relation-builder';
2
+ import { QueryBuilderOptions, OrderDirectionEnum } from './types';
3
+ import { deepMerge } from './utils';
4
+ import { WhereBuilder, WhereBuilderCondition } from './where-builder';
5
+
6
+
7
+ export class QueryBuilder {
8
+
9
+ private options: QueryBuilderOptions = {};
10
+
11
+ private whereBuilder: WhereBuilder = new WhereBuilder();
12
+
13
+ private relationBuilder: RelationBuilder = new RelationBuilder();
14
+
15
+ constructor(options: QueryBuilderOptions) {
16
+ this.options = options;
17
+ this.setOptions(options);
18
+ }
19
+
20
+ setOptions(options: QueryBuilderOptions): this {
21
+ this.options = options;
22
+ this.whereBuilder = new WhereBuilder(options.where);
23
+ this.relationBuilder = new RelationBuilder(options.relations);
24
+ return this;
25
+ }
26
+
27
+ mergeOptions(options: QueryBuilderOptions, deep = false): this {
28
+ let updatedOptions = {};
29
+ if (deep) {
30
+ updatedOptions = deepMerge(this.options, options);
31
+ } else {
32
+ updatedOptions = {
33
+ ...this.options,
34
+ ...options,
35
+ };
36
+ }
37
+ this.setOptions(updatedOptions);
38
+ return this;
39
+ }
40
+
41
+ addSelect(fields: string | string[]): this {
42
+ if (!this.options.select) {
43
+ this.options.select = [];
44
+ }
45
+ if (Array.isArray(fields)) {
46
+ this.options.select.push(...fields);
47
+ } else {
48
+ this.options.select.push(fields);
49
+ }
50
+ return this;
51
+ }
52
+
53
+ removeSelect(fields: string | string[]): this {
54
+ if (this.options.select) {
55
+ if (Array.isArray(fields)) {
56
+ this.options.select = this.options.select.filter(field => !fields.includes(field));
57
+ } else {
58
+ this.options.select = this.options.select.filter(field => field !== fields);
59
+ }
60
+ }
61
+ return this;
62
+ }
63
+
64
+ addRelation(relation: string, select?: string[], where?: Record<string, any>): this {
65
+ this.relationBuilder.add(relation, select, where);
66
+ return this;
67
+ }
68
+
69
+ removeRelation(relation: string): this {
70
+ this.relationBuilder.remove(relation);
71
+ return this;
72
+ }
73
+
74
+ where(...args: WhereBuilderCondition): this {
75
+ this.whereBuilder.where(...args);
76
+ return this;
77
+ }
78
+
79
+ andWhere(...args: WhereBuilderCondition): this {
80
+ this.whereBuilder.andWhere(...args);
81
+ return this;
82
+ }
83
+
84
+ orWhere(...args: WhereBuilderCondition): this {
85
+ this.whereBuilder.orWhere(...args);
86
+ return this;
87
+ }
88
+
89
+ addOrder(orderBy: string, order: OrderDirectionEnum): this {
90
+ if (!this.options.order) {
91
+ this.options.order = {};
92
+ }
93
+ this.options.order[orderBy] = order;
94
+ return this;
95
+ }
96
+
97
+ removeOrder(orderBy: string): this {
98
+ if (this.options.order) {
99
+ delete this.options.order[orderBy];
100
+ }
101
+ return this;
102
+ }
103
+
104
+ setSkip(skip: number): this {
105
+ this.options.skip = skip;
106
+ return this;
107
+ }
108
+
109
+ setTake(take: number): this {
110
+ this.options.take = take;
111
+ return this;
112
+ }
113
+
114
+ toObject() {
115
+ const options = {
116
+ ...this.options,
117
+ };
118
+
119
+ if (this.whereBuilder.hasConditions()) {
120
+ options.where = this.whereBuilder.toObject();
121
+ } else {
122
+ delete options.where;
123
+ }
124
+
125
+ if (this.relationBuilder.hasRelations()) {
126
+ options.relations = this.relationBuilder.toObject();
127
+ } else {
128
+ delete options.relations;
129
+ }
130
+
131
+ return options;
132
+ }
133
+
134
+ toJson() {
135
+ const obj = this.toObject();
136
+ return JSON.stringify(obj);
137
+ }
138
+
139
+ }
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RelationBuilder = void 0;
4
+ class RelationBuilder {
5
+ constructor(relations) {
6
+ this.relations = {};
7
+ if (relations) {
8
+ this.setRelations(relations);
9
+ }
10
+ }
11
+ setRelations(relations) {
12
+ if (relations) {
13
+ if (Array.isArray(relations)) {
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
+ }
21
+ });
22
+ }
23
+ else if (typeof relations === 'string') {
24
+ this.add(relations);
25
+ }
26
+ else {
27
+ this.relations = relations || {};
28
+ }
29
+ }
30
+ return this;
31
+ }
32
+ clear() {
33
+ this.relations = {};
34
+ return this;
35
+ }
36
+ add(relation, select, where) {
37
+ this.relations[relation] = {
38
+ relation,
39
+ select,
40
+ where,
41
+ };
42
+ return this;
43
+ }
44
+ remove(relation) {
45
+ delete this.relations[relation];
46
+ return this;
47
+ }
48
+ hasRelations() {
49
+ return Object.keys(this.relations).length > 0;
50
+ }
51
+ toObject() {
52
+ return Object.values(this.relations);
53
+ }
54
+ toJson() {
55
+ return JSON.stringify(this.relations);
56
+ }
57
+ }
58
+ exports.RelationBuilder = RelationBuilder;
59
+ //# sourceMappingURL=relation-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relation-builder.js","sourceRoot":"","sources":["relation-builder.ts"],"names":[],"mappings":";;;AAGA,MAAa,eAAe;IAIxB,YAAY,SAA0C;QAF9C,cAAS,GAA6B,EAAE,CAAC;QAG7C,IAAI,SAAS,EAAE,CAAC;YACZ,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;QACjC,CAAC;IACL,CAAC;IAED,YAAY,CAAC,SAAyC;QAClD,IAAI,SAAS,EAAE,CAAC;YACZ,IAAI,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;gBAC3B,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE;oBACzB,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE,CAAC;wBAC/B,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;oBACvB,CAAC;yBAAM,CAAC;wBACJ,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,KAAK,CAAC,CAAC;oBACjE,CAAC;gBACL,CAAC,CAAC,CAAC;YACP,CAAC;iBAAM,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;gBACvC,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YACxB,CAAC;iBAAM,CAAC;gBACJ,IAAI,CAAC,SAAS,GAAG,SAAS,IAAI,EAAE,CAAC;YACrC,CAAC;QACL,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,KAAK;QACD,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,GAAG,CAAC,QAAgB,EAAE,MAAiB,EAAE,KAA2B;QAChE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG;YACvB,QAAQ;YACR,MAAM;YACN,KAAK;SACR,CAAC;QACF,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,QAAgB;QACnB,OAAO,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QAChC,OAAO,IAAI,CAAC;IAChB,CAAC;IAED,YAAY;QACR,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC;IAClD,CAAC;IAED,QAAQ;QACJ,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACzC,CAAC;IAED,MAAM;QACF,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC1C,CAAC;CAEJ;AA5DD,0CA4DC"}
@@ -0,0 +1,64 @@
1
+ import { Relation } from './types';
2
+
3
+
4
+ export class RelationBuilder {
5
+
6
+ private relations: Record<string, Relation> = {};
7
+
8
+ constructor(relations?: Relation[] | string[] | string) {
9
+ if (relations) {
10
+ this.setRelations(relations);
11
+ }
12
+ }
13
+
14
+ setRelations(relations: Relation[] | string[] | string): this {
15
+ if (relations) {
16
+ if (Array.isArray(relations)) {
17
+ relations.forEach(relation => {
18
+ if (typeof relation === 'string') {
19
+ this.add(relation);
20
+ } else {
21
+ this.add(relation.relation, relation.select, relation.where);
22
+ }
23
+ });
24
+ } else if (typeof relations === 'string') {
25
+ this.add(relations);
26
+ } else {
27
+ this.relations = relations || {};
28
+ }
29
+ }
30
+ return this;
31
+ }
32
+
33
+ clear(): this {
34
+ this.relations = {};
35
+ return this;
36
+ }
37
+
38
+ add(relation: string, select?: string[], where?: Record<string, any>): this {
39
+ this.relations[relation] = {
40
+ relation,
41
+ select,
42
+ where,
43
+ };
44
+ return this;
45
+ }
46
+
47
+ remove(relation: string): this {
48
+ delete this.relations[relation];
49
+ return this;
50
+ }
51
+
52
+ hasRelations(): boolean {
53
+ return Object.keys(this.relations).length > 0;
54
+ }
55
+
56
+ toObject(): Relation[] {
57
+ return Object.values(this.relations);
58
+ }
59
+
60
+ toJson(): string {
61
+ return JSON.stringify(this.relations);
62
+ }
63
+
64
+ }