-
Notifications
You must be signed in to change notification settings - Fork 176
/
grantOnTables.ts
42 lines (33 loc) · 1.38 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
import type { MigrationOptions } from '../../types';
import type { Reversible } from '../generalTypes';
import { revokeOnTables } from './revokeOnTables';
import type {
AllTablesOptions,
CommonGrantOnTablesOptions,
RevokeOnObjectsOptions,
SomeTablesOptions,
} from './shared';
import { asArray, asRolesStr, asTablesStr } from './shared';
export type GrantOnSomeTablesOptions = CommonGrantOnTablesOptions &
SomeTablesOptions;
export type GrantOnAllTablesOptions = CommonGrantOnTablesOptions &
AllTablesOptions;
export type GrantOnTablesOptions =
| GrantOnSomeTablesOptions
| GrantOnAllTablesOptions;
export type GrantOnTablesFn = (
options: GrantOnTablesOptions & RevokeOnObjectsOptions
) => string | string[];
export type GrantOnTables = Reversible<GrantOnTablesFn>;
export function grantOnTables(mOptions: MigrationOptions): GrantOnTables {
const _grantOnTables: GrantOnTables = (options) => {
const { privileges, roles, withGrantOption } = options;
const rolesStr = asRolesStr(roles, mOptions);
const privilegesStr = asArray(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;
}