-
Notifications
You must be signed in to change notification settings - Fork 180
/
addTypeAttribute.ts
31 lines (25 loc) · 1.03 KB
/
addTypeAttribute.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
import type { MigrationOptions } from '../../types';
import { applyType } from '../../utils';
import type { Name, Reversible, Type } from '../generalTypes';
import type { DropTypeAttributeOptions } from './dropTypeAttribute';
import { dropTypeAttribute } from './dropTypeAttribute';
export type AddTypeAttributeFn = (
typeName: Name,
attributeName: string,
attributeType: Type & DropTypeAttributeOptions
) => string;
export type AddTypeAttribute = Reversible<AddTypeAttributeFn>;
export function addTypeAttribute(mOptions: MigrationOptions): AddTypeAttribute {
const _alterAttributeAdd: AddTypeAttribute = (
typeName,
attributeName,
attributeType
) => {
const typeStr = applyType(attributeType, mOptions.typeShorthands).type;
const typeNameStr = mOptions.literal(typeName);
const attributeNameStr = mOptions.literal(attributeName);
return `ALTER TYPE ${typeNameStr} ADD ATTRIBUTE ${attributeNameStr} ${typeStr};`;
};
_alterAttributeAdd.reverse = dropTypeAttribute(mOptions);
return _alterAttributeAdd;
}