-
Notifications
You must be signed in to change notification settings - Fork 180
/
addTypeValue.ts
35 lines (27 loc) · 1.1 KB
/
addTypeValue.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
import type { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import type { IfNotExistsOption, Name, Value } from '../generalTypes';
export interface AddTypeValueOptions extends IfNotExistsOption {
before?: string;
after?: string;
}
export type AddTypeValue = (
typeName: Name,
value: Value,
typeValueOptions?: AddTypeValueOptions
) => string;
export function addTypeValue(mOptions: MigrationOptions): AddTypeValue {
const _add: AddTypeValue = (typeName, value, options = {}) => {
const { before, after, ifNotExists = false } = options;
if (before && after) {
throw new Error('"before" and "after" can\'t be specified together');
}
const beforeStr = before ? ` BEFORE ${escapeValue(before)}` : '';
const afterStr = after ? ` AFTER ${escapeValue(after)}` : '';
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const valueStr = escapeValue(value);
const typeNameStr = mOptions.literal(typeName);
return `ALTER TYPE ${typeNameStr} ADD VALUE${ifNotExistsStr} ${valueStr}${beforeStr}${afterStr};`;
};
return _add;
}