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

typeorm-infrastructure

v1.0.16

Published

Simple infrastructure implementation for TypeORM.

Downloads

25

Readme

Typeorm Infrastructure

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

Installation

npm install typeorm-infrastructure --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(options: FindManyOptions<T>): Promise<T[]>;
findOneById(id: number): Promise<T>;
findOneByIdWithOptions(id: number, options?: FindOneOptions<T>): Promise<T>;
findManyById(idList: number[], options?: FindOneOptions<T>): Promise<T[]>;
findOneByFilter(options: FindOneOptions<T>): Promise<T>;
save(record: T, options?: SaveOptions): Promise<T>;
saveAll(
	records: T[],
	options?: SaveOptions,
	resolveRelations?: boolean
): Promise<T[]>;
updateOneById(id: number, record: T): Promise<T>;
deleteOneById(
	id: number,
	findOptions?: FindOneOptions<T>,
	deleteOptions?: RemoveOptions
): Promise<T>;
deleteManyById(idList: number[], deleteOptions?: RemoveOptions): Promise<T>;
findOneWithQueryBuilder(
	options: ISearchQueryBuilderOptions
): Promise<T | undefined>;
findManyWithQueryBuilder(options: ISearchQueryBuilderOptions): Promise<T[]>;
delete(record: T, options?: RemoveOptions);

Service API

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

preSaveHook(entity: T): void;
preSaveHook(entity: T): void;
preUpdateHook(entity: T): void;
preDeleteHook(entity: T): void;
preResultHook(entity: T): void;
validId(id: number): boolean;
isValid(entity: T): Promise<boolean>;
findAll(): Promise<T[]>;
findAllByFilter(filter: FindManyOptions<T>): Promise<T[]>;
findOneById(id: number): Promise<T>;
findOneByFilter(filter: FindOneOptions<T>): Promise<T>;
findOneWithQueryBuilder(options: ISearchQueryBuilderOptions): Promise<T>;
findManyWithQueryBuilder(options: ISearchQueryBuilderOptions): Promise<T[]>;
search(limit: number, searchTerms: SearchTerm[]);
save(entity: T): Promise<T>;
saveAll(entities: T[]): Promise<T[]>;
update(entity: T, id: number): Promise<T>;
updateAll(entities: T[]): Promise<T[]>;
getSearchFilter(
	limit: number,
	searchTerms: SearchTerm[]
): ISearchQueryBuilderOptions;
delete(id: number): Promise<T>;
softDelete(id: number): Promise<T>;