-
Notifications
You must be signed in to change notification settings - Fork 180
/
createRole.ts
36 lines (29 loc) · 1.16 KB
/
createRole.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
import type { MigrationOptions } from '../../types';
import type { Name, Reversible } from '../generalTypes';
import type { DropRoleOptions } from './dropRole';
import { dropRole } from './dropRole';
import type { RoleOptions } from './shared';
import { formatRoleOptions } from './shared';
export type CreateRoleOptions = RoleOptions;
export type CreateRoleFn = (
roleName: Name,
roleOptions?: CreateRoleOptions & DropRoleOptions
) => string;
export type CreateRole = Reversible<CreateRoleFn>;
export function createRole(mOptions: MigrationOptions): CreateRole {
const _create: CreateRole = (roleName, roleOptions = {}) => {
const options = formatRoleOptions({
...roleOptions,
superuser: roleOptions.superuser || false,
createdb: roleOptions.createdb || false,
createrole: roleOptions.createrole || false,
inherit: roleOptions.inherit !== false,
login: roleOptions.login || false,
replication: roleOptions.replication || false,
});
const optionsStr = options ? ` WITH ${options}` : '';
return `CREATE ROLE ${mOptions.literal(roleName)}${optionsStr};`;
};
_create.reverse = dropRole(mOptions);
return _create;
}