forked from goldcaddy77/warthog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJSONField.ts
35 lines (28 loc) · 1.03 KB
/
JSONField.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { GraphQLJSONObject } = require('graphql-type-json');
import { Field } from 'type-graphql';
import { Column } from 'typeorm';
import { decoratorDefaults } from '../metadata';
import { defaultColumnType } from '../torm';
import { composeMethodDecorators, MethodDecoratorFactory } from '../utils';
import { WarthogField } from './WarthogField';
interface JSONFieldOptions {
nullable?: boolean;
}
export function JSONField(args: JSONFieldOptions = {}): any {
const options = { ...decoratorDefaults, ...args };
const databaseConnection: string = process.env.WARTHOG_DB_CONNECTION || '';
const type = defaultColumnType(databaseConnection, 'json');
// These are the 2 required decorators to get type-graphql and typeorm working
const factories = [
WarthogField('json', options),
Field(() => GraphQLJSONObject, {
...options
}),
Column({
type,
...options
}) as MethodDecoratorFactory
];
return composeMethodDecorators(...factories);
}