import { nameof } from "ts-simple-nameof" ; import { EntitySchema , getConnectionManager , Repository , SelectQueryBuilder } from "typeorm" ; import { IQuery } from "../query/interfaces/IQuery" ; import { Query } from "../query/Query" ; import { EntityBase } from "../types/EntityBase" ; import { EntityConstructor } from "../types/EntityConstructor" ; import { RepositoryOptions } from "../types/RepositoryOptions" ; import { ILinqRepository } from "./interfaces/ILinqRepository" ; /**
 * Base repository operations for TypeORM entities.
 */ export class LinqRepository < T extends EntityBase > implements ILinqRepository < T > { protected readonly _repository : Repository < T > ; private readonly _autoGenerateId : boolean ; private readonly _primaryKeyName : string ; param entityType param options public constructor ( entityType , options ? ) { let autoGenerateId = true ; let connectionName ; let primaryKeyName = "id" ; if ( options ) { if ( typeof ( options . autoGenerateId ) === "boolean" ) { autoGenerateId = options . autoGenerateId ; } if ( options . connectionName ) { connectionName = options . connectionName ; } if ( options . primaryKey ) { primaryKeyName = nameof ( options . primaryKey ) ; } } this . _repository = getConnectionManager ( ) . get ( connectionName ) . getRepository < T > ( entityType ) ; this . _autoGenerateId = autoGenerateId ; this . _primaryKeyName = primaryKeyName ; } public get typeormRepository ( ) : Repository < T > { return this . _repository ; } public async create < E extends T | T [ ] > ( entities ) { if ( this . _autoGenerateId ) { if ( entities instanceof Array ) { for ( const entity of ( < T [ ] > entities ) ) { ( < Record < string , any > > entity ) [ this . _primaryKeyName ] = undefined ; } } else { ( < Record < string , any > > entities ) [ this . _primaryKeyName ] = undefined ; } } return this . upsert ( entities ) ; } public createQueryBuilder ( alias ) { return this . _repository . createQueryBuilder ( alias ) ; } public async delete ( entities ) { if ( typeof ( entities ) === "number" || typeof ( entities ) === "string" ) { await this . _repository . delete ( entities ) ; } else { await this . _repository . remove ( < any > entities ) ; } return true ; } public getAll ( ) { const queryBuilder = this . createQueryBuilder ( "entity" ) ; const query = new Query ( queryBuilder , queryBuilder . getMany ) ; return query ; } public getById ( id ) { const alias = "entity" ; let queryBuilder = this . createQueryBuilder ( alias ) ; queryBuilder = queryBuilder . where ( `${ alias }.${ this . _primaryKeyName } = :id` , { id } ) ; const query = new Query ( queryBuilder , queryBuilder . getOne ) ; return query ; } public getOne ( ) { const queryBuilder = this . createQueryBuilder ( "entity" ) ; const query = new Query ( queryBuilder , queryBuilder . getOne ) ; return query ; } public async update < E extends T | T [ ] > ( entities ) { return this . upsert ( entities ) ; } public async upsert < E extends T | T [ ] > ( entities ) { return < Promise < E > > this . _repository . save ( < any > entities ) ; } }