@klerick/json-api-nestjs 10.0.0-beta.1 → 10.0.0-beta.10
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 +207 -0
- package/README.md +587 -0
- package/package.json +38 -34
- package/src/index.d.ts +6 -6
- package/src/index.js +6 -1
- package/src/index.js.map +1 -1
- package/src/lib/constants/constants.d.ts +4 -0
- package/src/lib/constants/constants.js +12 -1
- package/src/lib/constants/constants.js.map +1 -1
- package/src/lib/constants/reflection.d.ts +2 -0
- package/src/lib/constants/reflection.js +3 -1
- package/src/lib/constants/reflection.js.map +1 -1
- package/src/lib/modules/atomic-operation/atomic-operation.module.js +6 -0
- package/src/lib/modules/atomic-operation/atomic-operation.module.js.map +1 -1
- package/src/lib/modules/atomic-operation/pipes/input-operation.pipe.js +1 -4
- package/src/lib/modules/atomic-operation/pipes/input-operation.pipe.js.map +1 -1
- package/src/lib/modules/atomic-operation/service/execute.service.d.ts +6 -0
- package/src/lib/modules/atomic-operation/service/execute.service.js +46 -2
- package/src/lib/modules/atomic-operation/service/execute.service.js.map +1 -1
- package/src/lib/modules/atomic-operation/service/explorer.service.js +1 -1
- package/src/lib/modules/atomic-operation/service/explorer.service.js.map +1 -1
- package/src/lib/modules/atomic-operation/service/swagger.service.js +2 -2
- package/src/lib/modules/atomic-operation/service/swagger.service.js.map +1 -1
- package/src/lib/modules/atomic-operation/utils/zod/zod-helper.d.ts +35 -680
- package/src/lib/modules/atomic-operation/utils/zod/zod-helper.js +6 -7
- package/src/lib/modules/atomic-operation/utils/zod/zod-helper.js.map +1 -1
- package/src/lib/modules/mixin/config/bindings.js +9 -9
- package/src/lib/modules/mixin/config/bindings.js.map +1 -1
- package/src/lib/modules/mixin/decorators/index.d.ts +1 -0
- package/src/lib/modules/mixin/decorators/index.js +1 -0
- package/src/lib/modules/mixin/decorators/index.js.map +1 -1
- package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.d.ts +48 -0
- package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.js +83 -0
- package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.js.map +1 -0
- package/src/lib/modules/mixin/factory/zod-validate.factory.js +11 -6
- package/src/lib/modules/mixin/factory/zod-validate.factory.js.map +1 -1
- package/src/lib/modules/mixin/helpers/bind-controller.js +10 -3
- package/src/lib/modules/mixin/helpers/bind-controller.js.map +1 -1
- package/src/lib/modules/mixin/helpers/create-controller.js +3 -0
- package/src/lib/modules/mixin/helpers/create-controller.js.map +1 -1
- package/src/lib/modules/mixin/helpers/utils.d.ts +2 -0
- package/src/lib/modules/mixin/helpers/utils.js +10 -0
- package/src/lib/modules/mixin/helpers/utils.js.map +1 -1
- package/src/lib/modules/mixin/mixin.module.js +1 -0
- package/src/lib/modules/mixin/mixin.module.js.map +1 -1
- package/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.js +1 -4
- package/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.js.map +1 -1
- package/src/lib/modules/mixin/pipe/patch-relationship/patch-relationship.pipe.js +1 -4
- package/src/lib/modules/mixin/pipe/patch-relationship/patch-relationship.pipe.js.map +1 -1
- package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.d.ts +1 -0
- package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.js +10 -4
- package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.js.map +1 -1
- package/src/lib/modules/mixin/pipe/post-relationship/post-relationship.pipe.js +1 -4
- package/src/lib/modules/mixin/pipe/post-relationship/post-relationship.pipe.js.map +1 -1
- package/src/lib/modules/mixin/pipe/query-filed-on-include/query-filed-in-include.pipe.js +2 -4
- package/src/lib/modules/mixin/pipe/query-filed-on-include/query-filed-in-include.pipe.js.map +1 -1
- package/src/lib/modules/mixin/pipe/query-input/query-input.pipe.js +1 -4
- package/src/lib/modules/mixin/pipe/query-input/query-input.pipe.js.map +1 -1
- package/src/lib/modules/mixin/service/json-api-transformer.service.js +27 -4
- package/src/lib/modules/mixin/service/json-api-transformer.service.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/delete-relationship.js +3 -3
- package/src/lib/modules/mixin/swagger/method/delete-relationship.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/index.d.ts +0 -4
- package/src/lib/modules/mixin/swagger/method/index.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/patch-one.js +6 -3
- package/src/lib/modules/mixin/swagger/method/patch-one.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/patch-relationship.js +3 -3
- package/src/lib/modules/mixin/swagger/method/patch-relationship.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/post-one.js +6 -3
- package/src/lib/modules/mixin/swagger/method/post-one.js.map +1 -1
- package/src/lib/modules/mixin/swagger/method/post-relationship.js +3 -3
- package/src/lib/modules/mixin/swagger/method/post-relationship.js.map +1 -1
- package/src/lib/modules/mixin/swagger/utils.d.ts +2 -0
- package/src/lib/modules/mixin/swagger/utils.js +16 -1
- package/src/lib/modules/mixin/swagger/utils.js.map +1 -1
- package/src/lib/modules/mixin/types/decorator-options.type.d.ts +1 -0
- package/src/lib/modules/mixin/types/orm-service.type.d.ts +8 -0
- package/src/lib/modules/mixin/zod/index.d.ts +1 -0
- package/src/lib/modules/mixin/zod/index.js +6 -0
- package/src/lib/modules/mixin/zod/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/map-transform-to-json-shema.d.ts +6 -0
- package/src/lib/modules/mixin/zod/map-transform-to-json-shema.js +27 -0
- package/src/lib/modules/mixin/zod/map-transform-to-json-shema.js.map +1 -0
- package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.d.ts +4 -44
- package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.js +2 -3
- package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.d.ts +16 -5
- package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.js +7 -9
- package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.d.ts +4 -50
- package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.js +2 -3
- package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-post-schema/index.d.ts +16 -6
- package/src/lib/modules/mixin/zod/zod-input-post-schema/index.js +7 -13
- package/src/lib/modules/mixin/zod/zod-input-post-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.d.ts +13 -27
- package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.js +20 -13
- package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.d.ts +72 -987
- package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.js +11 -7
- package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-query-schema/include.d.ts +1 -1
- package/src/lib/modules/mixin/zod/zod-input-query-schema/index.d.ts +73 -4933
- package/src/lib/modules/mixin/zod/zod-input-query-schema/index.js +6 -3
- package/src/lib/modules/mixin/zod/zod-input-query-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-input-query-schema/sort.d.ts +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/fields.d.ts +3 -3
- package/src/lib/modules/mixin/zod/zod-query-schema/fields.js +6 -2
- package/src/lib/modules/mixin/zod/zod-query-schema/fields.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/filter.d.ts +24 -104
- package/src/lib/modules/mixin/zod/zod-query-schema/filter.js +32 -10
- package/src/lib/modules/mixin/zod/zod-query-schema/filter.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/include.d.ts +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/include.js +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/include.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/index.d.ts +4 -4
- package/src/lib/modules/mixin/zod/zod-query-schema/index.js +3 -4
- package/src/lib/modules/mixin/zod/zod-query-schema/index.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/sort.d.ts +11 -3
- package/src/lib/modules/mixin/zod/zod-query-schema/sort.js +1 -1
- package/src/lib/modules/mixin/zod/zod-query-schema/sort.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-share/attributes.d.ts +15 -14
- package/src/lib/modules/mixin/zod/zod-share/attributes.js +26 -20
- package/src/lib/modules/mixin/zod/zod-share/attributes.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-share/page.d.ts +3 -9
- package/src/lib/modules/mixin/zod/zod-share/rel-data.d.ts +4 -40
- package/src/lib/modules/mixin/zod/zod-share/rel-data.js +2 -2
- package/src/lib/modules/mixin/zod/zod-share/rel-data.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-share/relationships.d.ts +8 -304
- package/src/lib/modules/mixin/zod/zod-share/relationships.js +1 -1
- package/src/lib/modules/mixin/zod/zod-share/relationships.js.map +1 -1
- package/src/lib/modules/mixin/zod/zod-utils.d.ts +2 -1
- package/src/lib/modules/mixin/zod/zod-utils.js +12 -0
- package/src/lib/modules/mixin/zod/zod-utils.js.map +1 -1
- package/src/lib/types/entity-param.type.d.ts +6 -4
- package/src/lib/types/entity-param.type.js +13 -0
- package/src/lib/types/entity-param.type.js.map +1 -1
- package/src/lib/types/error.types.d.ts +3 -3
- package/src/lib/types/index.d.ts +1 -0
- package/src/lib/types/index.js +1 -0
- package/src/lib/types/index.js.map +1 -1
- package/src/lib/types/json-api-read-only.d.ts +51 -0
- package/src/lib/types/json-api-read-only.js +3 -0
- package/src/lib/types/json-api-read-only.js.map +1 -0
- package/src/lib/types/module-options.types.d.ts +5 -1
- package/src/lib/utils/module-helper.js +4 -0
- package/src/lib/utils/module-helper.js.map +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
<p align='center'>
|
|
2
|
+
<a href="https://www.npmjs.com/package/@klerick/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/v/@klerick/json-api-nestjs.svg" alt="NPM Version" /></a>
|
|
3
|
+
<a href="https://www.npmjs.com/package/@klerick/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/l/@klerick/json-api-nestjs.svg" alt="Package License" /></a>
|
|
4
|
+
<a href="https://www.npmjs.com/package/@klerick/json-api-nestjs" target="_blank"><img src="https://img.shields.io/npm/dm/@klerick/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
|
|
448
|
+
|
|
449
|
+
## Resource Linkage for To-One Relations
|
|
450
|
+
|
|
451
|
+
According to the [JSON:API specification](https://jsonapi.org/format/#document-resource-object-linkage), relationships should include a `data` member containing resource linkage (the `id` and `type` of the related resource).
|
|
452
|
+
|
|
453
|
+
For **to-one relations** (ManyToOne, OneToOne), if your entity has an FK field (foreign key), the library will automatically include `relationships.{relation}.data` in responses **even without using `include`**.
|
|
454
|
+
|
|
455
|
+
**Example response:**
|
|
456
|
+
```json
|
|
457
|
+
{
|
|
458
|
+
"data": {
|
|
459
|
+
"id": "1",
|
|
460
|
+
"type": "comments",
|
|
461
|
+
"attributes": { "text": "Hello" },
|
|
462
|
+
"relationships": {
|
|
463
|
+
"user": {
|
|
464
|
+
"links": { "self": "/api/comments/1/relationships/user" },
|
|
465
|
+
"data": { "id": "5", "type": "users" }
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
```
|
|
471
|
+
|
|
472
|
+
If the FK field is `null`, then `data` will also be `null`:
|
|
473
|
+
```json
|
|
474
|
+
"relationships": {
|
|
475
|
+
"user": {
|
|
476
|
+
"links": { "self": "/api/comments/1/relationships/user" },
|
|
477
|
+
"data": null
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
```
|
|
481
|
+
|
|
482
|
+
**Note:** TypeORM and MikroORM implement FK field detection differently. See the respective adapter documentation for details on how to define FK fields in your entities.
|
|
483
|
+
|
|
484
|
+
## Field Access Control Decorators
|
|
485
|
+
|
|
486
|
+
The library provides two decorators to control field accessibility in POST and PATCH operations:
|
|
487
|
+
|
|
488
|
+
### @JsonApiReadOnly()
|
|
489
|
+
|
|
490
|
+
Fields marked with `@JsonApiReadOnly()` are completely excluded from input validation schemas. They cannot be set via POST or PATCH requests and are not shown in Swagger documentation for input.
|
|
491
|
+
|
|
492
|
+
**Use case:** Server-managed fields like `createdAt`, `updatedAt`, computed fields.
|
|
493
|
+
|
|
494
|
+
```typescript
|
|
495
|
+
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
|
496
|
+
import { JsonApiReadOnly } from '@klerick/json-api-nestjs';
|
|
497
|
+
|
|
498
|
+
@Entity()
|
|
499
|
+
export class Users {
|
|
500
|
+
@PrimaryGeneratedColumn()
|
|
501
|
+
id: number;
|
|
502
|
+
|
|
503
|
+
@Column()
|
|
504
|
+
login: string;
|
|
505
|
+
|
|
506
|
+
@JsonApiReadOnly()
|
|
507
|
+
@CreateDateColumn()
|
|
508
|
+
createdAt: Date;
|
|
509
|
+
|
|
510
|
+
@JsonApiReadOnly()
|
|
511
|
+
@UpdateDateColumn()
|
|
512
|
+
updatedAt: Date;
|
|
513
|
+
}
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
With this configuration:
|
|
517
|
+
- **POST /users** - `createdAt` and `updatedAt` fields are not accepted
|
|
518
|
+
- **PATCH /users/:id** - `createdAt` and `updatedAt` fields are not accepted
|
|
519
|
+
|
|
520
|
+
### @JsonApiImmutable()
|
|
521
|
+
|
|
522
|
+
Fields marked with `@JsonApiImmutable()` can be optionally set during creation (POST) but cannot be modified after (PATCH).
|
|
523
|
+
|
|
524
|
+
**Use case:** Fields that should be set once and never changed, like `externalId`, `slug`, or `username`.
|
|
525
|
+
|
|
526
|
+
```typescript
|
|
527
|
+
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
|
|
528
|
+
import { JsonApiImmutable } from '@klerick/json-api-nestjs';
|
|
529
|
+
|
|
530
|
+
@Entity()
|
|
531
|
+
export class Users {
|
|
532
|
+
@PrimaryGeneratedColumn()
|
|
533
|
+
id: number;
|
|
534
|
+
|
|
535
|
+
@JsonApiImmutable()
|
|
536
|
+
@Column({ unique: true })
|
|
537
|
+
login: string;
|
|
538
|
+
|
|
539
|
+
@Column()
|
|
540
|
+
firstName: string;
|
|
541
|
+
|
|
542
|
+
@Column()
|
|
543
|
+
lastName: string;
|
|
544
|
+
}
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
With this configuration:
|
|
548
|
+
- **POST /users** - `login` is optional, can be provided or omitted
|
|
549
|
+
- **PATCH /users/:id** - `login` is not accepted (cannot be changed)
|
|
550
|
+
|
|
551
|
+
### Combining Decorators
|
|
552
|
+
|
|
553
|
+
You can use both decorators in the same entity:
|
|
554
|
+
|
|
555
|
+
```typescript
|
|
556
|
+
@Entity()
|
|
557
|
+
export class Article {
|
|
558
|
+
@PrimaryGeneratedColumn()
|
|
559
|
+
id: number;
|
|
560
|
+
|
|
561
|
+
@JsonApiImmutable()
|
|
562
|
+
@Column({ unique: true })
|
|
563
|
+
slug: string;
|
|
564
|
+
|
|
565
|
+
@Column()
|
|
566
|
+
title: string;
|
|
567
|
+
|
|
568
|
+
@Column()
|
|
569
|
+
content: string;
|
|
570
|
+
|
|
571
|
+
@JsonApiReadOnly()
|
|
572
|
+
@CreateDateColumn()
|
|
573
|
+
createdAt: Date;
|
|
574
|
+
|
|
575
|
+
@JsonApiReadOnly()
|
|
576
|
+
@UpdateDateColumn()
|
|
577
|
+
updatedAt: Date;
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
| Field | POST | PATCH |
|
|
582
|
+
|-------|------|-------|
|
|
583
|
+
| `slug` | Optional | Not accepted |
|
|
584
|
+
| `title` | Required | Optional |
|
|
585
|
+
| `content` | Required | Optional |
|
|
586
|
+
| `createdAt` | Not accepted | Not accepted |
|
|
587
|
+
| `updatedAt` | Not accepted | Not accepted |
|
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.10",
|
|
19
4
|
"type": "commonjs",
|
|
20
5
|
"description": "JsonApi Plugin for NestJs",
|
|
21
6
|
"keywords": [
|
|
@@ -27,32 +12,51 @@
|
|
|
27
12
|
"microorm",
|
|
28
13
|
"CRUD"
|
|
29
14
|
],
|
|
30
|
-
"
|
|
31
|
-
"@
|
|
32
|
-
"@nestjs/
|
|
33
|
-
"@nestjs/
|
|
34
|
-
"@nestjs/swagger": "^11.0.4",
|
|
35
|
-
"change-case-commonjs": "^5.4.4",
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@nestjs/common": "^11.1.0",
|
|
17
|
+
"@nestjs/core": "^11.1.0",
|
|
18
|
+
"@nestjs/swagger": "^11.2.0",
|
|
36
19
|
"reflect-metadata": "^0.1.12 || ^0.2.0",
|
|
37
20
|
"rxjs": "^7.1.0",
|
|
38
|
-
"
|
|
39
|
-
"tslib": ">2.3.0",
|
|
40
|
-
"zod": "^3.24.1",
|
|
41
|
-
"zod-validation-error": "^3.4.0"
|
|
21
|
+
"tslib": ">2.3.0"
|
|
42
22
|
},
|
|
43
|
-
"
|
|
44
|
-
"
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@klerick/json-api-nestjs-shared": "^1.0.0-beta.4",
|
|
25
|
+
"zod": "^4.1.0",
|
|
26
|
+
"zod-validation-error": "^4.0.0"
|
|
45
27
|
},
|
|
46
|
-
"
|
|
47
|
-
"
|
|
28
|
+
"overrides": {
|
|
29
|
+
"mikro-orm-pglite": {
|
|
30
|
+
"@mikro-orm/postgresql": "^6.4.0"
|
|
31
|
+
}
|
|
48
32
|
},
|
|
49
33
|
"exports": {
|
|
50
34
|
"./package.json": "./package.json",
|
|
51
35
|
".": {
|
|
52
|
-
"types": "
|
|
53
|
-
"default": "
|
|
36
|
+
"types": "./src/index.d.ts",
|
|
37
|
+
"default": "./src/index.js"
|
|
54
38
|
}
|
|
55
39
|
},
|
|
56
|
-
"types": "
|
|
57
|
-
"main": "
|
|
40
|
+
"types": "./src/index.d.ts",
|
|
41
|
+
"main": "./src/index.js",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"contributors": [
|
|
44
|
+
{
|
|
45
|
+
"email": "klerick666@gmain.com",
|
|
46
|
+
"name": "Aleksandr Kharkovey"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "https://github.com/klerick/nestjs-json-api.git"
|
|
52
|
+
},
|
|
53
|
+
"engines": {
|
|
54
|
+
"node": ">= 20.0.0"
|
|
55
|
+
},
|
|
56
|
+
"files": [
|
|
57
|
+
"**/*"
|
|
58
|
+
],
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public"
|
|
61
|
+
}
|
|
58
62
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export { JsonApiModule } from './lib/json-api.module';
|
|
2
2
|
export { JsonBaseController } from './lib/modules/mixin/controllers/json-base.controller';
|
|
3
|
-
export { JsonApi, InjectService } from './lib/modules/mixin/decorators';
|
|
4
|
-
export { entityForClass, excludeMethod, } from './lib/modules/mixin/helpers/utils';
|
|
5
|
-
export { PrepareParams, NestProvider, ValidateQueryError, UnionToTuple, EntityParam, TypeField, } from './lib/types';
|
|
6
|
-
export { JsonApiTransformerService, ErrorFormatService, } from './lib/modules/mixin/service';
|
|
7
|
-
export { MODULE_OPTIONS_TOKEN, CONTROLLER_OPTIONS_TOKEN, ASC, DESC, FIND_ONE_ROW_ENTITY, CHECK_RELATION_NAME, RUN_IN_TRANSACTION_FUNCTION, ORM_SERVICE, ENTITY_PARAM_MAP, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_PAGE, CURRENT_ENTITY, } from './lib/constants';
|
|
8
|
-
export { OrmService, OrmService as JsonApiService, EntityControllerParam, CheckRelationName, FindOneRowEntity, RunInTransaction, EntityParamMap, } from './lib/modules/mixin/types';
|
|
3
|
+
export { JsonApi, InjectService, JsonApiReadOnly, JsonApiImmutable } from './lib/modules/mixin/decorators';
|
|
4
|
+
export { entityForClass, excludeMethod, patchInputData, } from './lib/modules/mixin/helpers/utils';
|
|
5
|
+
export { PrepareParams, NestProvider, ValidateQueryError, UnionToTuple, EntityParam, TypeField, Params, JsonApiReadOnlyField, JsonApiImmutableField, } from './lib/types';
|
|
6
|
+
export { JsonApiTransformerService, ErrorFormatService, EntityParamMapService } from './lib/modules/mixin/service';
|
|
7
|
+
export { MODULE_OPTIONS_TOKEN, CONTROLLER_OPTIONS_TOKEN, ASC, DESC, FIND_ONE_ROW_ENTITY, CHECK_RELATION_NAME, RUN_IN_TRANSACTION_FUNCTION, ORM_SERVICE, ENTITY_PARAM_MAP, DEFAULT_PAGE_SIZE, DEFAULT_QUERY_PAGE, CURRENT_ENTITY, METHOD_NAME } from './lib/constants';
|
|
8
|
+
export { OrmService, OrmService as JsonApiService, EntityControllerParam, CheckRelationName, FindOneRowEntity, RunInTransaction, EntityParamMap, MethodName } from './lib/modules/mixin/types';
|
|
9
9
|
export { PatchData, PatchRelationshipData, PostData, PostRelationshipData, Query, QueryOne, SortQuery, Relationships, } from './lib/modules/mixin/zod';
|