-
Notifications
You must be signed in to change notification settings - Fork 169
/
grantOnSchemas.ts
45 lines (37 loc) · 1.35 KB
/
grantOnSchemas.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
import type { MigrationOptions } from '../../types';
import type { Name, Reversible } from '../generalTypes';
import { revokeOnSchemas } from './revokeOnSchemas';
import type {
RevokeOnObjectsOptions,
SchemaPrivilege,
WithGrantOption,
} from './shared';
import { asArray, asRolesStr } from './shared';
export interface OnlyGrantOnSchemasOptions {
privileges: SchemaPrivilege | SchemaPrivilege[] | 'ALL';
schemas: string[] | string;
roles: Name | Name[];
}
export type GrantOnSchemasOptions = OnlyGrantOnSchemasOptions &
WithGrantOption &
RevokeOnObjectsOptions;
export type GrantOnSchemasFn = (
options: GrantOnSchemasOptions
) => string | string[];
export type GrantOnSchemas = Reversible<GrantOnSchemasFn>;
export function grantOnSchemas(mOptions: MigrationOptions): GrantOnSchemas {
const _grantOnSchemas: GrantOnSchemas = ({
privileges,
schemas,
roles,
withGrantOption,
}) => {
const rolesStr = asRolesStr(roles, mOptions);
const schemasStr = asArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = asArray(privileges).map(String).join(', ');
const withGrantOptionStr = withGrantOption ? ' WITH GRANT OPTION' : '';
return `GRANT ${privilegesStr} ON SCHEMA ${schemasStr} TO ${rolesStr}${withGrantOptionStr};`;
};
_grantOnSchemas.reverse = revokeOnSchemas(mOptions);
return _grantOnSchemas;
}