-
Notifications
You must be signed in to change notification settings - Fork 180
/
shared.ts
73 lines (59 loc) · 1.66 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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { CascadeOption, Name } from '../generalTypes';
export interface WithAdminOption {
readonly withAdminOption?: boolean;
}
export interface OnlyAdminOption {
readonly onlyAdminOption?: boolean;
}
export interface OnlyGrantOption {
readonly onlyGrantOption?: boolean;
}
export interface WithGrantOption {
readonly withGrantOption?: boolean;
}
export type TablePrivilege =
| 'SELECT'
| 'INSERT'
| 'UPDATE'
| 'DELETE'
| 'TRUNCATE'
| 'REFERENCES'
| 'TRIGGER';
export type SchemaPrivilege = 'CREATE' | 'USAGE';
export interface CommonOnTablesOptions {
privileges: TablePrivilege | TablePrivilege[] | 'ALL';
roles: Name | Name[];
}
export type CommonGrantOnTablesOptions = CommonOnTablesOptions &
WithGrantOption;
export interface SomeTablesOptions {
tables: Name | Name[];
}
export interface AllTablesOptions {
tables: 'ALL';
schema: string;
}
export type RevokeOnObjectsOptions = OnlyGrantOption & CascadeOption;
export function isAllTablesOptions(
options: AllTablesOptions | SomeTablesOptions
): options is AllTablesOptions {
return 'schema' in options;
}
export function asRolesStr(
roles: Name | Name[],
mOptions: MigrationOptions
): string {
return toArray(roles)
.map((role) => (role === 'PUBLIC' ? role : mOptions.literal(role)))
.join(', ');
}
export function asTablesStr(
options: AllTablesOptions | SomeTablesOptions,
mOptions: MigrationOptions
): string {
return isAllTablesOptions(options)
? `ALL TABLES IN SCHEMA ${mOptions.literal(options.schema)}`
: toArray(options.tables).map(mOptions.literal).join(', ');
}