-
Notifications
You must be signed in to change notification settings - Fork 180
/
shared.ts
98 lines (69 loc) · 2.26 KB
/
shared.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
import { escapeValue, toArray } from '../../utils';
import type { Value } from '../generalTypes';
export interface RoleOptions {
superuser?: boolean;
createdb?: boolean;
createrole?: boolean;
inherit?: boolean;
login?: boolean;
replication?: boolean;
bypassrls?: boolean;
limit?: number;
password?: Value;
encrypted?: boolean;
valid?: Value;
inRole?: string | string[];
role?: string | string[];
admin?: string | string[];
}
export function formatRoleOptions(roleOptions: RoleOptions = {}): string {
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 = toArray(roleOptions.inRole).join(', ');
options.push(`IN ROLE ${inRole}`);
}
if (roleOptions.role) {
const role = toArray(roleOptions.role).join(', ');
options.push(`ROLE ${role}`);
}
if (roleOptions.admin) {
const admin = toArray(roleOptions.admin).join(', ');
options.push(`ADMIN ${admin}`);
}
return options.join(' ');
}