-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathtypes.ts
179 lines (141 loc) · 5.26 KB
/
types.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import type { MigrationOptions } from '../types';
import { applyType, escapeValue } from '../utils';
import type {
AddTypeAttribute,
AddTypeValue,
CreateType,
DropType,
DropTypeAttribute,
RenameType,
RenameTypeAttribute,
RenameTypeValue,
SetTypeAttribute,
} from './typesTypes';
export type {
CreateType,
DropType,
RenameType,
AddTypeAttribute,
DropTypeAttribute,
SetTypeAttribute,
AddTypeValue,
RenameTypeAttribute,
RenameTypeValue,
};
export function dropType(mOptions: MigrationOptions): DropType {
const _drop: DropType = (typeName, options = {}) => {
const { ifExists, cascade } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const typeNameStr = mOptions.literal(typeName);
return `DROP TYPE${ifExistsStr} ${typeNameStr}${cascadeStr};`;
};
return _drop;
}
export function createType(mOptions: MigrationOptions): CreateType {
const _create: CreateType = (typeName, options) => {
if (Array.isArray(options)) {
const optionsStr = options.map(escapeValue).join(', ');
const typeNameStr = mOptions.literal(typeName);
return `CREATE TYPE ${typeNameStr} AS ENUM (${optionsStr});`;
}
const attributes = Object.entries(options)
.map(([attributeName, attribute]) => {
const typeStr = applyType(attribute, mOptions.typeShorthands).type;
return `${mOptions.literal(attributeName)} ${typeStr}`;
})
.join(',\n');
return `CREATE TYPE ${mOptions.literal(typeName)} AS (\n${attributes}\n);`;
};
_create.reverse = dropType(mOptions);
return _create;
}
export function dropTypeAttribute(
mOptions: MigrationOptions
): DropTypeAttribute {
const _drop: DropTypeAttribute = (
typeName,
attributeName,
{ ifExists } = {}
) => {
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const typeNameStr = mOptions.literal(typeName);
const attributeNameStr = mOptions.literal(attributeName);
return `ALTER TYPE ${typeNameStr} DROP ATTRIBUTE ${attributeNameStr}${ifExistsStr};`;
};
return _drop;
}
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;
}
export function setTypeAttribute(mOptions: MigrationOptions): SetTypeAttribute {
return (typeName, attributeName, attributeType) => {
const typeStr = applyType(attributeType, mOptions.typeShorthands).type;
const typeNameStr = mOptions.literal(typeName);
const attributeNameStr = mOptions.literal(attributeName);
return `ALTER TYPE ${typeNameStr} ALTER ATTRIBUTE ${attributeNameStr} SET DATA TYPE ${typeStr};`;
};
}
export function addTypeValue(mOptions: MigrationOptions): AddTypeValue {
const _add: AddTypeValue = (typeName, value, options = {}) => {
const { ifNotExists, before, after } = 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;
}
export function renameType(mOptions: MigrationOptions): RenameType {
const _rename: RenameType = (typeName, newTypeName) => {
const typeNameStr = mOptions.literal(typeName);
const newTypeNameStr = mOptions.literal(newTypeName);
return `ALTER TYPE ${typeNameStr} RENAME TO ${newTypeNameStr};`;
};
_rename.reverse = (typeName, newTypeName) => _rename(newTypeName, typeName);
return _rename;
}
export function renameTypeAttribute(
mOptions: MigrationOptions
): RenameTypeAttribute {
const _rename: RenameTypeAttribute = (
typeName,
attributeName,
newAttributeName
) => {
const typeNameStr = mOptions.literal(typeName);
const attributeNameStr = mOptions.literal(attributeName);
const newAttributeNameStr = mOptions.literal(newAttributeName);
return `ALTER TYPE ${typeNameStr} RENAME ATTRIBUTE ${attributeNameStr} TO ${newAttributeNameStr};`;
};
_rename.reverse = (typeName, attributeName, newAttributeName) =>
_rename(typeName, newAttributeName, attributeName);
return _rename;
}
export function renameTypeValue(mOptions: MigrationOptions): RenameTypeValue {
const _rename: RenameTypeValue = (typeName, value, newValue) => {
const valueStr = escapeValue(value);
const newValueStr = escapeValue(newValue);
const typeNameStr = mOptions.literal(typeName);
return `ALTER TYPE ${typeNameStr} RENAME VALUE ${valueStr} TO ${newValueStr};`;
};
_rename.reverse = (typeName, value, newValue) =>
_rename(typeName, newValue, value);
return _rename;
}