Commit fb70fb1e authored by nanahira's avatar nanahira

update StringIdBase and DateColumn parser

parent 128a4577
...@@ -7,7 +7,7 @@ import { ...@@ -7,7 +7,7 @@ import {
NotWritable, NotWritable,
StringColumn, StringColumn,
} from '../decorators'; } from '../decorators';
import { IsNotEmpty } from 'class-validator'; import { IsNotEmpty, IsString } from 'class-validator';
import { MergePropertyDecorators } from 'nesties'; import { MergePropertyDecorators } from 'nesties';
export interface IdOptions { export interface IdOptions {
...@@ -38,7 +38,7 @@ export function IdBase(idOptions: IdOptions = {}) { ...@@ -38,7 +38,7 @@ export function IdBase(idOptions: IdOptions = {}) {
} }
export interface StringIdOptions extends IdOptions { export interface StringIdOptions extends IdOptions {
length: number; length?: number;
uuid?: boolean; uuid?: boolean;
} }
...@@ -56,18 +56,21 @@ export function StringIdBase(idOptions: StringIdOptions) { ...@@ -56,18 +56,21 @@ export function StringIdBase(idOptions: StringIdOptions) {
} }
}; };
const decs = [ const decs = [
NotChangeable(), StringColumn(idOptions.length || (idOptions.uuid ? 36 : 255), {
StringColumn(idOptions.length, {
required: !idOptions.uuid, required: !idOptions.uuid,
description: idOptions.description, description: idOptions.description,
columnExtras: { primary: true, nullable: false }, columnExtras: { primary: true, nullable: false },
}), }),
Reflect.metadata('design:type', String), Reflect.metadata('design:type', String),
IsNotEmpty(), ...(idOptions.uuid ? [
Generated('uuid'),
NotWritable(),
] : [
IsString(),
IsNotEmpty(),
NotChangeable(),
])
]; ];
if (idOptions.uuid) {
decs.push(Generated('uuid'));
}
const dec = MergePropertyDecorators(decs); const dec = MergePropertyDecorators(decs);
dec(cl.prototype, 'id'); dec(cl.prototype, 'id');
return cl; return cl;
......
...@@ -144,13 +144,23 @@ export const DateColumn = ( ...@@ -144,13 +144,23 @@ export const DateColumn = (
IsDate(), IsDate(),
Transform( Transform(
(v) => { (v) => {
if (v.value == null) return v.value; const value = v.value;
if (v.value instanceof Date) return v.value; if (value == null || value instanceof Date) return value;
if (typeof v.value === 'number') return new Date(v.value * 1000);
if (typeof v.value === 'string' && v.value.match(/^\d+$/)) { const timestampToDate = (t: number, isSeconds: boolean) =>
return new Date(parseInt(v.value, 10) * 1000); new Date(isSeconds ? t * 1000 : t);
if (typeof value === 'number') {
const isSeconds = !Number.isInteger(value) || value < 1e12;
return timestampToDate(value, isSeconds);
} }
return new Date(v.value);
if (typeof value === 'string' && /^\d+(\.\d+)?$/.test(value)) {
const isSeconds = value.includes('.') || parseFloat(value) < 1e12;
return timestampToDate(parseFloat(value), isSeconds);
}
return new Date(value); // fallback to native parser
}, },
{ {
toClassOnly: true, toClassOnly: true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment