Commit 5b6498b0 authored by nanahira's avatar nanahira

add JsonColumn

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