File

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

Description

Datatype for the EntitySchemaService transforming Date values to/from a short string month format ("YYYY-mm").

Throws an exception if the property is set to something that is not a Date instance and cannot be cast to Date either. Uses the import value mapping properties of the general DateDatatype.

For example: @DatabaseField({dataType: 'month'}) myMonth: Date = new Date('2020-01-15'); // will be "2020-01" in the database

Extends

DateDatatype

Index

Properties
Methods

Methods

transformToDatabaseFormat
transformToDatabaseFormat(value)
Inherited from DefaultDatatype
Defined in DefaultDatatype:22
Parameters :
Name Optional
value No
Returns : string
transformToObjectFormat
transformToObjectFormat(value: string, schemaField: EntitySchemaField, parent: any)
Inherited from DefaultDatatype
Defined in DefaultDatatype:33
Parameters :
Name Type Optional
value string No
schemaField EntitySchemaField No
parent any No
Returns : any
Async anonymize
anonymize(value: Date)
Inherited from DefaultDatatype
Defined in DefaultDatatype:80
Parameters :
Name Type Optional
value Date No
Returns : Promise<Date>
Async importMapFunction
importMapFunction(val: any, schemaField: EntitySchemaField, additional?: any)
Inherited from DefaultDatatype
Defined in DefaultDatatype:67
Parameters :
Name Type Optional
val any No
schemaField EntitySchemaField No
additional any Yes
Returns : unknown
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

Properties

Static dataType
Type : string
Default value : "month"
Inherited from DefaultDatatype
Defined in DefaultDatatype:16
editComponent
Type : string
Default value : "EditMonth"
Inherited from DefaultDatatype
Defined in DefaultDatatype:20
Static label
Type : string
Default value : $localize`:datatype-label:month (date without day of month)`
Inherited from DefaultDatatype
Defined in DefaultDatatype:17
viewComponent
Type : string
Default value : "DisplayMonth"
Inherited from DefaultDatatype
Defined in DefaultDatatype:19
importConfigComponent
Type : string
Default value : "DateImportConfig"
Inherited from DefaultDatatype
Defined in DefaultDatatype:65
Readonly importAllowsMultiMapping
Type : boolean
Default value : false
Inherited from DefaultDatatype
Defined in DefaultDatatype:48

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

import { Injectable } from "@angular/core";
import { DateDatatype } from "../date/date.datatype";
import { EntitySchemaField } from "../../entity/schema/entity-schema-field";

/**
 * Datatype for the EntitySchemaService transforming Date values to/from a short string month format ("YYYY-mm").
 *
 * Throws an exception if the property is set to something that is not a Date instance and cannot be cast to Date either.
 * Uses the import value mapping properties of the general DateDatatype.
 *
 * For example:
 * `@DatabaseField({dataType: 'month'}) myMonth: Date = new Date('2020-01-15'); // will be "2020-01" in the database`
 */
@Injectable()
export class MonthDatatype extends DateDatatype {
  static override dataType = "month";
  static override label: string = $localize`:datatype-label:month (date without day of month)`;

  override viewComponent = "DisplayMonth";
  override editComponent = "EditMonth";

  override transformToDatabaseFormat(value) {
    if (!(value instanceof Date)) {
      value = new Date(value);
    }
    return (
      value.getFullYear().toString() +
      "-" +
      (value.getMonth() + 1).toString().replace(/^(\d)$/g, "0$1")
    );
  }

  override transformToObjectFormat(
    value: string,
    schemaField: EntitySchemaField,
    parent: any,
  ) {
    const values = value.split("-").map((v) => Number(v));
    const date = new Date(values[0], values[1] - 1);

    // re-use error logging and basic return logic from base type
    return super.transformToObjectFormat(date, schemaField, parent);
  }
}

results matching ""

    No results matching ""