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

@softkit/typeorm

v0.6.0

Published

This library has some useful utilities for typeorm, entities, repositories, useful subscribers, interceptors.

Downloads

9

Readme

Typeorm Utilities

This library has some useful utilities for typeorm, entities, repositories, useful subscribers, interceptors.

It can be useful outside Softkit ecosystem

Features

  • It provides a base entity that has some useful fields like createdAt, updatedAt, deletedAt, version, id
  • It overrides the default typeorm repository, and fixes some type confuses in the default typeorm repository
  • It provides a tenant base repository, that make all requests based on tenant id that must be present in ClsStore
  • Useful subscribers for auto populate any field from ClsStore, like tenantId, userId
  • Optimistic lock handler, that will throw an error if you try to update an entity that was updated by someone else
  • Common interceptors for DB errors, that transforms DB errors to RFC7807 errors
  • Simplify transaction management with @Transaction decorator

Installation

yarn add @softkit/typeorm

Usage

Add default configuration in your root config class

import { DbConfig } from '@softkit/typeorm';

export class RootConfig {
  @Type(() => DbConfig)
  @ValidateNested()
  public readonly db!: DbConfig;
}

.env.yaml file

db:
  type: 'postgres'
  host: 'localhost'
  port: 5432
  username: postgres
  password: postgres
  database: local-db
  synchronize: false
  dropSchema: false
  keepConnectionAlive: true
  logging: false
  ssl: false

Add setup and entities to your main app module

import { setupTypeormModule } from '@softkit/typeorm';
import * as Entities from './database/entities';

@Module({
  imports: [TypeOrmModule.forFeature(Object.values(Entities)), setupTypeormModule()],
})
class YourAppModule {}

Entities to extend from

  • EntityHelper - with entity data for handling polymorphism
  • BaseEntityHelper - with id, createdAt, updatedAt, deletedAt, version fields
  • BaseTenantEntityHelper - useful for entities that belongs to specific tenant. It has tenantId field, that is auto populated for search operations on repository level

Note:

in your entity you can extend from BaseEntityHelper or BaseTenantEntityHelper or EntityHelper and add your fields and also override id field decide what you want to use uuid or number or some other type

class SampleEntity extends BaseEntityHelper {
  @PrimaryGeneratedColumn('uuid')
  id!: number;

  @Column()
  name!: string;
}

Repositories to extend from

  • BaseRepository - extends default typeorm repository, and fixes some type confuses in the default typeorm repository
  • TenantBaseRepository - extends BaseRepository and adds tenantId to all requests, that is taken from ClsStore

Note:

If your entity is Tenant Base but in some cases you want to get all data, you can just create another repository for this entity that extends BaseRepository. That's ok to have multiple repositories for one entity.

Examples

  • Plain repository
@Injectable()
export class SampleRepository extends BaseRepository<SampleEntity> {
  constructor(
    @InjectDataSource()
    ds: DataSource,
  ) {
    super(SampleEntity, ds);
  }
}
  • Tenant base repository (it require to pass a ClsService)
@Injectable()
export class TenantUserRepository extends BaseTenantRepository<TenantUserEntity> {
  constructor(
    @InjectDataSource()
    ds: DataSource,
    clsService: ClsService<TenantClsStore>,
  ) {
    super(TenantUserEntity, ds, clsService);
  }
}

Subscribers

  • ClsPresetSubscriber - responsible for populating any fields from ClsStore to entity beforeInsert and beforeUpdate

It configurable via ClsPreset decorator

export class BaseTenantEntityHelper extends BaseEntityHelper {
  @ClsPreset<TenantClsStore>({
    clsPropertyFieldName: 'tenantId',
  })
  @Column({ nullable: false })
  tenantId!: string;
}
  • OptimisticLockSubscriber - responsible for handling optimistic lock errors. It's important to handle optimistic locks properly for many reasons. For example, if you have a frontend app, and some resource where users can collaborate and override each other, it's important to handle optimistic lock errors properly. Even if your entity can be changed by one user at a time, it's important to handle optimistic lock errors properly, because it can be changed by another service or by this user in another tab, and he just forgot about it.

Filters

  • PostgresDbQueryFailedErrorFilter this filter is responsible for handling low level postgres QueryFailedError, it's mapping codes to the appropriate RFC7807 errors