-
Notifications
You must be signed in to change notification settings - Fork 180
/
createPolicy.ts
42 lines (32 loc) · 1.28 KB
/
createPolicy.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
import type { MigrationOptions } from '../../types';
import type { Name, Reversible } from '../generalTypes';
import type { DropPolicyOptions } from './dropPolicy';
import { dropPolicy } from './dropPolicy';
import type { PolicyOptions } from './shared';
import { makeClauses } from './shared';
export interface CreatePolicyOptionsEn {
command?: 'ALL' | 'SELECT' | 'INSERT' | 'UPDATE' | 'DELETE';
}
export type CreatePolicyOptions = CreatePolicyOptionsEn & PolicyOptions;
type CreatePolicyFn = (
tableName: Name,
policyName: string,
policyOptions?: CreatePolicyOptions & DropPolicyOptions
) => string;
export type CreatePolicy = Reversible<CreatePolicyFn>;
export function createPolicy(mOptions: MigrationOptions): CreatePolicy {
const _create: CreatePolicy = (tableName, policyName, options = {}) => {
const { role = 'PUBLIC', command = 'ALL' } = options;
const createOptions = {
...options,
role,
};
const clauses = [`FOR ${command}`, ...makeClauses(createOptions)];
const clausesStr = clauses.join(' ');
const policyNameStr = mOptions.literal(policyName);
const tableNameStr = mOptions.literal(tableName);
return `CREATE POLICY ${policyNameStr} ON ${tableNameStr} ${clausesStr};`;
};
_create.reverse = dropPolicy(mOptions);
return _create;
}