Commit 89aa1d52 authored by nanahira's avatar nanahira

fix JsonColumn

parent 16c23a10
...@@ -28,6 +28,7 @@ import { ...@@ -28,6 +28,7 @@ import {
getClassFromClassOrArray, getClassFromClassOrArray,
ParseType, ParseType,
} from '../utility/insert-field'; } from '../utility/insert-field';
import { TypeTransformer } from '../utility/type-transformer';
export interface OpenAPIOptions<T> { export interface OpenAPIOptions<T> {
description?: string; description?: string;
...@@ -205,7 +206,10 @@ export const JsonColumn = <C extends ClassOrArray>( ...@@ -205,7 +206,10 @@ export const JsonColumn = <C extends ClassOrArray>(
Index(), Index(),
Type(() => cl), Type(() => cl),
ValidateNested(), ValidateNested(),
Column('jsonb', columnDecoratorOptions(options)), Column('jsonb', {
...columnDecoratorOptions(options),
transformer: new TypeTransformer(definition),
}),
validatorDecorator(options), validatorDecorator(options),
swaggerDecorator(options, { type: definition }), 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