@klerick/json-api-nestjs 10.0.0-beta.1 → 10.0.0-beta.4
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/CHANGELOG.md +137 -0
- package/README.md +447 -0
- package/package.json +29 -29
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
## 10.0.0-beta.4 (2025-05-23)
|
|
2
|
+
|
|
3
|
+
### 🚀 Features
|
|
4
|
+
|
|
5
|
+
- **json-api-nestjs:** Update package.json dep ([87519a0](https://github.com/klerick/nestjs-json-api/commit/87519a0))
|
|
6
|
+
|
|
7
|
+
### ❤️ Thank You
|
|
8
|
+
|
|
9
|
+
- Alex H
|
|
10
|
+
|
|
11
|
+
## 10.0.0-beta.3 (2025-05-23)
|
|
12
|
+
|
|
13
|
+
### 🚀 Features
|
|
14
|
+
|
|
15
|
+
- **json-api-nestjs,json-api-nestjs-microorm,json-api-nestjs-sdk,json-api-nestjs-shared,json-api-nestjs-typeorm:** up nestjs ([42b6b82](https://github.com/klerick/nestjs-json-api/commit/42b6b82))
|
|
16
|
+
|
|
17
|
+
### ❤️ Thank You
|
|
18
|
+
|
|
19
|
+
- Alex H
|
|
20
|
+
|
|
21
|
+
## 10.0.0-beta.0 (2025-05-21)
|
|
22
|
+
|
|
23
|
+
### 🚀 Features
|
|
24
|
+
|
|
25
|
+
- ⚠️ **json-api-nestjs:** remove module and refactoring ([f202ebc](https://github.com/klerick/nestjs-json-api/commit/f202ebc))
|
|
26
|
+
|
|
27
|
+
### 🩹 Fixes
|
|
28
|
+
|
|
29
|
+
- **json-api-nestjs:** Some fix for swagger ([9a0f190](https://github.com/klerick/nestjs-json-api/commit/9a0f190))
|
|
30
|
+
- **json-api-nestjs:** Use correct options for check is debug or not ([e52cc92](https://github.com/klerick/nestjs-json-api/commit/e52cc92))
|
|
31
|
+
- **json-api-nestjs:** fix type in some place ([61f45e6](https://github.com/klerick/nestjs-json-api/commit/61f45e6))
|
|
32
|
+
|
|
33
|
+
### ⚠️ Breaking Changes
|
|
34
|
+
|
|
35
|
+
- **json-api-nestjs:** Change type signature, rename npm package
|
|
36
|
+
|
|
37
|
+
### ❤️ Thank You
|
|
38
|
+
|
|
39
|
+
- Alex H
|
|
40
|
+
|
|
41
|
+
# 9.0.0 (2025-02-12)
|
|
42
|
+
|
|
43
|
+
### 🚀 Features
|
|
44
|
+
|
|
45
|
+
- **json-api-nestjs:** Microro orm ([4696f51](https://github.com/klerick/nestjs-json-api/commit/4696f51))
|
|
46
|
+
- **json-api-nestjs:** Microro orm ([18f4a0c](https://github.com/klerick/nestjs-json-api/commit/18f4a0c))
|
|
47
|
+
|
|
48
|
+
### 🩹 Fixes
|
|
49
|
+
|
|
50
|
+
- **json-api-nestjs:** Fix circular type for query obkect ([e492cd1](https://github.com/klerick/nestjs-json-api/commit/e492cd1))
|
|
51
|
+
|
|
52
|
+
### ❤️ Thank You
|
|
53
|
+
|
|
54
|
+
- Alex H
|
|
55
|
+
|
|
56
|
+
# 8.0.0 (2024-12-18)
|
|
57
|
+
|
|
58
|
+
### 🚀 Features
|
|
59
|
+
|
|
60
|
+
- **json-api-nestjs:** Return meta info in atomic operation ([1b61fb3](https://github.com/klerick/nestjs-json-api/commit/1b61fb3))
|
|
61
|
+
- **json-api-nestjs:** Allow set id from request for postOne ([6202c22](https://github.com/klerick/nestjs-json-api/commit/6202c22))
|
|
62
|
+
- **json-api-nestjs:** Extend validate for array, json and datetime, use nullable if possible ([a8c6f83](https://github.com/klerick/nestjs-json-api/commit/a8c6f83))
|
|
63
|
+
- **json-api-nestjs:** Allow call interceptor for each operation in atomic endpoint ([9e0f066](https://github.com/klerick/nestjs-json-api/commit/9e0f066))
|
|
64
|
+
- **json-api-nestjs:** Add wrapper transaction options ([8e498a8](https://github.com/klerick/nestjs-json-api/commit/8e498a8))
|
|
65
|
+
- **nestjs-json-rpc:** Add support soft delete ([c3b9322](https://github.com/klerick/nestjs-json-api/commit/c3b9322))
|
|
66
|
+
|
|
67
|
+
### ❤️ Thank You
|
|
68
|
+
|
|
69
|
+
- Alex H
|
|
70
|
+
|
|
71
|
+
## 7.0.4 (2024-10-27)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
### 🩹 Fixes
|
|
75
|
+
|
|
76
|
+
- **json-api-nestjs:** Add filter by null ([3af99ff](https://github.com/klerick/nestjs-json-api/commit/3af99ff))
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
### ❤️ Thank You
|
|
80
|
+
|
|
81
|
+
- Alex H
|
|
82
|
+
|
|
83
|
+
## 7.0.3 (2024-05-15)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
### 🩹 Fixes
|
|
87
|
+
|
|
88
|
+
- **json-api-nestjs:** Resource Relationship not allowing data key. ([f648422](https://github.com/klerick/nestjs-json-api/commit/f648422))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
### ❤️ Thank You
|
|
92
|
+
|
|
93
|
+
- Alex H
|
|
94
|
+
|
|
95
|
+
## 7.0.2 (2024-04-21)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
### 🩹 Fixes
|
|
99
|
+
|
|
100
|
+
- **json-api-nestjs:** Fix validation ([e5e9936](https://github.com/klerick/nestjs-json-api/commit/e5e9936))
|
|
101
|
+
|
|
102
|
+
- **json-api-nestjs:** Fix validate for patch method ([40b0303](https://github.com/klerick/nestjs-json-api/commit/40b0303))
|
|
103
|
+
|
|
104
|
+
- **json-api-nestjs:** Fix validate for patch method ([2caa2d8](https://github.com/klerick/nestjs-json-api/commit/2caa2d8))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
### ❤️ Thank You
|
|
108
|
+
|
|
109
|
+
- Alex H
|
|
110
|
+
|
|
111
|
+
## 7.0.1 (2024-04-06)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
### 🩹 Fixes
|
|
115
|
+
|
|
116
|
+
- **json-api-nestjs:** Fix validation ([1d048a8](https://github.com/klerick/nestjs-json-api/commit/1d048a8))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
### ❤️ Thank You
|
|
120
|
+
|
|
121
|
+
- Alex H
|
|
122
|
+
|
|
123
|
+
# 7.0.0 (2024-03-08)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
### 🚀 Features
|
|
127
|
+
|
|
128
|
+
- ⚠️ **json-api-nestjs:** new version json-api ([66076a3](https://github.com/klerick/nestjs-json-api/commit/66076a3))
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
#### ⚠️ Breaking Changes
|
|
132
|
+
|
|
133
|
+
- **json-api-nestjs:** - remove ajv and use zod for validation
|
|
134
|
+
|
|
135
|
+
### ❤️ Thank You
|
|
136
|
+
|
|
137
|
+
- Alex H
|
package/README.md
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
<p align='center'>
|
|
2
|
+
<a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/v/json-api-nestjs.svg" alt="NPM Version" /></a>
|
|
3
|
+
<a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/l/json-api-nestjs.svg" alt="Package License" /></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/dm/json-api-nestjs.svg" alt="NPM Downloads" /></a>
|
|
5
|
+
<a href="http://commitizen.github.io/cz-cli/" target="_blank"><img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg" alt="Commitizen friendly" /></a>
|
|
6
|
+
<img src="https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/klerick/02a4c98cf7008fea2af70dc2d50f4cb7/raw/json-api-nestjs.json" alt="Coverage Badge" />
|
|
7
|
+
</p>
|
|
8
|
+
|
|
9
|
+
# json-api-nestjs
|
|
10
|
+
|
|
11
|
+
This plugin works upon **TypeOrm** or **MicroOrm** library, which is used as the main database abstraction layer tool. The module
|
|
12
|
+
automatically generates an API according to JSON API specification from the database structure (**TypeOrm** or **MicroOrm** entities). It
|
|
13
|
+
supports features such as requests validation based on database fields types, request filtering, endpoints extending,
|
|
14
|
+
data relations control and much more. Our module significantly reduces the development time of REST services by removing
|
|
15
|
+
the need to negotiate the mechanism of client-server interaction and implementing automatic API generation without the
|
|
16
|
+
need to write any code.
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
$ npm install @klerick/json-api-nestjs
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Example
|
|
25
|
+
|
|
26
|
+
Once the installation process is complete, we can import the **JsonApiModule** into the root **AppModule**.
|
|
27
|
+
### TypeOrm
|
|
28
|
+
```typescript
|
|
29
|
+
import {Module} from '@nestjs/common';
|
|
30
|
+
import {JsonApiModule} from '@klerick/json-api-nestjs';
|
|
31
|
+
import {TypeOrmJsonApiModule} from '@klerick/json-api-nestjs-typeorm';
|
|
32
|
+
import {Users} from 'type-orm/database';
|
|
33
|
+
|
|
34
|
+
@Module({
|
|
35
|
+
imports: [
|
|
36
|
+
JsonApiModule.forRoot(TypeOrmJsonApiModule, {
|
|
37
|
+
entities: [Users]
|
|
38
|
+
}),
|
|
39
|
+
],
|
|
40
|
+
})
|
|
41
|
+
export class AppModule {
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### MicroOrm
|
|
46
|
+
```typescript
|
|
47
|
+
import {Module} from '@nestjs/common';
|
|
48
|
+
import {JsonApiModule} from '@klerick/json-api-nestjs';
|
|
49
|
+
import {MicroOrmJsonApiModule} from '@klerick/json-api-nestjs-microorm';
|
|
50
|
+
import {Users} from 'micro-orm/database';
|
|
51
|
+
|
|
52
|
+
@Module({
|
|
53
|
+
imports: [
|
|
54
|
+
JsonApiModule.forRoot(MicroOrmJsonApiModule, {
|
|
55
|
+
entities: [Users]
|
|
56
|
+
}),
|
|
57
|
+
],
|
|
58
|
+
})
|
|
59
|
+
export class AppModule {
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
After this, you have to prepare CRUDs with ready-to-use endpoints:
|
|
64
|
+
|
|
65
|
+
- GET /users
|
|
66
|
+
- POST /users
|
|
67
|
+
- GET /users/:id
|
|
68
|
+
- PATCH /users/:id
|
|
69
|
+
- DELETE /users/:id
|
|
70
|
+
- GET /users/{id}/relationships/{relName}
|
|
71
|
+
- POST /users/{id}/relationships/{relName}
|
|
72
|
+
- PATCH /users/{id}/relationships/{relName}
|
|
73
|
+
- DELETE /users/{id}/relationships/{relName}
|
|
74
|
+
|
|
75
|
+
## Configuration params
|
|
76
|
+
|
|
77
|
+
The following interface is using for the configuration:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
export interface ModuleOptions {
|
|
81
|
+
entities: Entity[]; // List of typeOrm Entity
|
|
82
|
+
controllers?: NestController[]; // List of controller, if you need extend default present
|
|
83
|
+
connectionName?: string; // Type orm connection name: "default" is default name
|
|
84
|
+
providers?: NestProvider[]; // List of addition provider for useing in custom controller
|
|
85
|
+
imports?: NestImport[]; // List of addition module for useing in custom controller
|
|
86
|
+
options?: {
|
|
87
|
+
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
|
|
88
|
+
debug?: boolean; // Debug info in result object, like error message
|
|
89
|
+
pipeForId?: Type<PipeTransform> // Nestjs pipe for validate id params, by default ParseIntPipe
|
|
90
|
+
operationUrl?: string // Url for atomic operation https://jsonapi.org/ext/atomic/
|
|
91
|
+
// You can add params for MicroOrm or TypeOrm adapter
|
|
92
|
+
} ;
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
You can extend the default controller:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import {Get, Param, Inject, BadRequestException} from '@nestjs/common';
|
|
100
|
+
|
|
101
|
+
import {Users} from 'database';
|
|
102
|
+
import {
|
|
103
|
+
JsonApi,
|
|
104
|
+
excludeMethod,
|
|
105
|
+
JsonBaseController,
|
|
106
|
+
InjectService,
|
|
107
|
+
JsonApiService,
|
|
108
|
+
Query,
|
|
109
|
+
} from '@klerick/json-api-nestjs';
|
|
110
|
+
import {
|
|
111
|
+
ResourceObjectRelationships,
|
|
112
|
+
} from '@klerick/json-api-nestjs-shared';
|
|
113
|
+
import {ExampleService} from '../../service/example/example.service';
|
|
114
|
+
|
|
115
|
+
@JsonApi(Users, {
|
|
116
|
+
allowMethod: excludeMethod(['deleteRelationship']),
|
|
117
|
+
requiredSelectField: true,
|
|
118
|
+
overrideRoute: 'user',
|
|
119
|
+
})
|
|
120
|
+
export class ExtendUserController extends JsonBaseController<Users, 'id'> {
|
|
121
|
+
@InjectService() public service: JsonApiService<Users>;
|
|
122
|
+
@Inject(ExampleService) protected exampleService: ExampleService;
|
|
123
|
+
|
|
124
|
+
public override getAll(query: Query<Users, 'id'>): Promise<ResourceObject<Users, 'array'>> {
|
|
125
|
+
if (!this.exampleService.someCheck(query)) {
|
|
126
|
+
throw new BadRequestException({});
|
|
127
|
+
}
|
|
128
|
+
return this.service.getAll(query);// OR call parent method: super.getAll(query);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
public override patchRelationship<Rel extends EntityRelation<Users>>(
|
|
132
|
+
id: string | number,
|
|
133
|
+
relName: Rel,
|
|
134
|
+
input: PatchRelationshipData
|
|
135
|
+
): Promise<ResourceObjectRelationships<Users, 'id', Rel>> {
|
|
136
|
+
return super.patchRelationship(id, relName, input);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
@Get(':id/example')
|
|
140
|
+
testOne(@Param('id') id: string): string {
|
|
141
|
+
return this.exampleService.testMethode(id);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
You can overwrite the default config for the current controller using options in the decorator **JsonAPi**.
|
|
147
|
+
Also you can specify an API method necessary for you, using **allowMethod**
|
|
148
|
+
Defulat validation check only simple type and database structure.
|
|
149
|
+
If you need custom pipe validation you can your owner pipe like this:
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
|
|
153
|
+
import { Query } from '@nestjs/common';
|
|
154
|
+
import {
|
|
155
|
+
JsonApi,
|
|
156
|
+
excludeMethod,
|
|
157
|
+
JsonBaseController,
|
|
158
|
+
InjectService,
|
|
159
|
+
JsonApiService,
|
|
160
|
+
Query as QueryType,
|
|
161
|
+
} from 'json-api-nestjs';
|
|
162
|
+
|
|
163
|
+
@JsonApi(Users, {
|
|
164
|
+
allowMethod: excludeMethod(['deleteRelationship']),
|
|
165
|
+
requiredSelectField: true,
|
|
166
|
+
overrideRoute: 'user',
|
|
167
|
+
})
|
|
168
|
+
export class ExtendUserController extends JsonBaseController<Users, 'id'> {
|
|
169
|
+
@InjectService() public service: JsonApiService<Users>;
|
|
170
|
+
@Inject(ExampleService) protected exampleService: ExampleService;
|
|
171
|
+
|
|
172
|
+
public override getAll(
|
|
173
|
+
@Query(ExamplePipe) query: QueryType<Users, 'id'>
|
|
174
|
+
): Promise<ResourceObject<Users, 'array'>> {
|
|
175
|
+
return super.getAll(query);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
import { ArgumentMetadata, PipeTransform } from '@nestjs/common';
|
|
182
|
+
|
|
183
|
+
import { Query } from 'json-api-nestjs';
|
|
184
|
+
import { Users } from 'database';
|
|
185
|
+
|
|
186
|
+
export class ExamplePipe implements PipeTransform<Query<Users, 'id'>, Query<Users, 'id'>> {
|
|
187
|
+
transform(value: Query<Users, 'id'>, metadata: ArgumentMetadata): Query<Users, 'id'> {
|
|
188
|
+
return value;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Swagger UI
|
|
194
|
+
|
|
195
|
+
For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction) and configure it
|
|
196
|
+
```typescript
|
|
197
|
+
const app = await NestFactory.create(AppModule);
|
|
198
|
+
|
|
199
|
+
const config = new DocumentBuilder()
|
|
200
|
+
.setOpenAPIVersion('3.1.0')
|
|
201
|
+
.setTitle('JSON API swagger example')
|
|
202
|
+
.setDescription('The JSON API list example')
|
|
203
|
+
.setVersion('1.0')
|
|
204
|
+
.build();
|
|
205
|
+
|
|
206
|
+
SwaggerModule.setup(
|
|
207
|
+
'swagger',
|
|
208
|
+
app,
|
|
209
|
+
() => SwaggerModule.createDocument(app, config), // !!!Important: document as factory
|
|
210
|
+
{}
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Available endpoint method
|
|
216
|
+
|
|
217
|
+
Using **Users** entity and relation **Roles** entity as example
|
|
218
|
+
|
|
219
|
+
### List item of Users
|
|
220
|
+
|
|
221
|
+
```
|
|
222
|
+
GET /users
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Available query params:
|
|
226
|
+
|
|
227
|
+
- **include** - you can extend result with relations (aka join)
|
|
228
|
+
```
|
|
229
|
+
GET /users?include=roles
|
|
230
|
+
```
|
|
231
|
+
result of request will have role relation for each **Users** item
|
|
232
|
+
|
|
233
|
+
- **fields** - you can specify required fields of result query
|
|
234
|
+
|
|
235
|
+
```
|
|
236
|
+
GET /users?fields[target]=login,lastName&fileds[roles]=name,key
|
|
237
|
+
```
|
|
238
|
+
The "target" is **Users** entity
|
|
239
|
+
The "roles" is **Roles** entity
|
|
240
|
+
So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *
|
|
241
|
+
key* for **Roles** entity
|
|
242
|
+
- **sort** - you can sort result of the request
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
GET /users?sort=target.name,-roles.key
|
|
246
|
+
```
|
|
247
|
+
The "target" is **Users** entity
|
|
248
|
+
The "roles" is **Roles** entity
|
|
249
|
+
So, result of the request will be sorted by field *name* of **Users** by *ASC* and field *key* of **Roles** entity
|
|
250
|
+
by **DESC**.
|
|
251
|
+
- **page** - pagination for you request
|
|
252
|
+
|
|
253
|
+
```
|
|
254
|
+
GET /users?page[number]=1page[size]=20
|
|
255
|
+
```
|
|
256
|
+
- **filter** - filter for query
|
|
257
|
+
|
|
258
|
+
```
|
|
259
|
+
GET /users?filter[name][eq]=1&filter[roles.name][ne]=test&filter[roles.status][eq]=true
|
|
260
|
+
```
|
|
261
|
+
The "name" is a field of **Users** entity
|
|
262
|
+
The "roles.name" is *name* field of **Roles** entity
|
|
263
|
+
The "eq", "ne" is *[Filter operand](#filter-operand)*
|
|
264
|
+
|
|
265
|
+
So, this query will be transformed like sql:
|
|
266
|
+
```sql
|
|
267
|
+
WHERE users.name = 1 AND roles.name <> 'test' AND roles.status = true
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Filter operand
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
type FilterOperand
|
|
274
|
+
{
|
|
275
|
+
in:string[] // is equal to the conditional of query "WHERE 'attribute_name' IN ('value1', 'value2')"
|
|
276
|
+
nin: string[] // is equal to the conditional of query "WHERE 'attribute_name' NOT IN ('value1', 'value1')"
|
|
277
|
+
eq: string // is equal to the conditional of query "WHERE 'attribute_name' = 'value1'
|
|
278
|
+
ne: string // is equal to the conditional of query "WHERE 'attribute_name' <> 'value1'
|
|
279
|
+
gte: string // is equal to the conditional of query "WHERE 'attribute_name' >= 'value1'
|
|
280
|
+
gt: string // is equal to the conditional of query "WHERE 'attribute_name' > 'value1'
|
|
281
|
+
lt: string // is equal to the conditional of query "WHERE 'attribute_name' < 'value1'
|
|
282
|
+
lte:string // is equal to the conditional of query "WHERE 'attribute_name' <= 'value1'
|
|
283
|
+
regexp: string // is equal to the conditional of query "WHERE 'attribute_name' ~* value1
|
|
284
|
+
some: string // is equal to the conditional of query "WHERE 'attribute_name' && [value1]
|
|
285
|
+
}
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Get item of Users
|
|
289
|
+
|
|
290
|
+
```
|
|
291
|
+
GET /users/:id
|
|
292
|
+
```
|
|
293
|
+
- **include** - you can extend result with relations (aka join)
|
|
294
|
+
```
|
|
295
|
+
GET /users?include=roles
|
|
296
|
+
```
|
|
297
|
+
result of request will have role relation for each **Users** item
|
|
298
|
+
|
|
299
|
+
- **fields** - you can specify required fields of result query
|
|
300
|
+
|
|
301
|
+
```
|
|
302
|
+
GET /users?fields[target]=login,lastName&fileds[roles]=name,key
|
|
303
|
+
```
|
|
304
|
+
The "target" is **Users** entity
|
|
305
|
+
The "roles" is **Roles** entity
|
|
306
|
+
So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *
|
|
307
|
+
key* for **Roles** entity
|
|
308
|
+
|
|
309
|
+
### Create item of Users
|
|
310
|
+
```
|
|
311
|
+
POST /users
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
- **body** - Create new User and add link to address
|
|
315
|
+
|
|
316
|
+
```json
|
|
317
|
+
{
|
|
318
|
+
"data": {
|
|
319
|
+
"type": "users",
|
|
320
|
+
"attributes": {
|
|
321
|
+
"id": 0,
|
|
322
|
+
"login": "string",
|
|
323
|
+
"firstName": "string",
|
|
324
|
+
"lastName": "string",
|
|
325
|
+
"isActive": true,
|
|
326
|
+
"createdAt": "2023-12-08T10:32:27.352Z",
|
|
327
|
+
"updatedAt": "2023-12-08T10:32:27.352Z"
|
|
328
|
+
},
|
|
329
|
+
"relationships": {
|
|
330
|
+
"addresses": {
|
|
331
|
+
"id": "1",
|
|
332
|
+
"type": "addresses"
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
```
|
|
338
|
+
### Update item of Users
|
|
339
|
+
```
|
|
340
|
+
PATCH /users/:id
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
- **body** - Update User with id 1 and update link to address and manager
|
|
344
|
+
|
|
345
|
+
```json
|
|
346
|
+
{
|
|
347
|
+
"data": {
|
|
348
|
+
"id": "1",
|
|
349
|
+
"type": "users",
|
|
350
|
+
"attributes": {
|
|
351
|
+
"id": 0,
|
|
352
|
+
"login": "string",
|
|
353
|
+
"firstName": "string",
|
|
354
|
+
"lastName": "string",
|
|
355
|
+
"isActive": true,
|
|
356
|
+
"createdAt": "2023-12-08T10:34:57.752Z",
|
|
357
|
+
"updatedAt": "2023-12-08T10:34:57.752Z"
|
|
358
|
+
},
|
|
359
|
+
"relationships": {
|
|
360
|
+
"addresses": {
|
|
361
|
+
"id": "2",
|
|
362
|
+
"type": "addresses"
|
|
363
|
+
},
|
|
364
|
+
"manager": {
|
|
365
|
+
"id": "2",
|
|
366
|
+
"type": "users"
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
### Atomic operation
|
|
375
|
+
You can more information find [here](https://jsonapi.org/ext/atomic/)
|
|
376
|
+
But you have additinal feature.
|
|
377
|
+
For example: you will need create new **Roles**, then - create new **Users** and assign new **Roles** to new **Users**.
|
|
378
|
+
If use native json api you need send 3 http request:
|
|
379
|
+
- POST /roles
|
|
380
|
+
- POST /users
|
|
381
|
+
|
|
382
|
+
but [Atomic operation](https://jsonapi.org/ext/atomic/) allow for one request.
|
|
383
|
+
```json
|
|
384
|
+
|
|
385
|
+
{
|
|
386
|
+
"atomic:operations":[
|
|
387
|
+
{
|
|
388
|
+
"op":"add",
|
|
389
|
+
"ref":{
|
|
390
|
+
"type":"roles",
|
|
391
|
+
"tmpId":10000
|
|
392
|
+
},
|
|
393
|
+
"data":{
|
|
394
|
+
"type":"roles",
|
|
395
|
+
"attributes":{
|
|
396
|
+
"name":"testRolesAgain",
|
|
397
|
+
"key":"testRolesAgain"
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
"op":"add",
|
|
403
|
+
"ref":{
|
|
404
|
+
"type":"users"
|
|
405
|
+
},
|
|
406
|
+
"data":{
|
|
407
|
+
"type":"users",
|
|
408
|
+
"attributes":{
|
|
409
|
+
"login":"newLogin"
|
|
410
|
+
},
|
|
411
|
+
"relationships":{
|
|
412
|
+
"addresses":{
|
|
413
|
+
"type":"addresses",
|
|
414
|
+
"id":"1"
|
|
415
|
+
},
|
|
416
|
+
"roles":[
|
|
417
|
+
{
|
|
418
|
+
"type":"roles",
|
|
419
|
+
"id":"10000"
|
|
420
|
+
}
|
|
421
|
+
]
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
]
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
```
|
|
429
|
+
**tmpId** - is params for operation **add**, should be unique for all operations.
|
|
430
|
+
|
|
431
|
+
If you have Interceptor you can check call it from **AtomicOperation**
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
```ts
|
|
436
|
+
@Injectable()
|
|
437
|
+
export class AtomicInterceptor<T> implements NestInterceptor {
|
|
438
|
+
intercept(context: ExecutionContext, next: CallHandler<T>): Observable<T> {
|
|
439
|
+
const isAtomic = context.getArgByIndex(3)
|
|
440
|
+
if (isAtomic) {
|
|
441
|
+
console.log('call from atomic operation')
|
|
442
|
+
}
|
|
443
|
+
return next.handle();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
```
|
|
447
|
+
**isAtomic** - is array of params of method
|
package/package.json
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@klerick/json-api-nestjs",
|
|
3
|
-
"version": "10.0.0-beta.
|
|
4
|
-
"license": "MIT",
|
|
5
|
-
"private": false,
|
|
6
|
-
"contributors": [
|
|
7
|
-
{
|
|
8
|
-
"email": "klerick666@gmain.com",
|
|
9
|
-
"name": "Aleksandr Kharkovey"
|
|
10
|
-
}
|
|
11
|
-
],
|
|
12
|
-
"repository": {
|
|
13
|
-
"type": "git",
|
|
14
|
-
"url": "https://github.com/klerick/nestjs-json-api.git"
|
|
15
|
-
},
|
|
16
|
-
"engines": {
|
|
17
|
-
"node": ">= 20.0.0"
|
|
18
|
-
},
|
|
3
|
+
"version": "10.0.0-beta.4",
|
|
19
4
|
"type": "commonjs",
|
|
20
5
|
"description": "JsonApi Plugin for NestJs",
|
|
21
6
|
"keywords": [
|
|
@@ -29,9 +14,10 @@
|
|
|
29
14
|
],
|
|
30
15
|
"dependencies": {
|
|
31
16
|
"@anatine/zod-openapi": "^2.2.3",
|
|
32
|
-
"@nestjs
|
|
33
|
-
"@nestjs/
|
|
34
|
-
"@nestjs/
|
|
17
|
+
"@klerick/json-api-nestjs-shared": "1.0.0-beta.3",
|
|
18
|
+
"@nestjs/common": "^11.1.0",
|
|
19
|
+
"@nestjs/core": "^11.1.0",
|
|
20
|
+
"@nestjs/swagger": "^11.2.0",
|
|
35
21
|
"change-case-commonjs": "^5.4.4",
|
|
36
22
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
37
23
|
"rxjs": "^7.1.0",
|
|
@@ -40,19 +26,33 @@
|
|
|
40
26
|
"zod": "^3.24.1",
|
|
41
27
|
"zod-validation-error": "^3.4.0"
|
|
42
28
|
},
|
|
43
|
-
"publishConfig": {
|
|
44
|
-
"access": "public"
|
|
45
|
-
},
|
|
46
|
-
"peerDependencies": {
|
|
47
|
-
"@klerick/json-api-nestjs-shared": "1.0.0-beta.1"
|
|
48
|
-
},
|
|
49
29
|
"exports": {
|
|
50
30
|
"./package.json": "./package.json",
|
|
51
31
|
".": {
|
|
52
|
-
"types": "
|
|
53
|
-
"default": "
|
|
32
|
+
"types": "./src/index.d.ts",
|
|
33
|
+
"default": "./src/index.js"
|
|
54
34
|
}
|
|
55
35
|
},
|
|
56
|
-
"types": "
|
|
57
|
-
"main": "
|
|
36
|
+
"types": "./src/index.d.ts",
|
|
37
|
+
"main": "./src/index.js",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"contributors": [
|
|
40
|
+
{
|
|
41
|
+
"email": "klerick666@gmain.com",
|
|
42
|
+
"name": "Aleksandr Kharkovey"
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
"repository": {
|
|
46
|
+
"type": "git",
|
|
47
|
+
"url": "https://github.com/klerick/nestjs-json-api.git"
|
|
48
|
+
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">= 20.0.0"
|
|
51
|
+
},
|
|
52
|
+
"files": [
|
|
53
|
+
"**/*"
|
|
54
|
+
],
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
}
|
|
58
58
|
}
|