-
Notifications
You must be signed in to change notification settings - Fork 180
/
createOperator.ts
90 lines (65 loc) · 1.81 KB
/
createOperator.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
import type { MigrationOptions } from '../../types';
import type { Name, Reversible } from '../generalTypes';
import type { DropOperatorOptions } from './dropOperator';
import { dropOperator } from './dropOperator';
export interface CreateOperatorOptions {
procedure: Name;
left?: Name;
right?: Name;
commutator?: Name;
negator?: Name;
restrict?: Name;
join?: Name;
hashes?: boolean;
merges?: boolean;
}
export type CreateOperatorFn = (
operatorName: Name,
operatorOptions: CreateOperatorOptions & DropOperatorOptions
) => string;
export type CreateOperator = Reversible<CreateOperatorFn>;
export function createOperator(mOptions: MigrationOptions): CreateOperator {
const _create: CreateOperator = (operatorName, options) => {
const {
procedure,
left,
right,
commutator,
negator,
restrict,
join,
hashes = false,
merges = false,
} = options;
const defs: string[] = [];
defs.push(`PROCEDURE = ${mOptions.literal(procedure)}`);
if (left) {
defs.push(`LEFTARG = ${mOptions.literal(left)}`);
}
if (right) {
defs.push(`RIGHTARG = ${mOptions.literal(right)}`);
}
if (commutator) {
defs.push(`COMMUTATOR = ${mOptions.schemalize(commutator)}`);
}
if (negator) {
defs.push(`NEGATOR = ${mOptions.schemalize(negator)}`);
}
if (restrict) {
defs.push(`RESTRICT = ${mOptions.literal(restrict)}`);
}
if (join) {
defs.push(`JOIN = ${mOptions.literal(join)}`);
}
if (hashes) {
defs.push('HASHES');
}
if (merges) {
defs.push('MERGES');
}
const operatorNameStr = mOptions.schemalize(operatorName);
return `CREATE OPERATOR ${operatorNameStr} (${defs.join(', ')});`;
};
_create.reverse = dropOperator(mOptions);
return _create;
}