-
Notifications
You must be signed in to change notification settings - Fork 38
/
StringField.ts
39 lines (32 loc) · 1.21 KB
/
StringField.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
36
37
38
39
import { MaxLength, MinLength } from 'class-validator';
import { DecoratorCommonOptions } from '../metadata';
import { composeMethodDecorators } from '../utils';
import { StringColumnType, StringWhereOperator } from '../torm';
import { getCombinedDecorator } from './getCombinedDecorator';
interface StringFieldOptions extends DecoratorCommonOptions {
dataType?: StringColumnType; // int16, jsonb, etc...
maxLength?: number;
minLength?: number;
default?: string;
unique?: boolean;
filter?: boolean | StringWhereOperator[];
array?: boolean;
}
export function StringField(options: StringFieldOptions = {}): any {
const maxLenOption = options.maxLength ? { length: options.maxLength } : {};
const uniqueOption = options.unique ? { unique: true } : {};
const factories = getCombinedDecorator<StringFieldOptions>({
fieldType: 'string',
warthogColumnMeta: options,
gqlFieldType: String,
dbType: options.dataType || 'varchar',
dbColumnOptions: { ...maxLenOption, ...uniqueOption }
});
if (options.minLength) {
factories.push(MinLength(options.minLength));
}
if (options.maxLength) {
factories.push(MaxLength(options.maxLength));
}
return composeMethodDecorators(...factories);
}