import { TsDatastoreOrmError } from "./errors/TsDatastoreOrmError" ; import { IEntityCompositeIndexList , IEntityFieldIndex , IEntityFieldMetaOptions , IEntityMetaOptions } from "./types" ; class DecoratorMeta { public entityMetaMap = new Map < object , IEntityMetaOptions > ( ) ; public entityFieldMetaListMap = new Map < object , Map < string , IEntityFieldMetaOptions > > ( ) ; public compositeIndexListMap = new Map < object , IEntityCompositeIndexList > ( ) ; public entityHookOfBeforeInsertMap = new Map < object , string | symbol > ( ) ; public entityHookOfBeforeUpsertMap = new Map < object , string | symbol > ( ) ; public entityHookOfBeforeUpdateMap = new Map < object , string | symbol > ( ) ; public entityHookOfBeforeDeleteMap = new Map < object , string | symbol > ( ) ; public entityHookOfAfterLoadMap = new Map < object , string | symbol > ( ) ; public addEntityMeta ( classObject , options ) { this . entityMetaMap . set ( classObject , options ) ; } public hasEntityMeta ( classObject ) { return this . entityMetaMap . has ( classObject ) ; } public getEntityMeta ( classObject ) { const entityMeta = this . entityMetaMap . get ( classObject ) ; if ( ! entityMeta ) { throw new TsDatastoreOrmError ( `(${ ( classObject as any ) . name }) Entity must define with class decorator @Entity().` ) ; } return entityMeta ; } public addEntityCompositeIndex ( classObject , fields , hasAncestor ) { if ( ! this . compositeIndexListMap . has ( classObject ) ) { this . compositeIndexListMap . set ( classObject , [ ] ) ; } const compositeIndexList = this . compositeIndexListMap . get ( classObject ) ! ; compositeIndexList . push ( { fields , hasAncestor , } ) ; } public getEntityCompositeIndexList ( classObject ) { const entityCompositeIndexes = this . compositeIndexListMap . get ( classObject ) ; return entityCompositeIndexes || [ ] ; } public addEntityFieldMeta ( classObject , fieldName , options ) { let map = this . entityFieldMetaListMap . get ( classObject ) ; if ( ! map ) { map = new Map ( ) ; this . entityFieldMetaListMap . set ( classObject , map ) ; } map . set ( fieldName , options ) ; } public hasEntityFieldMetaList ( classObject ) { return this . entityFieldMetaListMap . has ( classObject ) ; } public hasEntityFieldMeta ( classObject , fieldName ) { const entityFieldMeta = this . entityFieldMetaListMap . get ( classObject ) ; return entityFieldMeta ? entityFieldMeta . has ( fieldName ) : false ; } public getEntityFieldMetaList ( classObject ) { return this . entityFieldMetaListMap . get ( classObject ) ! ; } public getEntityFieldNames ( classObject ) { const map = this . entityFieldMetaListMap . get ( classObject ) ! ; return Array . from ( map . keys ( ) ) ; } public addHookOfBeforeInsert ( classObject , propertyKey ) { this . entityHookOfBeforeInsertMap . set ( classObject , propertyKey ) ; } public getHookOfBeforeInsert ( classObject ) { return this . entityHookOfBeforeInsertMap . get ( classObject ) ; } public addHookOfBeforeUpsert ( classObject , propertyKey ) { this . entityHookOfBeforeUpsertMap . set ( classObject , propertyKey ) ; } public getHookOfBeforeUpsert ( classObject ) { return this . entityHookOfBeforeUpsertMap . get ( classObject ) ; } public addHookOfBeforeUpdate ( classObject , propertyKey ) { this . entityHookOfBeforeUpdateMap . set ( classObject , propertyKey ) ; } public getHookOfBeforeUpdate ( classObject ) { return this . entityHookOfBeforeUpdateMap . get ( classObject ) ; } public addHookOfBeforeDelete ( classObject , propertyKey ) { this . entityHookOfBeforeDeleteMap . set ( classObject , propertyKey ) ; } public getHookOfBeforeDelete ( classObject ) { return this . entityHookOfBeforeDeleteMap . get ( classObject ) ; } public addHookOfAfterLoad ( classObject , propertyKey ) { this . entityHookOfAfterLoadMap . set ( classObject , propertyKey ) ; } public getHookOfAfterLoad ( classObject ) { return this . entityHookOfAfterLoadMap . get ( classObject ) ; } public mergeHooks ( classObject , subClassObject ) { for ( const map of [ this . entityHookOfAfterLoadMap , this . entityHookOfBeforeInsertMap , this . entityHookOfBeforeUpsertMap , this . entityHookOfBeforeUpdateMap , this . entityHookOfBeforeDeleteMap ] ) { if ( ! map . has ( classObject ) ) { const propertyKey = map . get ( subClassObject ) ; if ( propertyKey ) { map . set ( classObject , propertyKey ) ; } } } } public isGenerateId ( classObject ) { const map = this . entityFieldMetaListMap . get ( classObject ) ! ; const fieldMap = map . get ( "_id" ) ; return ! ! ( fieldMap ! . generateId ) ; } public getExcludeFromIndexes ( target ) { const entityMeta = this . getEntityMeta ( target ) ; return entityMeta . excludeFromIndexes ; } } export const decoratorMeta = new DecoratorMeta ( ) ;