-
Notifications
You must be signed in to change notification settings - Fork 180
/
grantOnSchemas.ts
44 lines (35 loc) · 1.48 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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Name, Reversible } from '../generalTypes';
import type { RevokeOnSchemasOptions } from './revokeOnSchemas';
import { revokeOnSchemas } from './revokeOnSchemas';
import type {
RevokeOnObjectsOptions,
SchemaPrivilege,
WithGrantOption,
} from './shared';
import { 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 = (
grantOptions: GrantOnSchemasOptions & RevokeOnSchemasOptions
) => string;
export type GrantOnSchemas = Reversible<GrantOnSchemasFn>;
export function grantOnSchemas(mOptions: MigrationOptions): GrantOnSchemas {
const _grantOnSchemas: GrantOnSchemas = (options) => {
const { privileges, schemas, roles, withGrantOption = false } = options;
const rolesStr = asRolesStr(roles, mOptions);
const schemasStr = toArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = toArray(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;
}