npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@anatoliacms/anatolia-common

v1.0.1

Published

An abstraction layer to create core controllers for Anatolia CMS

Downloads

7

Readme

nest-crud

An abstraction layer that implements TypeORM and Repository pattern, to create core controllers for Nestjs

Nowadays we're spending too much time to create core controllers called CRUD controller. To prevent this, we can use nest-crud abstraction layer.

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;

//docker-compose.yml

docker-compose docker-compose --env-file .env.dev up
//package.json

npm 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.

//cat.entity.ts

import 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
}
//cat.dto.ts

import 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<T, D> 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.

// cat.mapper.ts
import AbstractMapper from "nest-crud-abstraction/dist/mapper/AbstractMapper";

@Injectable()
export default class CatMapper extends AbstractMapper<CatEntity, CatDto> {
    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.

//cat.service.ts

import AbstractCrudService from 'nest-crud-abstraction/dist/service/AbstractCrudService';
import AbstractMapper from "nest-crud-abstraction/dist/mapper/AbstractMapper";

@Injectable()
export class CatService extends AbstractCrudService<CatEntity, CatDto> {
    constructor(
        @InjectRepository(CatEntity) private usersRepository: Repository<CatEntity>,
        private readonly catMapper: CatMapper,
    ) {
        super();
    }

    protected getRepository(): any {
        return this.usersRepository;
    }

    protected getMapper(): AbstractMapper<CatEntity, CatDto> {
        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<T,D>

getService()

This method will pass your service component to AbstractController to create our core controllers.

@Controller('/cats')
export class CatController extends AbstractController<CatEntity, CatDto> {
    constructor(
        private readonly appService: CatService
    ) {
        super();
    }

    protected getService(): AbstractCrudService<CatEntity, CatDto> {
        return this.appService;
    }
}

Conclusion

If you run your application you should see below output that describes our mapped endpoints.

[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