import assert from 'assert' import {EntityManager, FindOptionsOrder, FindOptionsRelations, FindOptionsWhere} from 'typeorm' import {EntityTarget} from 'typeorm/common/EntityTarget' import {ColumnMetadata} from 'typeorm/metadata/ColumnMetadata' import {ChangeTracker} from './hot' export interface EntityClass { new (): T } export interface Entity { id: string } /** * Defines a special criteria to find specific entity. */ export interface FindOneOptions { /** * Adds a comment with the supplied string in the generated query. This is * helpful for debugging purposes, such as finding a specific query in the * database server's logs, or for categorization using an APM product. */ comment?: string /** * Simple condition that should be applied to match entities. */ where?: FindOptionsWhere[] | FindOptionsWhere /** * Indicates what relations of entity should be loaded (simplified left join form). */ relations?: FindOptionsRelations; /** * Order, in which entities should be ordered. */ order?: FindOptionsOrder } export interface FindManyOptions extends FindOneOptions { /** * Offset (paginated) where from entities should be taken. */ skip?: number; /** * Limit (paginated) - max number of entities should be taken. */ take?: number; } /** * Restricted version of TypeORM entity manager for squid data handlers. */ export class Store { constructor(private em: () => EntityManager, private changes?: ChangeTracker) {} /** * Alias for {@link Store.upsert} */ save(entity: E): Promise save(entities: E[]): Promise save(e: E | E[]): Promise { if (Array.isArray(e)) { // please the compiler return this.upsert(e) } else { return this.upsert(e) } } /** * Upserts a given entity or entities into the database. * * It always executes a primitive operation without cascades, relations, etc. */ upsert(entity: E): Promise upsert(entities: E[]): Promise async upsert(e: E | E[]): Promise { if (Array.isArray(e)) { if (e.length == 0) return let entityClass = e[0].constructor as EntityClass for (let i = 1; i < e.length; i++) { assert(entityClass === e[i].constructor, 'mass saving allowed only for entities of the same class') } await this.changes?.trackUpsert(entityClass, e) await this.saveMany(entityClass, e) } else { let entityClass = e.constructor as EntityClass await this.changes?.trackUpsert(entityClass, [e]) await this.em().upsert(entityClass, e as any, ['id']) } } private async saveMany(entityClass: EntityClass, entities: any[]): Promise { assert(entities.length > 0) let em = this.em() let metadata = em.connection.getMetadata(entityClass) let fk = metadata.columns.filter(c => c.relationMetadata) if (fk.length == 0) return this.upsertMany(em, entityClass, entities) let currentSignature = this.getFkSignature(fk, entities[0]) let batch = [] for (let e of entities) { let sig = this.getFkSignature(fk, e) if (sig === currentSignature) { batch.push(e) } else { await this.upsertMany(em, entityClass, batch) currentSignature = sig batch = [e] } } if (batch.length) { await this.upsertMany(em, entityClass, batch) } } private getFkSignature(fk: ColumnMetadata[], entity: any): bigint { let sig = 0n for (let i = 0; i < fk.length; i++) { let bit = fk[i].getEntityValue(entity) === undefined ? 0n : 1n sig |= (bit << BigInt(i)) } return sig } private async upsertMany(em: EntityManager, entityClass: EntityClass, entities: any[]): Promise { for (let b of splitIntoBatches(entities, 1000)) { await em.upsert(entityClass, b as any, ['id']) } } /** * Inserts a given entity or entities into the database. * Does not check if the entity(s) exist in the database and will fail if a duplicate is inserted. * * Executes a primitive INSERT operation without cascades, relations, etc. */ insert(entity: E): Promise insert(entities: E[]): Promise async insert(e: E | E[]): Promise { if (Array.isArray(e)) { if (e.length == 0) return let entityClass = e[0].constructor as EntityClass for (let i = 1; i < e.length; i++) { assert(entityClass === e[i].constructor, 'mass saving allowed only for entities of the same class') } await this.changes?.trackInsert(entityClass, e) for (let b of splitIntoBatches(e, 1000)) { await this.em().insert(entityClass, b as any) } } else { let entityClass = e.constructor as EntityClass await this.changes?.trackInsert(entityClass, [e]) await this.em().insert(entityClass, e as any) } } /** * Deletes a given entity or entities from the database. * * Unlike {@link EntityManager.remove} executes a primitive DELETE query without cascades, relations, etc. */ remove(entity: E): Promise remove(entities: E[]): Promise remove(entityClass: EntityClass, id: string | string[]): Promise async remove(e: E | E[] | EntityClass, id?: string | string[]): Promise{ if (id == null) { if (Array.isArray(e)) { if (e.length == 0) return let entityClass = e[0].constructor as EntityClass for (let i = 1; i < e.length; i++) { assert(entityClass === e[i].constructor, 'mass deletion allowed only for entities of the same class') } let ids = e.map(i => i.id) await this.changes?.trackDelete(entityClass, ids) await this.em().delete(entityClass, ids) } else { let entity = e as E let entityClass = entity.constructor as EntityClass await this.changes?.trackDelete(entityClass, [entity.id]) await this.em().delete(entityClass, entity.id) } } else { let entityClass = e as EntityClass await this.changes?.trackDelete(entityClass, Array.isArray(id) ? id : [id]) await this.em().delete(entityClass, id) } } async count(entityClass: EntityClass, options?: FindManyOptions): Promise { return this.em().count(entityClass, options) } async countBy(entityClass: EntityClass, where: FindOptionsWhere | FindOptionsWhere[]): Promise { return this.em().countBy(entityClass, where) } async find(entityClass: EntityClass, options?: FindManyOptions): Promise { return this.em().find(entityClass, options) } async findBy(entityClass: EntityClass, where: FindOptionsWhere | FindOptionsWhere[]): Promise { return this.em().findBy(entityClass, where) } async findOne(entityClass: EntityClass, options: FindOneOptions): Promise { return this.em().findOne(entityClass, options).then(noNull) } async findOneBy(entityClass: EntityClass, where: FindOptionsWhere | FindOptionsWhere[]): Promise { return this.em().findOneBy(entityClass, where).then(noNull) } async findOneOrFail(entityClass: EntityTarget, options: FindOneOptions): Promise { return this.em().findOneOrFail(entityClass, options) } async findOneByOrFail(entityClass: EntityTarget, where: FindOptionsWhere | FindOptionsWhere[]): Promise { return this.em().findOneByOrFail(entityClass, where) } get(entityClass: EntityClass, optionsOrId: FindOneOptions | string): Promise { if (typeof optionsOrId == 'string') { return this.findOneBy(entityClass, {id: optionsOrId} as any) } else { return this.findOne(entityClass, optionsOrId) } } } function* splitIntoBatches(list: T[], maxBatchSize: number): Generator { if (list.length <= maxBatchSize) { yield list } else { let offset = 0 while (list.length - offset > maxBatchSize) { yield list.slice(offset, offset + maxBatchSize) offset += maxBatchSize } yield list.slice(offset) } } function noNull(val: null | undefined | T): T | undefined { return val == null ? undefined : val }