Skip to content

Commit fddb7f2

Browse files
author
Alexander Kharkovey
committed
feat(json-api-nestjs): release new version
fix name for type allow id as string change readme add import options
1 parent daa9a9b commit fddb7f2

File tree

1 file changed

+85
-70
lines changed

1 file changed

+85
-70
lines changed

libs/json-api-nestjs/README.md

Lines changed: 85 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# json-api-nestjs
22

3-
This plugin works upon TypeOrm library, which is used as the main database abstraction layer tool. The module automatically generates an API according to JSON API specification from the database structure (TypeORM entities). It supports features such as requests validation based on database fields types, request filtering, endpoints extending, data relations control and much more. Our module significantly reduces the development time of REST services by removing the need to negotiate the mechanism of client-server interaction and implementing automatic API generation without the need to write any code.
3+
This plugin works upon TypeOrm library, which is used as the main database abstraction layer tool. The module
4+
automatically generates an API according to JSON API specification from the database structure (TypeORM entities). It
5+
supports features such as requests validation based on database fields types, request filtering, endpoints extending,
6+
data relations control and much more. Our module significantly reduces the development time of REST services by removing
7+
the need to negotiate the mechanism of client-server interaction and implementing automatic API generation without the
8+
need to write any code.
49

510
## Installation
611

@@ -13,21 +18,22 @@ $ npm install json-api-nestjs
1318
Once the installation process is complete, we can import the **JsonApiModule** into the root **AppModule**.
1419

1520
```typescript
16-
import { Module } from '@nestjs/common';
17-
import { JsonApiModule } from 'json-api-nestjs';
18-
import { Users } from 'database';
21+
import {Module} from '@nestjs/common';
22+
import {JsonApiModule} from 'json-api-nestjs';
23+
import {Users} from 'database';
1924

2025
@Module({
2126
imports: [
22-
JsonApiModule.forRoot({
23-
entities: [Users]
24-
}),
27+
JsonApiModule.forRoot({
28+
entities: [Users]
29+
}),
2530
],
2631
})
27-
export class AppModule {}
32+
export class AppModule {
33+
}
2834
```
29-
After this, you have to prepare CRUDs with ready-to-use endpoints:
3035

36+
After this, you have to prepare CRUDs with ready-to-use endpoints:
3137

3238
- GET /users
3339
- POST /users
@@ -42,54 +48,57 @@ After this, you have to prepare CRUDs with ready-to-use endpoints:
4248
## Configuration params
4349

4450
The following interface is using for the configuration:
51+
4552
```typescript
46-
export interface ModuleOptions {
47-
entities: Entity[]; // List of typeOrm Entity
48-
controllers?: NestController[]; // List of controller, if you need extend default present
49-
connectionName?: string; // Type orm connection name: "default" is default name
50-
providers?: NestProvider[]; // List of addition provider for useing in custom controller
51-
imports?: NestImport[]; // List of addition module for useing in custom controller
52-
options?: {
53-
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
54-
debug?: boolean; // Debug info in result object
53+
export interface ModuleOptions {
54+
entities: Entity[]; // List of typeOrm Entity
55+
controllers?: NestController[]; // List of controller, if you need extend default present
56+
connectionName?: string; // Type orm connection name: "default" is default name
57+
providers?: NestProvider[]; // List of addition provider for useing in custom controller
58+
imports?: NestImport[]; // List of addition module for useing in custom controller
59+
options?: {
60+
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
61+
debug?: boolean; // Debug info in result object
5562
pipeForId?: Type<PipeTransform> // Nestjs pipe for validate id params, by default ParseIntPipe
56-
};
63+
};
5764
}
5865
```
66+
5967
You can extend the default controller:
68+
6069
```typescript
61-
import { Get, Param, Inject, BadRequestException } from '@nestjs/common';
62-
63-
import { Users } from 'database';
64-
import {
65-
JsonApi,
66-
excludeMethod,
67-
JsonBaseController,
68-
InjectService,
69-
JsonApiService,
70-
QueryParams,
71-
} from 'json-api-nestjs';
72-
import { ExampleService } from '../../service/example/example.service';
73-
74-
@JsonApi(Users, {
75-
allowMethod: excludeMethod(['deleteRelationship']),
76-
requiredSelectField: true,
77-
})
78-
export class ExtendUserController extends JsonBaseController<Users> {
79-
@InjectService() public service: JsonApiService<Users>;
80-
@Inject(ExampleService) protected exampleService: ExampleService;
81-
82-
public override getAll(query: QueryParams<Users>) {
83-
if (!this.exampleService.someCheck(query)) {
84-
throw new BadRequestException({});
85-
}
86-
return this.service.getAll({ query });
87-
}
88-
89-
@Get(':id/example')
90-
testOne(@Param('id') id: string): string {
91-
return this.exampleService.testMethode(id);
92-
}
70+
import {Get, Param, Inject, BadRequestException} from '@nestjs/common';
71+
72+
import {Users} from 'database';
73+
import {
74+
JsonApi,
75+
excludeMethod,
76+
JsonBaseController,
77+
InjectService,
78+
JsonApiService,
79+
QueryParams,
80+
} from 'json-api-nestjs';
81+
import {ExampleService} from '../../service/example/example.service';
82+
83+
@JsonApi(Users, {
84+
allowMethod: excludeMethod(['deleteRelationship']),
85+
requiredSelectField: true,
86+
})
87+
export class ExtendUserController extends JsonBaseController<Users> {
88+
@InjectService() public service: JsonApiService<Users>;
89+
@Inject(ExampleService) protected exampleService: ExampleService;
90+
91+
public override getAll(query: QueryParams<Users>) {
92+
if (!this.exampleService.someCheck(query)) {
93+
throw new BadRequestException({});
94+
}
95+
return this.service.getAll({query});
96+
}
97+
98+
@Get(':id/example')
99+
testOne(@Param('id') id: string): string {
100+
return this.exampleService.testMethode(id);
101+
}
93102
}
94103
```
95104

