import { IdentityGenerator } from "../config/configuration" ; import { ClassMapping } from "./classMapping" ; import { ChangeTrackingType , FlushPriority } from "./mappingModel" ; import { Index } from "./index" ; import { CollectionOptions } from "./collectionOptions" ; import { MappingModel } from "./mappingModel" ; import { Reference } from "../reference" ; import { InternalSession } from "../session" ; import { ResultCallback } from "../core/callback" ; import { ResolveContext } from "./resolveContext" ; import { ReadContext } from "./readContext" ; import { Observer } from "../observer" ; import { Property } from "./property" ; import { WriteContext } from "./writeContext" ; import { PersistenceError } from "../persistenceError" ; import { QueryDocument } from "../query/queryBuilder" ; hidden export class EntityMapping extends ClassMapping { collectionName : string ; databaseName : string ; indexes : Index [ ] ; collectionOptions : CollectionOptions ; identity : IdentityGenerator ; changeTracking : ChangeTrackingType ; versioned : boolean ; versionField : string ; /**
     * The order in which collections are flushed to the database. Higher priority collections are flushed first.
     */ flushPriority = FlushPriority . Medium ; private _defaultFields : QueryDocument ; constructor ( baseClass ? ) { super ( baseClass ) ; this . flags &= ~ MappingModel . MappingFlags . Embeddable ; this . flags |= MappingModel . MappingFlags . Entity ; } param property returns validateProperty ( property ) { if ( property && property . name ) { if ( property . name == "_id" ) { return "The '_id' property on an entity class is automatically populated with the primary key and cannot be explicitly mapped." ; } if ( property . name == "id" ) { return "The 'id' property on an entity class is automatically populated with the string representation of the primary key " + "and cannot be explicitly mapped." ; } } return super . validateProperty ( property ) ; } setDocumentVersion ( obj , version ) { this . setDocumentVersion = < any > ( new Function ( "o" , "v" , "o['" + ( < EntityMapping > this . inheritanceRoot ) . versionField + "'] = v" ) ) ; obj [ ( < EntityMapping > this . inheritanceRoot ) . versionField ] = version ; } getDocumentVersion ( obj ) { this . getDocumentVersion = < any > ( new Function ( "o" , "return o['" + ( < EntityMapping > this . inheritanceRoot ) . versionField + "']" ) ) ; return obj [ ( < EntityMapping > this . inheritanceRoot ) . versionField ] ; } addIndex ( index ) { if ( ! this . indexes ) { this . indexes = [ ] ; } this . indexes . push ( index ) ; } refresh ( context , entity , document ) { var mapping = this . getMapping ( context , document ) ; if ( mapping ) { if ( mapping != this ) { context . addError ( "Refresh does not support changing instantiated class of entity." ) ; return ; } return this . readObject ( context , entity , document , true ) ; } } read ( context , value ) { if ( value == null ) return null ; var id ; if ( context . path ) { id = value ; } else { id = value [ "_id" ] ; if ( ! id ) { context . addError ( "Missing identifier." , "_id" ) ; return ; } } if ( ! ( < EntityMapping > this . inheritanceRoot ) . identity . validate ( id ) ) { context . addError ( "'" + id . toString ( ) + "' is not a valid identifier." , ( context . path ? context . path + "." : "" ) + "_id" ) ; return ; } if ( context . path ) { return context . session . getReferenceInternal ( this , id ) ; } var obj = super . read ( context , value ) ; if ( obj ) { obj [ "_id" ] = id ; obj [ "id" ] = id . toString ( ) ; } return obj ; } write ( context , value ) { if ( value == null ) return null ; var id ; if ( ! ( id = value [ "_id" ] ) ) { context . addError ( "Missing identifier." , ( context . path ? context . path + "." : "" ) + "_id" ) ; return ; } if ( ! ( < EntityMapping > this . inheritanceRoot ) . identity . validate ( id ) ) { context . addError ( "'" + id . toString ( ) + "' is not a valid identifier." , ( context . path ? context . path + "." : "" ) + "_id" ) ; return ; } if ( context . path ) { return id ; } var document = super . write ( context , value ) ; if ( document ) { document [ "_id" ] = id ; } return document ; } watchEntity ( entity , observer ) { super . watch ( entity , observer , [ ] ) ; } watch ( value , observer , visited ) { } areDocumentsEqual ( document1 , document2 ) { return super . areEqual ( document1 , document2 ) ; } areEqual ( documentValue1 , documentValue2 ) { if ( documentValue1 === documentValue2 ) return true ; if ( documentValue1 == null || documentValue2 == null ) return false ; var id1 = documentValue1 [ "_id" ] || documentValue1 , id2 = documentValue2 [ "_id" ] || documentValue2 ; if ( id1 == null || id2 == null ) { return false ; } return ( < EntityMapping > this . inheritanceRoot ) . identity . areEqual ( id1 , id2 ) ; } walk ( session , value , flags , entities , embedded , references ) { if ( ! value || typeof value !== "object" ) return ; if ( Reference . isReference ( value ) ) { var entity = session . getObject ( ( < Reference > value ) . id ) ; if ( entity ) { value = entity ; } else { if ( flags & MappingModel . PropertyFlags . Dereference ) { references . push ( value ) ; } return ; } } if ( entities . indexOf ( value ) !== - 1 ) return ; entities . push ( value ) ; if ( ( flags & MappingModel . PropertyFlags . WalkEntities ) == 0 && entities . length > 1 ) return ; super . walk ( session , value , flags , entities , embedded , references ) ; } fetch ( session , parentEntity , value , path , depth , callback ) { if ( ! value || typeof value !== "object" ) { process . nextTick ( ( ) => callback ( null , value ) ) ; return ; } if ( Reference . isReference ( value ) ) { ( < Reference > value ) . fetch ( session , ( err , entity ) => { if ( err ) return callback ( err ) ; super . fetch ( session , entity , entity , path , depth , callback ) ; } ) ; return ; } super . fetch ( session , value , value , path , depth , callback ) ; } fetchInverse ( session , parentEntity , propertyName , path , depth , callback ) { if ( ! parentEntity ) { return callback ( new PersistenceError ( "Parent entity required to resolve inverse relationship." ) ) ; } session . getPersister ( this ) . findOneInverseOf ( parentEntity , propertyName , ( err , value ) => { if ( err ) return callback ( err ) ; super . fetch ( session , this , value , path , depth , callback ) ; } ) ; } protected fetchPropertyValue ( session , value , property , callback ) { session . getPersister ( this ) . fetchPropertyValue ( value , property , callback ) ; } getDefaultFields ( ) { var fields = { } ; if ( this . _defaultFields === undefined ) { for ( var i = 0 ; i < this . properties . length ; i ++ ) { var property = this . properties [ i ] ; if ( ( property . flags & MappingModel . PropertyFlags . FetchLazy ) != 0 ) { property . setFieldValue ( fields , 0 ) ; } } this . _defaultFields = fields ; } return this . _defaultFields ; } protected resolveCore ( context ) { if ( ! context . isFirst ) { context . setError ( "Unable to resolve entity mapping. The dot notation can only be used for embedded objects." ) ; return ; } super . resolveCore ( context ) ; } }