File

src/app/core/basic-datatypes/date/date.datatype.ts

Description

Datatype for the EntitySchemaService transforming values to Date instances.

This type is automatically used if you annotate a class's property that has the TypeScript type "Date" ensuring that even if values in the database might be some kind of date string they will be cast to Date instances.

For example:

@DatabaseField() myDate: Date; // will be a valid Date even if the database previously had "2020-01-15" as string

Extends

DefaultDatatype

Index

Properties
Methods

Methods

Async anonymize
anonymize(value: Date)
Inherited from DefaultDatatype
Defined in DefaultDatatype:91
Parameters :
Name Type Optional
value Date No
Returns : Promise<Date>
Static detectFieldInEntity
detectFieldInEntity(entityOrType: Entity | EntityConstructor)
Inherited from DefaultDatatype
Defined in DefaultDatatype:45
Parameters :
Name Type Optional
entityOrType Entity | EntityConstructor No
Returns : string | undefined
Async importMapFunction
importMapFunction(val: any, schemaField?: EntitySchemaField | undefined, additional?: any)
Inherited from DefaultDatatype
Defined in DefaultDatatype:78
Parameters :
Name Type Optional
val any No
schemaField EntitySchemaField | undefined Yes
additional any Yes
Returns : unknown
transformToDatabaseFormat
transformToDatabaseFormat(value: Date)
Inherited from DefaultDatatype
Defined in DefaultDatatype:57
Parameters :
Name Type Optional
value Date No
Returns : any
transformToObjectFormat
transformToObjectFormat(value: unknown, schemaField: EntitySchemaField, parent: any)
Inherited from DefaultDatatype
Defined in DefaultDatatype:61
Parameters :
Name Type Optional
value unknown No
schemaField EntitySchemaField No
parent any No
Returns : any
importIncompleteAdditionalConfigBadge
importIncompleteAdditionalConfigBadge(col: ColumnMapping)
Inherited from DefaultDatatype

Output a label indicating whether the given column mapping needs user configuration for the "additional" config or has a valid, complete "additional" config. returns "undefined" if no user action is required.

Parameters :
Name Type Optional
col ColumnMapping No
Returns : string
normalizeSchemaField
normalizeSchemaField(schemaField: EntitySchemaField)
Inherited from DefaultDatatype

Return the (potentially adjusted) schema field for this datatype.

Called when schema fields are set up (e.g. from config), allowing the datatype to normalize or fill in required settings.

Override this in a subclass to enforce constraints (e.g. always setting isArray: true).

Parameters :
Name Type Optional Description
schemaField EntitySchemaField No

The current schema field definition

Returns : EntitySchemaField

The schema field to use (default: unchanged)

Properties

Static dataType
Type : string
Default value : "date"
Inherited from DefaultDatatype
Defined in DefaultDatatype:40
editComponent
Type : string
Default value : "EditDate"
Inherited from DefaultDatatype
Defined in DefaultDatatype:55
importConfigComponent
Type : string
Default value : "DateImportConfig"
Inherited from DefaultDatatype
Defined in DefaultDatatype:76
viewComponent
Type : string
Default value : "DisplayDate"
Inherited from DefaultDatatype
Defined in DefaultDatatype:54
Readonly importAllowsMultiMapping
Type : boolean
Default value : false
Inherited from DefaultDatatype
Defined in DefaultDatatype:80

Whether this datatype allows multiple values to be mapped to the same entity field during import.

Static label
Type : string
Default value : $localize`:datatype-label:any`
Inherited from DefaultDatatype
Defined in DefaultDatatype:89

The human-readable name for this dataType, used in config UIs.

import { DefaultDatatype } from "../../entity/default-datatype/default.datatype";
import { Injectable } from "@angular/core";
import { Entity, EntityConstructor } from "../../entity/model/entity";
import { EntitySchemaField } from "../../entity/schema/entity-schema-field";
import moment from "moment";
import { Logging } from "../../logging/logging.service";

/**
 * Datatype for the EntitySchemaService transforming values to Date instances.
 *
 * This type is automatically used if you annotate a class's property that has the TypeScript type "Date"
 * ensuring that even if values in the database might be some kind of date string they will be cast to Date instances.
 *
 * For example:
 *
 * `@DatabaseField() myDate: Date; // will be a valid Date even if the database previously had "2020-01-15" as string`
 */
@Injectable()
export class DateDatatype<DBFormat = string> extends DefaultDatatype<
  Date,
  DBFormat
> {
  static override dataType = "date";
  // currently not shown to users in Admin UI, as this is not supported well with timezones and UI
  // static override label: string = $localize`:datatype-label:date (with time)`;

  /** @override Detects the first `date` or `date-only` field in the entity schema. */
  static override detectFieldInEntity(
    entityOrType: Entity | EntityConstructor,
  ): string | undefined {
    return DefaultDatatype.detectFieldInEntity(entityOrType, [
      DateDatatype.dataType,
      "date-only",
    ]);
  }

  override viewComponent = "DisplayDate";
  override editComponent = "EditDate";

  override transformToDatabaseFormat(value: Date) {
    return value as any;
  }

  override transformToObjectFormat(
    value,
    schemaField: EntitySchemaField,
    parent: any,
  ) {
    const date = new Date(value);
    if (Number.isNaN(date.getTime())) {
      Logging.debug(
        `failed to convert data '${value}' to Date object for ${parent?._id}`,
      );
      return undefined;
    }
    return date;
  }

  override importConfigComponent = "DateImportConfig";

  override async importMapFunction(
    val: any,
    schemaField?: EntitySchemaField | undefined,
    additional?: any,
  ) {
    const date = moment(val, additional, true);
    if (date.isValid()) {
      return date.toDate();
    } else {
      return undefined;
    }
  }

  override async anonymize(value: Date): Promise<Date> {
    // normalize to 01.06. of the year, which has less statistical distortion than 01.01.
    // (roughly half the dates before anonymization will be earlier and half will be later)
    return new Date(value.getFullYear(), 6, 1);
  }
}

results matching ""

    No results matching ""