src/app/core/basic-datatypes/month/month.datatype.ts
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
Properties |
|
Methods |
| transformToDatabaseFormat | ||||||
transformToDatabaseFormat(value: unknown)
|
||||||
|
Inherited from
DefaultDatatype
|
||||||
|
Defined in
DefaultDatatype:22
|
||||||
|
Parameters :
Returns :
string
|
| transformToObjectFormat | ||||||||||||
transformToObjectFormat(value: string, schemaField: EntitySchemaField, parent: any)
|
||||||||||||
|
Inherited from
DefaultDatatype
|
||||||||||||
|
Defined in
DefaultDatatype:33
|
||||||||||||
|
Parameters :
Returns :
any
|
| Async anonymize | ||||||
anonymize(value: Date)
|
||||||
|
Inherited from
DefaultDatatype
|
||||||
|
Defined in
DefaultDatatype:91
|
||||||
|
Parameters :
Returns :
Promise<Date>
|
| Static detectFieldInEntity | ||||||
detectFieldInEntity(entityOrType: Entity | EntityConstructor)
|
||||||
|
Inherited from
DefaultDatatype
|
||||||
|
Defined in
DefaultDatatype:45
|
||||||
|
Parameters :
Returns :
string | undefined
|
| Async importMapFunction | ||||||||||||
importMapFunction(val: any, schemaField?: EntitySchemaField | undefined, additional?: any)
|
||||||||||||
|
Inherited from
DefaultDatatype
|
||||||||||||
|
Defined in
DefaultDatatype:78
|
||||||||||||
|
Parameters :
Returns :
unknown
|
| importIncompleteAdditionalConfigBadge | ||||||
importIncompleteAdditionalConfigBadge(col: ColumnMapping)
|
||||||
|
Inherited from
DefaultDatatype
|
||||||
|
Defined in
DefaultDatatype:166
|
||||||
|
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 :
Returns :
string
|
| normalizeSchemaField | ||||||||
normalizeSchemaField(schemaField: EntitySchemaField)
|
||||||||
|
Inherited from
DefaultDatatype
|
||||||||
|
Defined in
DefaultDatatype:182
|
||||||||
|
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
Parameters :
Returns :
EntitySchemaField
The schema field to use (default: unchanged) |
| 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);
}
}