import { Entity } from "../model/entity" ; import { EntitySchemaDatatype } from "./entity-schema-datatype" ; import { Injectable } from "@angular/core" ; import { defaultEntitySchemaDatatype } from "../schema-datatypes/datatype-default" ; import { EntitySchema } from "./entity-schema" ; import { EntitySchemaField } from "./entity-schema-field" ; import { stringEntitySchemaDatatype } from "../schema-datatypes/datatype-string" ; import { numberEntitySchemaDatatype } from "../schema-datatypes/datatype-number" ; import { dateEntitySchemaDatatype } from "../schema-datatypes/datatype-date" ; import { monthEntitySchemaDatatype } from "../schema-datatypes/datatype-month" ; import { arrayEntitySchemaDatatype } from "../schema-datatypes/datatype-array" ; import { schemaEmbedEntitySchemaDatatype } from "../schema-datatypes/datatype-schema-embed" ; import { dateOnlyEntitySchemaDatatype } from "../schema-datatypes/datatype-date-only" ; import { mapEntitySchemaDatatype } from "../schema-datatypes/datatype-map" ; import { booleanEntitySchemaDatatype } from "../schema-datatypes/datatype-boolean" ; /**
 * Transform between entity instances and database objects
 * based on the dataType set for properties in Entity classes using the  DatabaseField annotation.
 *
 * You can inject the EntitySchemaService in your code to register your custom  EntitySchemaDatatype implementations.
 *
 * This service is used by the  EntityMapperService to internally transform objects.
 * You should normally use the EntityMapperService instead of transforming objects yourself with the EntitySchemaService.
 *
 * also see the How-To Guides:
 * - [Create A New Entity Type] {@link /additional-documentation/how-to-guides/create-a-new-entity-type.html} @ Injectable ( ) export class EntitySchemaService { /**
     * Internal registry of data type definitions.
     * You can extend the Schema system with your data type conversions by using EntitySchema.registerSchemaDatatype()
     */ private schemaTypes = new Map < string , EntitySchemaDatatype > ( ) ; constructor ( ) { this . registerBasicDatatypes ( ) ; } private registerBasicDatatypes ( ) { this . registerSchemaDatatype ( stringEntitySchemaDatatype ) ; this . registerSchemaDatatype ( numberEntitySchemaDatatype ) ; this . registerSchemaDatatype ( dateEntitySchemaDatatype ) ; this . registerSchemaDatatype ( dateOnlyEntitySchemaDatatype ) ; this . registerSchemaDatatype ( monthEntitySchemaDatatype ) ; this . registerSchemaDatatype ( arrayEntitySchemaDatatype ) ; this . registerSchemaDatatype ( schemaEmbedEntitySchemaDatatype ) ; this . registerSchemaDatatype ( mapEntitySchemaDatatype ) ; this . registerSchemaDatatype ( booleanEntitySchemaDatatype ) ; } param type public registerSchemaDatatype ( type ) { this . schemaTypes . set ( type . name , type ) ; } param datatypeName public getDatatypeOrDefault ( datatypeName ) { datatypeName = datatypeName ? datatypeName . toLowerCase ( ) : undefined ; if ( this . schemaTypes . has ( datatypeName ) ) { return this . schemaTypes . get ( datatypeName ) ; } else { return defaultEntitySchemaDatatype ; } } param data param schema public transformDatabaseToEntityFormat ( data , schema ) { for ( const key of schema . keys ( ) ) { const schemaField = schema . get ( key ) ; if ( data [ key ] === undefined || data [ key ] === null ) { if ( schemaField . defaultValue !== undefined ) { data [ key ] = schemaField . defaultValue ; } else { continue ; } } const newValue = this . getDatatypeOrDefault ( schemaField . dataType ) . transformToObjectFormat ( data [ key ] , schemaField , this , data ) ; if ( newValue !== undefined ) { data [ key ] = newValue ; } if ( schemaField . generateIndex ) { throw new Error ( 'schema option "isIndexed" not implemented yet' ) ; } } return data ; } param entity param data public loadDataIntoEntity ( entity , data ) { data = this . transformDatabaseToEntityFormat ( data , ( < typeof Entity > entity . constructor ) . schema ) ; Object . assign ( entity , data ) ; } param entity param schema public transformEntityToDatabaseFormat ( entity , schema ? ) { if ( ! schema ) { schema = entity . getConstructor ( ) . schema ; } const data = { } ; for ( const key of schema . keys ( ) ) { let value = entity [ key ] ; const schemaField = schema . get ( key ) ; if ( value === undefined || value === null ) { if ( schemaField . defaultValue !== undefined ) { value = schemaField . defaultValue ; } else { continue ; } } try { data [ key ] = this . getDatatypeOrDefault ( schemaField . dataType ) . transformToDatabaseFormat ( value , schemaField , this , entity ) ; } catch ( err ) { throw new Error ( `Transformation for ${ key } failed: ${ err }` ) ; } if ( data [ key ] === undefined ) { delete data [ key ] ; } } return data ; } param propertySchema param mode returns getComponent ( propertySchema , mode = "view" ) { if ( ! propertySchema ) { return undefined ; } const componentAttribute = mode === "view" ? "viewComponent" : "editComponent" ; return ( propertySchema [ componentAttribute ] || this . getDatatypeOrDefault ( propertySchema . dataType ) [ componentAttribute ] ) ; } }