Moving some dayjs stuff to common folder to be able to re-use it

This commit is contained in:
Collin Duncan 2025-02-12 13:49:14 +01:00
parent 0e0fc68d7d
commit 9e9b0194da
No known key found for this signature in database
2 changed files with 25 additions and 30 deletions

View file

@ -1,5 +1,6 @@
import 'dayjs/locale/nl' import 'dayjs/locale/nl'
import { TransformationType, TransformFnParams } from 'class-transformer'
import * as dayjs from 'dayjs' import * as dayjs from 'dayjs'
import * as customParseFormat from 'dayjs/plugin/customParseFormat' import * as customParseFormat from 'dayjs/plugin/customParseFormat'
import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore' import * as isSameOrBefore from 'dayjs/plugin/isSameOrBefore'
@ -41,4 +42,20 @@ const dayjsTz = (
return dayjs(date, format).tz('Europe/Amsterdam') return dayjs(date, format).tz('Europe/Amsterdam')
} }
export const DayjsTransformer = ({ value, type }: TransformFnParams) => {
switch (type) {
case TransformationType.PLAIN_TO_CLASS:
return dayjs(value)
case TransformationType.CLASS_TO_PLAIN:
return value.format()
default:
return value
}
}
export const DayjsColumnTransformer = {
to: (value: dayjs.Dayjs) => value.format(),
from: (value: Date) => dayjs(value),
}
export default dayjsTz export default dayjsTz

View file

@ -1,9 +1,11 @@
import { Exclude, Transform, Type } from 'class-transformer' import { Exclude, Transform, Type } from 'class-transformer'
import { TransformationType } from 'class-transformer'
import { Dayjs } from 'dayjs' import { Dayjs } from 'dayjs'
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm' import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'
import dayjs from '../common/dayjs' import dayjs, {
DayjsColumnTransformer,
DayjsTransformer,
} from '../common/dayjs'
export interface Opponent { export interface Opponent {
id: string id: string
@ -20,42 +22,18 @@ export class Reservation {
@Column('datetime', { @Column('datetime', {
nullable: false, nullable: false,
transformer: { transformer: DayjsColumnTransformer,
to: (value: Dayjs) => value.format(),
from: (value: Date) => dayjs(value),
},
}) })
@Type(() => Dayjs) @Type(() => Dayjs)
@Transform(({ value, type }) => { @Transform(DayjsTransformer)
switch (type) {
case TransformationType.PLAIN_TO_CLASS:
return dayjs(value)
case TransformationType.CLASS_TO_PLAIN:
return value.format()
default:
return value
}
})
dateRangeStart: Dayjs dateRangeStart: Dayjs
@Column('datetime', { @Column('datetime', {
nullable: false, nullable: false,
transformer: { transformer: DayjsColumnTransformer,
to: (value: Dayjs) => value.format(),
from: (value: Date) => dayjs(value),
},
}) })
@Type(() => Dayjs) @Type(() => Dayjs)
@Transform(({ value, type }) => { @Transform(DayjsTransformer)
switch (type) {
case TransformationType.PLAIN_TO_CLASS:
return dayjs(value)
case TransformationType.CLASS_TO_PLAIN:
return value.format()
default:
return value
}
})
dateRangeEnd: Dayjs dateRangeEnd: Dayjs
@Column('json', { nullable: false }) @Column('json', { nullable: false })