@klerick/json-api-nestjs 10.0.0-beta.1 → 10.0.0-beta.11

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.
Files changed (153) hide show
  1. package/CHANGELOG.md +217 -0
  2. package/README.md +614 -0
  3. package/package.json +38 -34
  4. package/src/index.d.ts +6 -6
  5. package/src/index.js +6 -1
  6. package/src/index.js.map +1 -1
  7. package/src/lib/constants/constants.d.ts +4 -0
  8. package/src/lib/constants/constants.js +12 -1
  9. package/src/lib/constants/constants.js.map +1 -1
  10. package/src/lib/constants/reflection.d.ts +2 -0
  11. package/src/lib/constants/reflection.js +3 -1
  12. package/src/lib/constants/reflection.js.map +1 -1
  13. package/src/lib/json-api.module.js +3 -2
  14. package/src/lib/json-api.module.js.map +1 -1
  15. package/src/lib/modules/atomic-operation/atomic-operation.module.d.ts +1 -2
  16. package/src/lib/modules/atomic-operation/atomic-operation.module.js +12 -5
  17. package/src/lib/modules/atomic-operation/atomic-operation.module.js.map +1 -1
  18. package/src/lib/modules/atomic-operation/factory/map-controller-entity.d.ts +1 -2
  19. package/src/lib/modules/atomic-operation/factory/map-controller-entity.js +8 -7
  20. package/src/lib/modules/atomic-operation/factory/map-controller-entity.js.map +1 -1
  21. package/src/lib/modules/atomic-operation/pipes/input-operation.pipe.js +1 -4
  22. package/src/lib/modules/atomic-operation/pipes/input-operation.pipe.js.map +1 -1
  23. package/src/lib/modules/atomic-operation/service/execute.service.d.ts +6 -0
  24. package/src/lib/modules/atomic-operation/service/execute.service.js +46 -2
  25. package/src/lib/modules/atomic-operation/service/execute.service.js.map +1 -1
  26. package/src/lib/modules/atomic-operation/service/explorer.service.js +1 -1
  27. package/src/lib/modules/atomic-operation/service/explorer.service.js.map +1 -1
  28. package/src/lib/modules/atomic-operation/service/swagger.service.js +2 -2
  29. package/src/lib/modules/atomic-operation/service/swagger.service.js.map +1 -1
  30. package/src/lib/modules/atomic-operation/utils/zod/zod-helper.d.ts +35 -680
  31. package/src/lib/modules/atomic-operation/utils/zod/zod-helper.js +6 -7
  32. package/src/lib/modules/atomic-operation/utils/zod/zod-helper.js.map +1 -1
  33. package/src/lib/modules/mixin/config/bindings.js +9 -9
  34. package/src/lib/modules/mixin/config/bindings.js.map +1 -1
  35. package/src/lib/modules/mixin/decorators/index.d.ts +1 -0
  36. package/src/lib/modules/mixin/decorators/index.js +1 -0
  37. package/src/lib/modules/mixin/decorators/index.js.map +1 -1
  38. package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.d.ts +48 -0
  39. package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.js +83 -0
  40. package/src/lib/modules/mixin/decorators/json-api-read-only/json-api-read-only.decorator.js.map +1 -0
  41. package/src/lib/modules/mixin/factory/zod-validate.factory.js +11 -6
  42. package/src/lib/modules/mixin/factory/zod-validate.factory.js.map +1 -1
  43. package/src/lib/modules/mixin/helpers/bind-controller.js +10 -3
  44. package/src/lib/modules/mixin/helpers/bind-controller.js.map +1 -1
  45. package/src/lib/modules/mixin/helpers/create-controller.js +3 -0
  46. package/src/lib/modules/mixin/helpers/create-controller.js.map +1 -1
  47. package/src/lib/modules/mixin/helpers/utils.d.ts +2 -0
  48. package/src/lib/modules/mixin/helpers/utils.js +10 -0
  49. package/src/lib/modules/mixin/helpers/utils.js.map +1 -1
  50. package/src/lib/modules/mixin/mixin.module.js +1 -0
  51. package/src/lib/modules/mixin/mixin.module.js.map +1 -1
  52. package/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.js +1 -4
  53. package/src/lib/modules/mixin/pipe/patch-input/patch-input.pipe.js.map +1 -1
  54. package/src/lib/modules/mixin/pipe/patch-relationship/patch-relationship.pipe.js +1 -4
  55. package/src/lib/modules/mixin/pipe/patch-relationship/patch-relationship.pipe.js.map +1 -1
  56. package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.d.ts +1 -0
  57. package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.js +10 -4
  58. package/src/lib/modules/mixin/pipe/post-input/post-input.pipe.js.map +1 -1
  59. package/src/lib/modules/mixin/pipe/post-relationship/post-relationship.pipe.js +1 -4
  60. package/src/lib/modules/mixin/pipe/post-relationship/post-relationship.pipe.js.map +1 -1
  61. package/src/lib/modules/mixin/pipe/query-filed-on-include/query-filed-in-include.pipe.js +2 -4
  62. package/src/lib/modules/mixin/pipe/query-filed-on-include/query-filed-in-include.pipe.js.map +1 -1
  63. package/src/lib/modules/mixin/pipe/query-input/query-input.pipe.js +1 -4
  64. package/src/lib/modules/mixin/pipe/query-input/query-input.pipe.js.map +1 -1
  65. package/src/lib/modules/mixin/service/json-api-transformer.service.js +27 -4
  66. package/src/lib/modules/mixin/service/json-api-transformer.service.js.map +1 -1
  67. package/src/lib/modules/mixin/swagger/method/delete-relationship.js +3 -3
  68. package/src/lib/modules/mixin/swagger/method/delete-relationship.js.map +1 -1
  69. package/src/lib/modules/mixin/swagger/method/index.d.ts +0 -4
  70. package/src/lib/modules/mixin/swagger/method/index.js.map +1 -1
  71. package/src/lib/modules/mixin/swagger/method/patch-one.js +6 -3
  72. package/src/lib/modules/mixin/swagger/method/patch-one.js.map +1 -1
  73. package/src/lib/modules/mixin/swagger/method/patch-relationship.js +3 -3
  74. package/src/lib/modules/mixin/swagger/method/patch-relationship.js.map +1 -1
  75. package/src/lib/modules/mixin/swagger/method/post-one.js +6 -3
  76. package/src/lib/modules/mixin/swagger/method/post-one.js.map +1 -1
  77. package/src/lib/modules/mixin/swagger/method/post-relationship.js +3 -3
  78. package/src/lib/modules/mixin/swagger/method/post-relationship.js.map +1 -1
  79. package/src/lib/modules/mixin/swagger/utils.d.ts +2 -0
  80. package/src/lib/modules/mixin/swagger/utils.js +16 -1
  81. package/src/lib/modules/mixin/swagger/utils.js.map +1 -1
  82. package/src/lib/modules/mixin/types/decorator-options.type.d.ts +1 -0
  83. package/src/lib/modules/mixin/types/orm-service.type.d.ts +8 -0
  84. package/src/lib/modules/mixin/zod/index.d.ts +1 -0
  85. package/src/lib/modules/mixin/zod/index.js +6 -0
  86. package/src/lib/modules/mixin/zod/index.js.map +1 -1
  87. package/src/lib/modules/mixin/zod/map-transform-to-json-shema.d.ts +6 -0
  88. package/src/lib/modules/mixin/zod/map-transform-to-json-shema.js +27 -0
  89. package/src/lib/modules/mixin/zod/map-transform-to-json-shema.js.map +1 -0
  90. package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.d.ts +4 -44
  91. package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.js +2 -3
  92. package/src/lib/modules/mixin/zod/zod-input-patch-relationship-schema/index.js.map +1 -1
  93. package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.d.ts +16 -5
  94. package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.js +7 -9
  95. package/src/lib/modules/mixin/zod/zod-input-patch-schema/index.js.map +1 -1
  96. package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.d.ts +4 -50
  97. package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.js +2 -3
  98. package/src/lib/modules/mixin/zod/zod-input-post-relationship-schema/index.js.map +1 -1
  99. package/src/lib/modules/mixin/zod/zod-input-post-schema/index.d.ts +16 -6
  100. package/src/lib/modules/mixin/zod/zod-input-post-schema/index.js +7 -13
  101. package/src/lib/modules/mixin/zod/zod-input-post-schema/index.js.map +1 -1
  102. package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.d.ts +13 -27
  103. package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.js +20 -13
  104. package/src/lib/modules/mixin/zod/zod-input-query-schema/fields.js.map +1 -1
  105. package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.d.ts +72 -987
  106. package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.js +11 -7
  107. package/src/lib/modules/mixin/zod/zod-input-query-schema/filter.js.map +1 -1
  108. package/src/lib/modules/mixin/zod/zod-input-query-schema/include.d.ts +1 -1
  109. package/src/lib/modules/mixin/zod/zod-input-query-schema/index.d.ts +73 -4933
  110. package/src/lib/modules/mixin/zod/zod-input-query-schema/index.js +6 -3
  111. package/src/lib/modules/mixin/zod/zod-input-query-schema/index.js.map +1 -1
  112. package/src/lib/modules/mixin/zod/zod-input-query-schema/sort.d.ts +1 -1
  113. package/src/lib/modules/mixin/zod/zod-query-schema/fields.d.ts +3 -3
  114. package/src/lib/modules/mixin/zod/zod-query-schema/fields.js +6 -2
  115. package/src/lib/modules/mixin/zod/zod-query-schema/fields.js.map +1 -1
  116. package/src/lib/modules/mixin/zod/zod-query-schema/filter.d.ts +24 -104
  117. package/src/lib/modules/mixin/zod/zod-query-schema/filter.js +32 -10
  118. package/src/lib/modules/mixin/zod/zod-query-schema/filter.js.map +1 -1
  119. package/src/lib/modules/mixin/zod/zod-query-schema/include.d.ts +1 -1
  120. package/src/lib/modules/mixin/zod/zod-query-schema/include.js +1 -1
  121. package/src/lib/modules/mixin/zod/zod-query-schema/include.js.map +1 -1
  122. package/src/lib/modules/mixin/zod/zod-query-schema/index.d.ts +4 -4
  123. package/src/lib/modules/mixin/zod/zod-query-schema/index.js +3 -4
  124. package/src/lib/modules/mixin/zod/zod-query-schema/index.js.map +1 -1
  125. package/src/lib/modules/mixin/zod/zod-query-schema/sort.d.ts +11 -3
  126. package/src/lib/modules/mixin/zod/zod-query-schema/sort.js +1 -1
  127. package/src/lib/modules/mixin/zod/zod-query-schema/sort.js.map +1 -1
  128. package/src/lib/modules/mixin/zod/zod-share/attributes.d.ts +15 -14
  129. package/src/lib/modules/mixin/zod/zod-share/attributes.js +26 -20
  130. package/src/lib/modules/mixin/zod/zod-share/attributes.js.map +1 -1
  131. package/src/lib/modules/mixin/zod/zod-share/page.d.ts +3 -9
  132. package/src/lib/modules/mixin/zod/zod-share/rel-data.d.ts +4 -40
  133. package/src/lib/modules/mixin/zod/zod-share/rel-data.js +2 -2
  134. package/src/lib/modules/mixin/zod/zod-share/rel-data.js.map +1 -1
  135. package/src/lib/modules/mixin/zod/zod-share/relationships.d.ts +8 -304
  136. package/src/lib/modules/mixin/zod/zod-share/relationships.js +1 -1
  137. package/src/lib/modules/mixin/zod/zod-share/relationships.js.map +1 -1
  138. package/src/lib/modules/mixin/zod/zod-utils.d.ts +2 -1
  139. package/src/lib/modules/mixin/zod/zod-utils.js +12 -0
  140. package/src/lib/modules/mixin/zod/zod-utils.js.map +1 -1
  141. package/src/lib/types/entity-param.type.d.ts +6 -4
  142. package/src/lib/types/entity-param.type.js +13 -0
  143. package/src/lib/types/entity-param.type.js.map +1 -1
  144. package/src/lib/types/error.types.d.ts +3 -3
  145. package/src/lib/types/index.d.ts +1 -0
  146. package/src/lib/types/index.js +1 -0
  147. package/src/lib/types/index.js.map +1 -1
  148. package/src/lib/types/json-api-read-only.d.ts +51 -0
  149. package/src/lib/types/json-api-read-only.js +3 -0
  150. package/src/lib/types/json-api-read-only.js.map +1 -0
  151. package/src/lib/types/module-options.types.d.ts +6 -1
  152. package/src/lib/utils/module-helper.js +5 -0
  153. package/src/lib/utils/module-helper.js.map +1 -1
