-
Notifications
You must be signed in to change notification settings - Fork 180
/
grantRoles.ts
32 lines (24 loc) · 1.1 KB
/
grantRoles.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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Name, Reversible } from '../generalTypes';
import type { RevokeRolesOptions } from './revokeRoles';
import { revokeRoles } from './revokeRoles';
import type { WithAdminOption } from './shared';
export type GrantRolesOptions = WithAdminOption;
export type GrantRolesFn = (
rolesFrom: Name | Name[],
rolesTo: Name | Name[],
grantOptions?: GrantRolesOptions & RevokeRolesOptions
) => string;
export type GrantRoles = Reversible<GrantRolesFn>;
export function grantRoles(mOptions: MigrationOptions): GrantRoles {
const _grantRoles: GrantRoles = (rolesFrom, rolesTo, options = {}) => {
const { withAdminOption = false } = options;
const rolesFromStr = toArray(rolesFrom).map(mOptions.literal).join(', ');
const rolesToStr = toArray(rolesTo).map(mOptions.literal).join(', ');
const withAdminOptionStr = withAdminOption ? ' WITH ADMIN OPTION' : '';
return `GRANT ${rolesFromStr} TO ${rolesToStr}${withAdminOptionStr};`;
};
_grantRoles.reverse = revokeRoles(mOptions);
return _grantRoles;
}