@@ -101,13 +110,15 @@ Also you can specify an API method necessary for you, using **allowMethod**
101110
For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction)
102111

103112
## Available endpoint method
113+
104114
Using **Users** entity and relation **Roles** entity as example
105115

106116
### List item of Users
107-
117+
108118
```
109119
GET /users
110120
```
121+
111122
Available query params:
112123

113124
- **include** - you can extend result with relations (aka join)
@@ -117,28 +128,30 @@ Available query params:
117128
result of request will have role relation for each **Users** item
118129

119130
- **fields** - you can specify required fields of result query
120-
131+
121132
```
122133
GET /users?fields[target]=login,lastName&fileds[roles]=name,key
123134
```
124135
The "target" is **Users** entity
125136
The "roles" is **Roles** entity
126-
So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *key* for **Roles** entity
137+
So, result of request will be have only fields *login* and *lastName* for **Users** entity and fields *name* and *
138+
key* for **Roles** entity
127139
- **sort** - you can sort result of the request
128-
140+
129141
```
130142
GET /users?sort=target.name,-roles.key
131143
```
132144
The "target" is **Users** entity
133145
The "roles" is **Roles** entity
134-
So, result of the request will be sorted by field *name* of **Users** by *ASC* and field *key* of **Roles** entity by **DESC**.
146+
So, result of the request will be sorted by field *name* of **Users** by *ASC* and field *key* of **Roles** entity
147+
by **DESC**.
135148
- **page** - pagination for you request
136-
149+
137150
```
138151
GET /users?page[number]=1page[size]=20
139152
```
140153
- **filter** - filter for query
141-
154+
142155
```
143156
GET /users?filter[name][eq]=1&filter[roles.name][ne]=test&filter[roles.status][eq]=true
144157
```
@@ -151,20 +164,22 @@ Available query params:
151164
WHERE users.name = 1 AND roles.name <> 'test' AND roles.status = true
152165
```
153166

154-
## Filter operand
167+
## Filter operand
155168

156169
```typescript
157-
type FilterOperand {
158-
in: string[] // is equal to the conditional of query "WHERE 'attribute_name' IN ('value1', 'value2')"
159-
nin: string[] // is equal to the conditional of query "WHERE 'attribute_name' NOT IN ('value1', 'value1')"
160-
eq: string // is equal to the conditional of query "WHERE 'attribute_name' = 'value1'
161-
ne: string // is equal to the conditional of query "WHERE 'attribute_name' <> 'value1'
162-
gte: string // is equal to the conditional of query "WHERE 'attribute_name' >= 'value1'
163-
gt: string // is equal to the conditional of query "WHERE 'attribute_name' > 'value1'
164-
lt: string // is equal to the conditional of query "WHERE 'attribute_name' < 'value1'
165-
lte:string // is equal to the conditional of query "WHERE 'attribute_name' <= 'value1'
166-
regexp: string // is equal to the conditional of query "WHERE 'attribute_name' ~* value1
167-
some: string // is equal to the conditional of query "WHERE 'attribute_name' && [value1]
170+
type FilterOperand
171+
{
172+
in:
173+
string[] // is equal to the conditional of query "WHERE 'attribute_name' IN ('value1', 'value2')"
174+
nin: string[] // is equal to the conditional of query "WHERE 'attribute_name' NOT IN ('value1', 'value1')"
175+
eq: string // is equal to the conditional of query "WHERE 'attribute_name' = 'value1'
176+
ne: string // is equal to the conditional of query "WHERE 'attribute_name' <> 'value1'
177+
gte: string // is equal to the conditional of query "WHERE 'attribute_name' >= 'value1'
178+
gt: string // is equal to the conditional of query "WHERE 'attribute_name' > 'value1'
179+
lt: string // is equal to the conditional of query "WHERE 'attribute_name' < 'value1'
180+
lte:string // is equal to the conditional of query "WHERE 'attribute_name' <= 'value1'
181+
regexp: string // is equal to the conditional of query "WHERE 'attribute_name' ~* value1
182+
some: string // is equal to the conditional of query "WHERE 'attribute_name' && [value1]
168183
}
169184
```
170185

0 commit comments

Comments
 (0)