package/README.md ADDED
@@ -0,0 +1,614 @@
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
+ excludeControllers?: Entity[]; // List of entities to exclude from automatic controller generation
83
+ controllers?: NestController[]; // List of controller, if you need extend default present
84
+ connectionName?: string; // Type orm connection name: "default" is default name
85
+ providers?: NestProvider[]; // List of addition provider for useing in custom controller
86
+ imports?: NestImport[]; // List of addition module for useing in custom controller
87
+ options?: {
88
+ requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
89
+ debug?: boolean; // Debug info in result object, like error message
90
+ pipeForId?: Type<PipeTransform> // Nestjs pipe for validate id params, by default ParseIntPipe
91
+ operationUrl?: string // Url for atomic operation https://jsonapi.org/ext/atomic/
92
+ // You can add params for MicroOrm or TypeOrm adapter
93
+ } ;
94
+ }
95
+ ```
96
+
97
+ ### Excluding Controllers
98
+
99
+ If you need to register entities for relationships but don't want automatic controller generation for some of them, use `excludeControllers`:
100
+
101
+ ```typescript
102
+ import {Module} from '@nestjs/common';
103
+ import {JsonApiModule} from '@klerick/json-api-nestjs';
104
+ import {TypeOrmJsonApiModule} from '@klerick/json-api-nestjs-typeorm';
105
+ import {Users, Roles, AuditLog} from 'database';
106
+
107
+ @Module({
108
+ imports: [
109
+ JsonApiModule.forRoot(TypeOrmJsonApiModule, {
110
+ entities: [Users, Roles, AuditLog],
111
+ excludeControllers: [AuditLog], // AuditLog entity will not have auto-generated controller
112
+ }),
113
+ ],
114
+ })
115
+ export class AppModule {}
116
+ ```
117
+
118
+ This is useful when:
119
+ - You want to manage certain entities only through relationships
120
+ - You need custom controller implementation without the auto-generated one
121
+ - Some entities should be internal and not exposed via REST API
122
+
123
+ You can extend the default controller:
124
+
125
+ ```typescript
126
+ import {Get, Param, Inject, BadRequestException} from '@nestjs/common';
127
+
128
+ import {Users} from 'database';
129
+ import {
130
+ JsonApi,
131
+ excludeMethod,
132
+ JsonBaseController,
133
+ InjectService,
134
+ JsonApiService,
135
+ Query,
136
+ } from '@klerick/json-api-nestjs';
137
+ import {
138
+ ResourceObjectRelationships,
139
+ } from '@klerick/json-api-nestjs-shared';
140
+ import {ExampleService} from '../../service/example/example.service';
141
+
142
+ @JsonApi(Users, {
143
+ allowMethod: excludeMethod(['deleteRelationship']),
144
+ requiredSelectField: true,
145
+ overrideRoute: 'user',
146
+ })
147
+ export class ExtendUserController extends JsonBaseController<Users, 'id'> {
148
+ @InjectService() public service: JsonApiService<Users>;
149
+ @Inject(ExampleService) protected exampleService: ExampleService;
150
+
151
+ public override getAll(query: Query<Users, 'id'>): Promise<ResourceObject<Users, 'array'>> {
152
+ if (!this.exampleService.someCheck(query)) {
153
+ throw new BadRequestException({});
154
+ }
155
+ return this.service.getAll(query);// OR call parent method: super.getAll(query);
156
+ }
157
+
158
+ public override patchRelationship<Rel extends EntityRelation<Users>>(
159
+ id: string | number,
160
+ relName: Rel,
161
+ input: PatchRelationshipData
162
+ ): Promise<ResourceObjectRelationships<Users, 'id', Rel>> {
163
+ return super.patchRelationship(id, relName, input);
164
+ }
165
+
166
+ @Get(':id/example')
167
+ testOne(@Param('id') id: string): string {
168
+ return this.exampleService.testMethode(id);
169
+ }
170
+ }
171
+ ```
172
+
173
+ You can overwrite the default config for the current controller using options in the decorator **JsonAPi**.
174
+ Also you can specify an API method necessary for you, using **allowMethod**
175
+ Defulat validation check only simple type and database structure.
176
+ If you need custom pipe validation you can your owner pipe like this:
177
+
178
+ ```typescript
179
+
180
+ import { Query } from '@nestjs/common';
181
+ import {
182
+ JsonApi,
183
+ excludeMethod,
184
+ JsonBaseController,
185
+ InjectService,
186
+ JsonApiService,
187
+ Query as QueryType,
188
+ } from 'json-api-nestjs';
189
+
190
+ @JsonApi(Users, {
191
+ allowMethod: excludeMethod(['deleteRelationship']),
192
+ requiredSelectField: true,
193
+ overrideRoute: 'user',
194
+ })
195
+ export class ExtendUserController extends JsonBaseController<Users, 'id'> {
196
+ @InjectService() public service: JsonApiService<Users>;
197
+ @Inject(ExampleService) protected exampleService: ExampleService;
198
+
199
+ public override getAll(
200
+ @Query(ExamplePipe) query: QueryType<Users, 'id'>
201
+ ): Promise<ResourceObject<Users, 'array'>> {
202
+ return super.getAll(query);
203
+ }
204
+ }
205
+ ```
206
+
207
+ ```typescript
208
+ import { ArgumentMetadata, PipeTransform } from '@nestjs/common';
209
+
210
+ import { Query } from 'json-api-nestjs';
211
+ import { Users } from 'database';
212
+
213
+ export class ExamplePipe implements PipeTransform<Query<Users, 'id'>, Query<Users, 'id'>> {
214
+ transform(value: Query<Users, 'id'>, metadata: ArgumentMetadata): Query<Users, 'id'> {
215
+ return value;
216
+ }
217
+ }
218
+ ```
219
+
220
+ ## Swagger UI
221
+
222
+ For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction) and configure it
223
+ ```typescript
224
+ const app = await NestFactory.create(AppModule);
225
+
226
+ const config = new DocumentBuilder()
227
+ .setOpenAPIVersion('3.1.0')
228
+ .setTitle('JSON API swagger example')
229
+ .setDescription('The JSON API list example')
230
+ .setVersion('1.0')
231
+ .build();
232
+
233
+ SwaggerModule.setup(
234
+ 'swagger',
235
+ app,
236
+ () => SwaggerModule.createDocument(app, config), // !!!Important: document as factory
237
+ {}
238
+ );
239
+
240
+ ```
241
+
242
+ ## Available endpoint method
243
+
244
+ Using **Users** entity and relation **Roles** entity as example
245
+
246
+ ### List item of Users
247
+
248
+ ```
249
+ GET /users
250
+ ```
251
+
252
+ Available query params:
253
+
254
+ - **include** - you can extend result with relations (aka join)
255
+ ```
256
+ GET /users?include=roles
257
+ ```
258
+ result of request will have role relation for each **Users** item
259
+
260
+ - **fields** - you can specify required fields of result query
261
+
262
+ ```
263
+ GET /users?fields[target]=login,lastName&fileds[roles]=name,key
264
+ ```
265
+ The "target" is **Users** entity
266
+ The "roles" is **Roles** entity
267
+ So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *
268
+ key* for **Roles** entity
269
+ - **sort** - you can sort result of the request
270
+
271
+ ```
272
+ GET /users?sort=target.name,-roles.key
273
+ ```
274
+ The "target" is **Users** entity
275
+ The "roles" is **Roles** entity
276
+ So, result of the request will be sorted by field *name* of **Users** by *ASC* and field *key* of **Roles** entity
277
+ by **DESC**.
278
+ - **page** - pagination for you request
279
+
280
+ ```
281
+ GET /users?page[number]=1page[size]=20
282
+ ```
283
+ - **filter** - filter for query
284
+
285
+ ```
286
+ GET /users?filter[name][eq]=1&filter[roles.name][ne]=test&filter[roles.status][eq]=true
287
+ ```
288
+ The "name" is a field of **Users** entity
289
+ The "roles.name" is *name* field of **Roles** entity
290
+ The "eq", "ne" is *[Filter operand](#filter-operand)*
291
+
292
+ So, this query will be transformed like sql:
293
+ ```sql
294
+ WHERE users.name = 1 AND roles.name <> 'test' AND roles.status = true
295
+ ```
296
+
297
+ ## Filter operand
298
+
299
+ ```typescript
300
+ type FilterOperand
301
+ {
302
+ in:string[] // is equal to the conditional of query "WHERE 'attribute_name' IN ('value1', 'value2')"
303
+ nin: string[] // is equal to the conditional of query "WHERE 'attribute_name' NOT IN ('value1', 'value1')"
304
+ eq: string // is equal to the conditional of query "WHERE 'attribute_name' = 'value1'
305
+ ne: string // is equal to the conditional of query "WHERE 'attribute_name' <> 'value1'
306
+ gte: string // is equal to the conditional of query "WHERE 'attribute_name' >= 'value1'
307
+ gt: string // is equal to the conditional of query "WHERE 'attribute_name' > 'value1'
308
+ lt: string // is equal to the conditional of query "WHERE 'attribute_name' < 'value1'
309
+ lte:string // is equal to the conditional of query "WHERE 'attribute_name' <= 'value1'
310
+ regexp: string // is equal to the conditional of query "WHERE 'attribute_name' ~* value1
311
+ some: string // is equal to the conditional of query "WHERE 'attribute_name' && [value1]
312
+ }
313
+ ```
314
+
315
+ ### Get item of Users
316
+
317
+ ```
318
+ GET /users/:id
319
+ ```
320
+ - **include** - you can extend result with relations (aka join)
321
+ ```
322
+ GET /users?include=roles
323
+ ```
324
+ result of request will have role relation for each **Users** item
325
+
326
+ - **fields** - you can specify required fields of result query
327
+
328
+ ```
329
+ GET /users?fields[target]=login,lastName&fileds[roles]=name,key
330
+ ```
331
+ The "target" is **Users** entity
332
+ The "roles" is **Roles** entity
333
+ So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *
334
+ key* for **Roles** entity
335
+
336
+ ### Create item of Users
337
+ ```
338
+ POST /users
339
+ ```
340
+
341
+ - **body** - Create new User and add link to address
342
+
343
+ ```json
344
+ {
345
+ "data": {
346
+ "type": "users",
347
+ "attributes": {
348
+ "id": 0,
349
+ "login": "string",
350
+ "firstName": "string",
351
+ "lastName": "string",
352
+ "isActive": true,
353
+ "createdAt": "2023-12-08T10:32:27.352Z",
354
+ "updatedAt": "2023-12-08T10:32:27.352Z"
355
+ },
356
+ "relationships": {
357
+ "addresses": {
358
+ "id": "1",
359
+ "type": "addresses"
360
+ }
361
+ }
362
+ }
363
+ }
364
+ ```
365
+ ### Update item of Users
366
+ ```
367
+ PATCH /users/:id
368
+ ```
369
+
370
+ - **body** - Update User with id 1 and update link to address and manager
371
+
372
+ ```json
373
+ {
374
+ "data": {
375
+ "id": "1",
376
+ "type": "users",
377
+ "attributes": {
378
+ "id": 0,
379
+ "login": "string",
380
+ "firstName": "string",
381
+ "lastName": "string",
382
+ "isActive": true,
383
+ "createdAt": "2023-12-08T10:34:57.752Z",
384
+ "updatedAt": "2023-12-08T10:34:57.752Z"
385
+ },
386
+ "relationships": {
387
+ "addresses": {
388
+ "id": "2",
389
+ "type": "addresses"
390
+ },
391
+ "manager": {
392
+ "id": "2",
393
+ "type": "users"
394
+ }
395
+ }
396
+ }
397
+ }
398
+
399
+ ```
400
+
401
+ ### Atomic operation
402
+ You can more information find [here](https://jsonapi.org/ext/atomic/)
403
+ But you have additinal feature.
404
+ For example: you will need create new **Roles**, then - create new **Users** and assign new **Roles** to new **Users**.
405
+ If use native json api you need send 3 http request:
406
+ - POST /roles
407
+ - POST /users
408
+
409
+ but [Atomic operation](https://jsonapi.org/ext/atomic/) allow for one request.
410
+ ```json
411
+
412
+ {
413
+ "atomic:operations":[
414
+ {
415
+ "op":"add",
416
+ "ref":{
417
+ "type":"roles",
418
+ "tmpId":10000
419
+ },
420
+ "data":{
421
+ "type":"roles",
422
+ "attributes":{
423
+ "name":"testRolesAgain",
424
+ "key":"testRolesAgain"
425
+ }
426
+ }
427
+ },
428
+ {
429
+ "op":"add",
430
+ "ref":{
431
+ "type":"users"
432
+ },
433
+ "data":{
434
+ "type":"users",
435
+ "attributes":{
436
+ "login":"newLogin"
437
+ },
438
+ "relationships":{
439
+ "addresses":{
440
+ "type":"addresses",
441
+ "id":"1"
442
+ },
443
+ "roles":[
444
+ {
445
+ "type":"roles",
446
+ "id":"10000"
447
+ }
448
+ ]
449
+ }
450
+ }
451
+ }
452
+ ]
453
+ }
454
+
455
+ ```
456
+ **tmpId** - is params for operation **add**, should be unique for all operations.
457
+
458
+ If you have Interceptor you can check call it from **AtomicOperation**
459
+
460
+
461
+
462
+ ```ts
463
+ @Injectable()
464
+ export class AtomicInterceptor<T> implements NestInterceptor {
465
+ intercept(context: ExecutionContext, next: CallHandler<T>): Observable<T> {
466
+ const isAtomic = context.getArgByIndex(3)
467
+ if (isAtomic) {
468
+ console.log('call from atomic operation')
469
+ }
470
+ return next.handle();
471
+ }
472
+ }
473
+ ```
474
+ **isAtomic** - is array of params of method
475
+
476
+ ## Resource Linkage for To-One Relations
477
+
478
+ 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).
479
+
480
+ 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`**.
481
+
482
+ **Example response:**
483
+ ```json
484
+ {
485
+ "data": {
486
+ "id": "1",
487
+ "type": "comments",
488
+ "attributes": { "text": "Hello" },
489
+ "relationships": {
490
+ "user": {
491
+ "links": { "self": "/api/comments/1/relationships/user" },
492
+ "data": { "id": "5", "type": "users" }
493
+ }
494
+ }
495
+ }
496
+ }
497
+ ```
498
+
499
+ If the FK field is `null`, then `data` will also be `null`:
500
+ ```json
501
+ "relationships": {
502
+ "user": {
503
+ "links": { "self": "/api/comments/1/relationships/user" },
504
+ "data": null
505
+ }
506
+ }
507
+ ```
508
+
509
+ **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.
510
+
511
+ ## Field Access Control Decorators
512
+
513
+ The library provides two decorators to control field accessibility in POST and PATCH operations:
514
+
515
+ ### @JsonApiReadOnly()
516
+
517
+ 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.
518
+
519
+ **Use case:** Server-managed fields like `createdAt`, `updatedAt`, computed fields.
520
+
521
+ ```typescript
522
+ import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn } from 'typeorm';
523
+ import { JsonApiReadOnly } from '@klerick/json-api-nestjs';
524
+
525
+ @Entity()
526
+ export class Users {
527
+ @PrimaryGeneratedColumn()
528
+ id: number;
529
+
530
+ @Column()
531
+ login: string;
532
+
533
+ @JsonApiReadOnly()
534
+ @CreateDateColumn()
535
+ createdAt: Date;
536
+
537
+ @JsonApiReadOnly()
538
+ @UpdateDateColumn()
539
+ updatedAt: Date;
540
+ }
541
+ ```
542
+
543
+ With this configuration:
544
+ - **POST /users** - `createdAt` and `updatedAt` fields are not accepted
545
+ - **PATCH /users/:id** - `createdAt` and `updatedAt` fields are not accepted
546
+
547
+ ### @JsonApiImmutable()
548
+
549
+ Fields marked with `@JsonApiImmutable()` can be optionally set during creation (POST) but cannot be modified after (PATCH).
550
+
551
+ **Use case:** Fields that should be set once and never changed, like `externalId`, `slug`, or `username`.
552
+
553
+ ```typescript
554
+ import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
555
+ import { JsonApiImmutable } from '@klerick/json-api-nestjs';
556
+
557
+ @Entity()
558
+ export class Users {
559
+ @PrimaryGeneratedColumn()
560
+ id: number;
561
+
562
+ @JsonApiImmutable()
563
+ @Column({ unique: true })
564
+ login: string;
565
+
566
+ @Column()
567
+ firstName: string;
568
+
569
+ @Column()
570
+ lastName: string;
571
+ }
572
+ ```
573
+
574
+ With this configuration:
575
+ - **POST /users** - `login` is optional, can be provided or omitted
576
+ - **PATCH /users/:id** - `login` is not accepted (cannot be changed)
577
+
578
+ ### Combining Decorators
579
+
580
+ You can use both decorators in the same entity:
581
+
582
+ ```typescript
583
+ @Entity()
584
+ export class Article {
585
+ @PrimaryGeneratedColumn()
586
+ id: number;
587
+
588
+ @JsonApiImmutable()
589
+ @Column({ unique: true })
590
+ slug: string;
591
+
592
+ @Column()
593
+ title: string;
594
+
595
+ @Column()
596
+ content: string;
597
+
598
+ @JsonApiReadOnly()
599
+ @CreateDateColumn()
600
+ createdAt: Date;
601
+
602
+ @JsonApiReadOnly()
603
+ @UpdateDateColumn()
604
+ updatedAt: Date;
605
+ }
606
+ ```
607
+
608
+ | Field | POST | PATCH |
609
+ |-------|------|-------|
610
+ | `slug` | Optional | Not accepted |
611
+ | `title` | Required | Optional |
612
+ | `content` | Required | Optional |
613
+ | `createdAt` | Not accepted | Not accepted |
614
+ | `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.1",
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.11",
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
- "dependencies": {
31
- "@anatine/zod-openapi": "^2.2.3",
32
- "@nestjs/common": "^11.0.10",
33
- "@nestjs/core": "^11.0.10",
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
- "ts-toolbelt": "^9.6.0",
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
- "publishConfig": {
44
- "access": "public"
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
- "peerDependencies": {
47
- "@klerick/json-api-nestjs-shared": "1.0.0-beta.1"
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": "./../../../dist/libs/json-api/json-api-nestjs-shared/libs/json-api/json-api-nestjs/src/index.d.ts",
53
- "default": "./../../../dist/libs/json-api/json-api-nestjs-shared/libs/json-api/json-api-nestjs/src/index.js"
36
+ "types": "./src/index.d.ts",
37
+ "default": "./src/index.js"
54
38
  }
55
39
  },
56
- "types": "./../../../dist/libs/json-api/json-api-nestjs-shared/libs/json-api/json-api-nestjs/src/index.d.ts",
57
- "main": "./../../../dist/libs/json-api/json-api-nestjs-shared/libs/json-api/json-api-nestjs/src/index.js"
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
  }