Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gunerkaanalkim/nest-crud
An abstraction layer to create core controllers for Nestjs
https://github.com/gunerkaanalkim/nest-crud
Last synced: about 1 month ago
JSON representation
An abstraction layer to create core controllers for Nestjs
- Host: GitHub
- URL: https://github.com/gunerkaanalkim/nest-crud
- Owner: gunerkaanalkim
- License: mit
- Created: 2023-11-03T15:42:36.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-09T19:59:22.000Z (about 1 year ago)
- Last Synced: 2024-10-01T15:55:02.658Z (about 2 months ago)
- Language: TypeScript
- Size: 252 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nest-crud
An abstraction layer that implements TypeORM and Repository pattern, to create core controllers for NestjsNowadays we're spending too much time to create core controllers called CRUD controller.
To prevent this, we can use **nest-crud** abstraction layer.```bash
npm install nest-crud-abstraction
```# Sample Project
To see how to use `nest-crud-abstraction` package, please refer to the sample folder in the root of the project. Then run following;```bash
//docker-compose.ymldocker-compose docker-compose --env-file .env.dev up
``````bash
//package.jsonnpm run start:dev
```# At a glance
## 1. Entities & Data Transfer Objects
First we'll need an entity and a DTO like below.
We'll use that entity clas for database related developments and DTO class will be in charge of request&response body.
Each entity must implement `AbstractEntity` class and each DTO must implement `AbstractDTO` class.
```typescript
//cat.entity.tsimport AbstractEntity from 'nest-crud-abstraction/dist/model/AbstractEntity';
import {Column, Entity} from 'typeorm';@Entity({name: 'cats'})
export default class CatEntity extends AbstractEntity {
@Column({name: 'breed'})
private _breed: string;@Column({name: 'color'})
private _color: string;// Getters & setters
}
``````typescript
//cat.dto.tsimport AbstractDTO from 'nest-crud-abstraction/dist/payload/AbstractDTO';
export default class CatDto extends AbstractDTO {
private _breed: string;
private _color: string;// Getters & setters
}```
# 2. Services for Business Logic
Each controller we develop will depend on a service class.
This classes will be in charge of executing all business logic as usual in `Nestjs`.Each service must implement generic `AbstractCrudService` class.
First type have to be an entity and second one is a DTO.
nest-crud service abstraction has two abstract methods called `getRepository` & `getMapper`.
##### getRepository()
nest-crud uses TypeORM repository pattern under the hood.That's why you will need to create a repository as in constructor below and will to pass it to AbstractService layer by overriding this method.
##### getMapper()
We wouldn't want to pass entity objects on rest layer, would we ?If we agreed on this, we will need to create a mapper object that extends `AbstractMapper`.
```typescript
// cat.mapper.ts
import AbstractMapper from "nest-crud-abstraction/dist/mapper/AbstractMapper";@Injectable()
export default class CatMapper extends AbstractMapper {
toDTO(entity: CatEntity): CatDto {
const sampleDTO = new CatDto();sampleDTO.breed = entity.breed;
sampleDTO.color = entity.color;return sampleDTO;
}toEntity(dto: CatDto): CatEntity {
const sampleEntity = new CatEntity();sampleEntity.breed = dto.breed;
sampleEntity.color = dto.color;return sampleEntity;
}
}
```The last thing that we implement is CatService as below.
```typescript
//cat.service.tsimport AbstractCrudService from 'nest-crud-abstraction/dist/service/AbstractCrudService';
import AbstractMapper from "nest-crud-abstraction/dist/mapper/AbstractMapper";@Injectable()
export class CatService extends AbstractCrudService {
constructor(
@InjectRepository(CatEntity) private usersRepository: Repository,
private readonly catMapper: CatMapper,
) {
super();
}protected getRepository(): any {
return this.usersRepository;
}protected getMapper(): AbstractMapper {
return this.catMapper;
}
// Your custom logic apart from core controllers.
}```
If everything well up to now, we can create our core controllers.
# Core Controllers
You can create your controller structure like Nest-style, there is only one difference. This controller should have implement `AbstractController`
##### getService()
This method will pass your service component to AbstractController to create our core controllers.
```typescript
@Controller('/cats')
export class CatController extends AbstractController {
constructor(
private readonly appService: CatService
) {
super();
}protected getService(): AbstractCrudService {
return this.appService;
}
}
```# Conclusion
If you run your application you should see below output that describes our mapped endpoints.
```bash
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [NestFactory] Starting Nest application...
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [InstanceLoader] AppModule dependencies initialized +49ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [InstanceLoader] TypeOrmCoreModule dependencies initialized +71ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [InstanceLoader] TypeOrmModule dependencies initialized +0ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [InstanceLoader] CatsModule dependencies initialized +0ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [RoutesResolver] CatController {/cats}: +7ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [RouterExplorer] Mapped {/cats, GET} route +1ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [RouterExplorer] Mapped {/cats/:id, GET} route +1ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [RouterExplorer] Mapped {/cats/:id, GET} route +0ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [RouterExplorer] Mapped {/cats/:id, DELETE} route +0ms
[Nest] 52991 - 11/06/2023, 5:42:00 PM LOG [NestApplication] Nest application successfully started +1ms
```