Nuclear Pumpkin Mayhem

    grpc-typeorm-infrastructure

    1.0.16 • Public • Published

    gRPC Typeorm Infrastructure

    License Current Version npm

    This library provides a simple wrapper around TypeORM functions in order to provide consistent and predictable error messages, it uses gRPC Boom to generate the gRPC-friendly error objects.

    It is assumed that you are using the grpc library.

    Installation

    npm install grpc-typeorm-infrastructure --save
    

    Install the grpc library:

    npm install grpc --save
    

    Usage

    Step 1:

    Create a TypeORM entity:

    import {
        Column,
        CreateDateColumn,
        Entity,
        PrimaryGeneratedColumn,
        UpdateDateColumn
    } from 'typeorm';
     
    @Entity({ name: 'hero' })
    export default class Hero {
        @PrimaryGeneratedColumn()
        public id?: number;
     
        @Column({ name: 'name', length: 500 })
        public name: string;
     
        @Column({ name: 'identity', length: 500 })
        public identity: string;
     
        @Column({ name: 'hometown', length: 500 })
        public hometown: string;
     
        @Column({ name: 'age' })
        public age: number;
     
        @CreateDateColumn({ name: 'created_at', type: 'timestamp with time zone' })
        public createdAt?: Date;
     
        @UpdateDateColumn({ name: 'updated_at', type: 'timestamp with time zone' })
        public updatedAt?: Date;
     
        @Column({ name: 'deleted_at', nullable: true, type: 'timestamp with time zone' })
        public deletedAt?: Date;
    }

    Step 2:

    Create a repository for the entity above:

    import BaseRepository from 'grpc-typeorm-infrastructure';
     
    import Hero from '@entities/hero.entity';
     
    export default class HeroRepository extends BaseRepository<Hero> {
        constructor() {
            super(Hero.name);
        }
    }

    Step 3:

    Create a service for the entity above:

    import { BaseService } from 'grpc-typeorm-infrastructure';
     
    import Hero from '@entities/hero.entity';
     
    export default class HeroService extends BaseService<Hero> {
        constructor(heroRepository: HeroRepository) {
            super(heroRepository);
        }
     
        public preSaveHook(hero: Hero): void {
            // Executed before the save repository call
            delete hero.id;
        }
     
        public preUpdateHook(hero: Hero) {
            // Executed before the update repository call
            delete hero.updatedAt;
        }
     
        public preDeleteHook(hero: Hero) {
            // Executed before the delete repository call
            hero.deletedAt = new Date();
        }
     
        public preResultHook(hero: Hero) {
            // Executed before the result is returned
            delete hero.deletedAt;
        }
    }

    Repository API

    The base repository will give you access to the following methods:

    getAll(options?: FindManyOptions<T>)Promise<T[]>;
    findManyByFilter(optionsFindManyOptions<T>)Promise<T[]>;
    findOneById(idnumber)Promise<T>;
    findOneByIdWithOptions(idnumber, options?: FindOneOptions<T>)Promise<T>;
    findManyById(idListnumber[], options?: FindOneOptions<T>)Promise<T[]>;
    findOneByFilter(optionsFindOneOptions<T>)Promise<T>;
    save(recordT, options?: SaveOptions)Promise<T>;
    saveAll(
        recordsT[],
        options?: SaveOptions,
        resolveRelations?: boolean
    )Promise<T[]>;
    updateOneById(idnumber, recordT)Promise<T>;
    deleteOneById(
        idnumber,
        findOptions?: FindOneOptions<T>,
        deleteOptions?: RemoveOptions
    )Promise<T>;
    deleteManyById(idListnumber[], deleteOptions?: RemoveOptions)Promise<T>;
    findOneWithQueryBuilder(
        optionsISearchQueryBuilderOptions
    )Promise<T | undefined>;
    findManyWithQueryBuilder(optionsISearchQueryBuilderOptions)Promise<T[]>;
    delete(record: T, options?: RemoveOptions);

    Service API

    The base service will give you access to the following methods:

    preSaveHook(entityT)void;
    preUpdateHook(entityT)void;
    preDeleteHook(entityT)void;
    preResultHook(entityT)void;
    validId(idnumber)boolean;
    isValid(entityT)Promise<boolean>;
    findAll()Promise<T[]>;
    findAllByFilter(filterFindManyOptions<T>)Promise<T[]>;
    findOneById(idnumber)Promise<T>;
    findOneByFilter(filterFindOneOptions<T>)Promise<T>;
    findOneWithQueryBuilder(optionsISearchQueryBuilderOptions)Promise<T>;
    findManyWithQueryBuilder(optionsISearchQueryBuilderOptions)Promise<T[]>;
    search(limitnumber, searchTermsSearchTerm[]);
    save(entityT)Promise<T>;
    saveAll(entitiesT[])Promise<T[]>;
    update(entityT, idnumber)Promise<T>;
    updateAll(entitiesT[])Promise<T[]>;
    getSearchFilter(
        limitnumber,
        searchTermsSearchTerm[]
    )ISearchQueryBuilderOptions;
    delete(id: number): Promise<T>;
    softDelete(id: number)Promise<T>;

    Install

    npm i grpc-typeorm-infrastructure

    DownloadsWeekly Downloads

    2

    Version

    1.0.16

    License

    MIT

    Unpacked Size

    34.5 kB

    Total Files

    10

    Last publish

    Collaborators

    • nicolaspearson