-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathroles.ts
143 lines (112 loc) · 3.95 KB
/
roles.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
import type { MigrationOptions } from '../types';
import { escapeValue } from '../utils';
import type {
AlterRole,
CreateRole,
DropRole,
RenameRole,
RoleOptions,
} from './rolesTypes';
export type { CreateRole, DropRole, AlterRole, RenameRole };
const formatRoleOptions = (roleOptions: RoleOptions = {}) => {
const options: string[] = [];
if (roleOptions.superuser !== undefined) {
options.push(roleOptions.superuser ? 'SUPERUSER' : 'NOSUPERUSER');
}
if (roleOptions.createdb !== undefined) {
options.push(roleOptions.createdb ? 'CREATEDB' : 'NOCREATEDB');
}
if (roleOptions.createrole !== undefined) {
options.push(roleOptions.createrole ? 'CREATEROLE' : 'NOCREATEROLE');
}
if (roleOptions.inherit !== undefined) {
options.push(roleOptions.inherit ? 'INHERIT' : 'NOINHERIT');
}
if (roleOptions.login !== undefined) {
options.push(roleOptions.login ? 'LOGIN' : 'NOLOGIN');
}
if (roleOptions.replication !== undefined) {
options.push(roleOptions.replication ? 'REPLICATION' : 'NOREPLICATION');
}
if (roleOptions.bypassrls !== undefined) {
options.push(roleOptions.bypassrls ? 'BYPASSRLS' : 'NOBYPASSRLS');
}
if (roleOptions.limit) {
options.push(`CONNECTION LIMIT ${Number(roleOptions.limit)}`);
}
if (roleOptions.password !== undefined) {
const encrypted =
roleOptions.encrypted === false ? 'UNENCRYPTED' : 'ENCRYPTED';
options.push(`${encrypted} PASSWORD ${escapeValue(roleOptions.password)}`);
}
if (roleOptions.valid !== undefined) {
const valid = roleOptions.valid
? escapeValue(roleOptions.valid)
: "'infinity'";
options.push(`VALID UNTIL ${valid}`);
}
if (roleOptions.inRole) {
const inRole = Array.isArray(roleOptions.inRole)
? roleOptions.inRole.join(',')
: roleOptions.inRole;
options.push(`IN ROLE ${inRole}`);
}
if (roleOptions.role) {
const role = Array.isArray(roleOptions.role)
? roleOptions.role.join(',')
: roleOptions.role;
options.push(`ROLE ${role}`);
}
if (roleOptions.admin) {
const admin = Array.isArray(roleOptions.admin)
? roleOptions.admin.join(',')
: roleOptions.admin;
options.push(`ADMIN ${admin}`);
}
return options.join(' ');
};
export function dropRole(mOptions: MigrationOptions): DropRole {
const _drop: DropRole = (roleName, options = {}) => {
const { ifExists } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const roleNameStr = mOptions.literal(roleName);
return `DROP ROLE${ifExistsStr} ${roleNameStr};`;
};
return _drop;
}
export function createRole(mOptions: MigrationOptions): CreateRole {
const _create: CreateRole = (roleName, roleOptions = {}) => {
const options = formatRoleOptions({
...roleOptions,
superuser: roleOptions.superuser || false,
createdb: roleOptions.createdb || false,
createrole: roleOptions.createrole || false,
inherit: roleOptions.inherit !== false,
login: roleOptions.login || false,
replication: roleOptions.replication || false,
});
const optionsStr = options ? ` WITH ${options}` : '';
return `CREATE ROLE ${mOptions.literal(roleName)}${optionsStr};`;
};
_create.reverse = dropRole(mOptions);
return _create;
}
export function alterRole(mOptions: MigrationOptions): AlterRole {
const _alter: AlterRole = (roleName, roleOptions = {}) => {
const options = formatRoleOptions(roleOptions);
return options
? `ALTER ROLE ${mOptions.literal(roleName)} WITH ${options};`
: '';
};
return _alter;
}
export function renameRole(mOptions: MigrationOptions): RenameRole {
const _rename: RenameRole = (oldRoleName, newRoleName) => {
const oldRoleNameStr = mOptions.literal(oldRoleName);
const newRoleNameStr = mOptions.literal(newRoleName);
return `ALTER ROLE ${oldRoleNameStr} RENAME TO ${newRoleNameStr};`;
};
_rename.reverse = (oldRoleName, newRoleName) =>
_rename(newRoleName, oldRoleName);
return _rename;
}