import { IdType, NonNullCreateEntity, CompiledSchema, IDbPlugin, HashType, toMap } from "@agrejus/db-framework-core"; import { ChangeTrackingBase } from "./ChangeTrackingBase"; import { IChangeTracker } from '../types'; export class IdentityKeyChangeTrackingBase extends ChangeTrackingBase implements IChangeTracker { protected additions: NonNullCreateEntity[] = []; constructor(schema: CompiledSchema, dbPlugin: IDbPlugin) { super(schema, dbPlugin); } protected override get additionsCount() { return this.additions.length; } protected override setAddition(enriched: NonNullCreateEntity): void { this.additions.push(enriched); } protected override getPreparedAdditions() { return this.additions.map(w => this.schema.prepare(w as any)) } protected override clearAdditions() { this.additions = []; } override saveChanges(done: (result: number, error?: any) => void): void { const hashedAdds = toMap(this.additions, w => this.schema.hash(w, HashType.Object)); this.bulkOperations(entity => { const hash = this.schema.hash(entity as any, HashType.Object); return hashedAdds.get(hash); }, done) } }