import BaseEntity from '../models/BaseEntity' ; import SlothData from '../models/SlothData' ; import PouchFactory from '../models/PouchFactory' ; import EntityConstructor from '../helpers/EntityConstructor' ; import getProtoData from '../utils/getProtoData' ; import ProtoData from '../models/ProtoData' ; function mapPropsOrDocToDocument ( { fields } , data ) { if ( typeof data === 'string' ) { return { } ; } return fields . reduce ( ( props , { key , docKey } ) => { if ( ! ( key in data ) && ! ( docKey in data ) ) { return props ; } if ( key in data && docKey in data && key !== docKey ) { throw new Error ( `Both '${ key }' and '${ docKey }' exist on ${ data }` ) ; } return Object . assign ( { } , props , { [ docKey ] : key in data ? data [ key ] : data [ docKey ] } ) ; } , { } as any ) ; } param name typeparam export default function SlothEntity < S extends { _id : string ; } > ( name ) { return < T extends BaseEntity < S > > ( constructor ) : EntityConstructor < S , T > => { const data = getProtoData ( constructor . prototype , true ) ; data . name = name ; const BaseEntity = constructor as EntityConstructor < any , any > ; return class WrappedEntity extends BaseEntity { constructor ( factory , idOrProps ) { super ( factory , idOrProps ) ; this . sloth . props = mapPropsOrDocToDocument ( getProtoData ( this ) , idOrProps ) ; } } as any ; } ; }