Commit 5b6498b0 authored by nanahira's avatar nanahira

add JsonColumn

parent 92dc3bc9
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
IsString, IsString,
MaxLength, MaxLength,
Min, Min,
ValidateNested,
} from 'class-validator'; } from 'class-validator';
import { import {
WithPrecisionColumnType, WithPrecisionColumnType,
...@@ -19,9 +20,14 @@ import { ...@@ -19,9 +20,14 @@ import {
} from 'typeorm/driver/types/ColumnTypes'; } from 'typeorm/driver/types/ColumnTypes';
import { ColumnWithWidthOptions } from 'typeorm/decorator/options/ColumnWithWidthOptions'; import { ColumnWithWidthOptions } from 'typeorm/decorator/options/ColumnWithWidthOptions';
import { ColumnNumericOptions } from 'typeorm/decorator/options/ColumnNumericOptions'; import { ColumnNumericOptions } from 'typeorm/decorator/options/ColumnNumericOptions';
import { Exclude, Transform } from 'class-transformer'; import { Exclude, Transform, Type } from 'class-transformer';
import { BigintTransformer } from '../utility/bigint'; import { BigintTransformer } from '../utility/bigint';
import { Metadata } from '../utility/metadata'; import { Metadata } from '../utility/metadata';
import {
ClassOrArray,
getClassFromClassOrArray,
ParseType,
} from '../utility/insert-field';
export interface OpenAPIOptions<T> { export interface OpenAPIOptions<T> {
description?: string; description?: string;
...@@ -190,6 +196,21 @@ export const BoolColumn = ( ...@@ -190,6 +196,21 @@ export const BoolColumn = (
swaggerDecorator(options, { type: Boolean }), swaggerDecorator(options, { type: Boolean }),
]); ]);
export const JsonColumn = <C extends ClassOrArray>(
definition: C,
options: PropertyOptions<ParseType<C>> = {},
): PropertyDecorator => {
const cl = getClassFromClassOrArray(definition);
return MergePropertyDecorators([
Index(),
Type(() => cl),
ValidateNested(),
Column('jsonb', columnDecoratorOptions(options)),
validatorDecorator(options),
swaggerDecorator(options, { type: definition }),
]);
};
export const NotColumn = ( export const NotColumn = (
options: OpenAPIOptions<any> = {}, options: OpenAPIOptions<any> = {},
): PropertyDecorator => ): PropertyDecorator =>
......
...@@ -30,6 +30,14 @@ type TypeFromInsertOptions<O extends InsertOptions> = O extends InsertOptions< ...@@ -30,6 +30,14 @@ type TypeFromInsertOptions<O extends InsertOptions> = O extends InsertOptions<
| (O extends { options: { required: true } } ? never : undefined) | (O extends { options: { required: true } } ? never : undefined)
: never; : never;
type Merge<T, U> = {
[K in keyof T | keyof U]: K extends keyof T
? T[K]
: K extends keyof U
? U[K]
: never;
};
export function InsertField< export function InsertField<
C extends AnyClass, C extends AnyClass,
M extends Record<string, InsertOptions>, M extends Record<string, InsertOptions>,
...@@ -37,9 +45,12 @@ export function InsertField< ...@@ -37,9 +45,12 @@ export function InsertField<
cl: C, cl: C,
map: M, map: M,
newName?: string, newName?: string,
): new (...args: ParamsFromClass<C>) => TypeFromClass<C> & { ): new (...args: ParamsFromClass<C>) => Merge<
[F in keyof M]: TypeFromInsertOptions<M[F]>; {
} { [F in keyof M]: TypeFromInsertOptions<M[F]>;
},
TypeFromClass<C>
> {
const extendedCl = class extends cl {}; const extendedCl = class extends cl {};
for (const key in map) { for (const key in map) {
ApiProperty({ ApiProperty({
......
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