This library has some useful utilities for typeorm, entities, repositories, useful subscribers, interceptors.
npm install @softkit/typeormThis library has some useful utilities for typeorm, entities, repositories, useful subscribers, interceptors.
It can be useful outside Softkit ecosystem
- 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
``bash`
yarn add @softkit/typeorm
`typescript
import { DbConfig } from '@softkit/typeorm';
export class RootConfig {
@Type(() => DbConfig)
@ValidateNested()
public readonly db!: DbConfig;
}
`
`yaml`
db:
type: 'postgres'
host: 'localhost'
port: 5432
username: postgres
password: postgres
database: local-db
synchronize: false
dropSchema: false
keepConnectionAlive: true
logging: false
ssl: false
`typescript
import { setupTypeormModule } from '@softkit/typeorm';
import * as Entities from './database/entities';
@Module({
imports: [TypeOrmModule.forFeature(Object.values(Entities)), setupTypeormModule()],
})
class YourAppModule {}
`
- EntityHelper - with entity data for handling polymorphismBaseEntityHelper
- - with id, createdAt, updatedAt, deletedAt, version fieldsBaseTenantEntityHelper
- - 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 fieldsid
and also override field decide what you want to use uuid or number or some other type
`typescript
class SampleEntity extends BaseEntityHelper {
@PrimaryGeneratedColumn('uuid')
id!: number;
@Column()
name!: string;
}
`
- BaseRepository - extends default typeorm repository, and fixes some type confuses in the default typeorm repositoryTenantBaseRepository
- - 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
`typescript`
@Injectable()
export class SampleRepository extends BaseRepository
constructor(
@InjectDataSource()
ds: DataSource,
) {
super(SampleEntity, ds);
}
}
- Tenant base repository (_it require to pass a ClsService_)
`typescript`
@Injectable()
export class TenantUserRepository extends BaseTenantRepository
constructor(
@InjectDataSource()
ds: DataSource,
clsService: ClsService
) {
super(TenantUserEntity, ds, clsService);
}
}
- ClsPresetSubscriber - responsible for populating any fields from ClsStore to entity beforeInsert and beforeUpdate
It configurable via ClsPreset decorator
`typescript`
export class BaseTenantEntityHelper extends BaseEntityHelper {
@ClsPreset
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.
- PostgresDbQueryFailedErrorFilter` this filter is responsible for handling low level postgres QueryFailedError, it's mapping codes to the appropriate RFC7807 errors