Commit 89aa1d52 authored by nanahira's avatar nanahira

fix JsonColumn

parent 16c23a10
......@@ -28,6 +28,7 @@ import {
getClassFromClassOrArray,
ParseType,
} from '../utility/insert-field';
import { TypeTransformer } from '../utility/type-transformer';
export interface OpenAPIOptions<T> {
description?: string;
......@@ -205,7 +206,10 @@ export const JsonColumn = <C extends ClassOrArray>(
Index(),
Type(() => cl),
ValidateNested(),
Column('jsonb', columnDecoratorOptions(options)),
Column('jsonb', {
...columnDecoratorOptions(options),
transformer: new TypeTransformer(definition),
}),
validatorDecorator(options),
swaggerDecorator(options, { type: definition }),
]);
......
import { ClassOrArray } from './insert-field';
import { ValueTransformer } from 'typeorm';
export class TypeTransformer implements ValueTransformer {
constructor(private definition: ClassOrArray) {}
from(dbValue) {
if (!dbValue) {
return dbValue;
}
if (Array.isArray(this.definition)) {
return dbValue.map((value) =>
Object.assign(new this.definition[0](), value),
);
}
return Object.assign(new this.definition(), dbValue);
}
to(entValue): any {
return entValue;
}
}
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