Skip to content

Commit

Permalink
feat(json-api-nestjs): new-version 3.x.x
Browse files Browse the repository at this point in the history
support TypeOrm 3.x.x and NestJS 9

BREAKING CHANGE: change init custom controller
change init config
  • Loading branch information
Alexander Kharkovey committed Oct 7, 2022
1 parent b8f032c commit f45d811
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
## Description

<p>
This is the plugin that works upon TypeOrm library as a main database abstraction layer tool. Module automaticly generates API according to JSON API specificaton from the database structure (TypeORM entities). It support such features as requests validation based on database fields types, request filtering, endpoints exdending, 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.
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.
</p>

## Installation
Expand Down
95 changes: 90 additions & 5 deletions libs/json-api-nestjs/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,96 @@

# json-api-nestjs

This library was generated with [Nx](https://nx.dev).
## Installation

```bash
$ npm install json-api-nestjs
```

## Example

Once the installation process is complete, we can import the **JsonApiModule** into the root **AppModule**.

```typescript
import { Module } from '@nestjs/common';
import { JsonApiModule } from 'json-api-nestjs';
import { Users } from 'database';

@Module({
imports: [
JsonApiModule.forRoot({
entities: [Users]
}),
],
})
export class AppModule {}
```
After this, you have prepare crude with ready to use end point:


- GET /users
- POST /users
- GET /users/:id
- PATCH /users/:id
- DELETE /users/:id
- GET /users/{id}/relationships/{relName}
- POST /users/{id}/relationships/{relName}
- PATCH /users/{id}/relationships/{relName}
- DELETE /users/{id}/relationships/{relName}

## Configuration params

## Building
The following interface is using for the configuration:
```typescript
export interface ModuleOptions {
entities: Entity[]; // List of typeOrm Entity
controllers?: NestController[]; // List of controller, if you need extend default present
connectionName?: string; // Type orm connection name: "default" is default name
providers?: NestProvider[]; // List of addition provider for useing in custom controller
options?: {
requiredSelectField?: boolean; // Need list of select field in get endpoint, try is default
debug?: boolean; // Debug info in result object
};
}
```
You can extend default controller:
```typescript
import { Get, Param, Inject, BadRequestException } from '@nestjs/common';

import { Users } from 'database';
import {
JsonApi,
excludeMethod,
JsonBaseController,
InjectService,
JsonApiService,
QueryParams,
} from 'json-api-nestjs';
import { ExampleService } from '../../service/example/example.service';

@JsonApi(Users, {
allowMethod: excludeMethod(['deleteRelationship']),
requiredSelectField: true,
})
export class ExtendUserController extends JsonBaseController<Users> {
@InjectService() public service: JsonApiService<Users>;
@Inject(ExampleService) protected exampleService: ExampleService;

public override getAll(query: QueryParams<Users>) {
if (!this.exampleService.someCheck(query)) {
throw new BadRequestException({});
}
return this.service.getAll({ query });
}

@Get(':id/example')
testOne(@Param('id') id: string): string {
return this.exampleService.testMethode(id);
}
}
```

Run `nx build json-api-nestjs` to build the library.
## Swagger UI

## Running unit tests
For using swagger, you should only add [@nestjs/swagger](https://docs.nestjs.com/openapi/introduction)

Run `nx test json-api-nestjs` to execute the unit tests via [Jest](https://jestjs.io).

0 comments on commit f45d811

Please sign in to comment.