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: unknown)
Inherited from DefaultDatatype
Defined in DefaultDatatype:22
Parameters :
Name Type Optional
value unknown 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: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
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 : "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:76
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.

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 ""