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