-
Notifications
You must be signed in to change notification settings - Fork 180
/
grantOnTables.ts
47 lines (37 loc) · 1.5 KB
/
grantOnTables.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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Reversible } from '../generalTypes';
import type { RevokeOnTablesOptions } from './revokeOnTables';
import { revokeOnTables } from './revokeOnTables';
import type {
AllTablesOptions,
CommonGrantOnTablesOptions,
RevokeOnObjectsOptions,
SomeTablesOptions,
} from './shared';
import { asRolesStr, asTablesStr } from './shared';
export type GrantOnSomeTablesOptions = CommonGrantOnTablesOptions &
SomeTablesOptions;
export type GrantOnAllTablesOptions = CommonGrantOnTablesOptions &
AllTablesOptions;
export type GrantOnTablesOptions = (
| GrantOnSomeTablesOptions
| GrantOnAllTablesOptions
) &
RevokeOnObjectsOptions;
export type GrantOnTablesFn = (
grantOptions: GrantOnTablesOptions & RevokeOnTablesOptions
) => string;
export type GrantOnTables = Reversible<GrantOnTablesFn>;
export function grantOnTables(mOptions: MigrationOptions): GrantOnTables {
const _grantOnTables: GrantOnTables = (options) => {
const { privileges, roles, withGrantOption = false } = options;
const rolesStr = asRolesStr(roles, mOptions);
const privilegesStr = toArray(privileges).map(String).join(', ');
const tablesStr = asTablesStr(options, mOptions);
const withGrantOptionStr = withGrantOption ? ' WITH GRANT OPTION' : '';
return `GRANT ${privilegesStr} ON ${tablesStr} TO ${rolesStr}${withGrantOptionStr};`;
};
_grantOnTables.reverse = revokeOnTables(mOptions);
return _grantOnTables;
}