-
Notifications
You must be signed in to change notification settings - Fork 178
/
createPolicy.ts
41 lines (33 loc) · 1.26 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
import type { MigrationOptions } from '../../types';
import type { IfExistsOption, Name } from '../generalTypes';
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,
options?: CreatePolicyOptions & IfExistsOption
) => string | string[];
export type CreatePolicy = CreatePolicyFn & { reverse: CreatePolicyFn };
export function createPolicy(mOptions: MigrationOptions): CreatePolicy {
const _create: CreatePolicy = (tableName, policyName, options = {}) => {
const createOptions = {
...options,
role: options.role || 'PUBLIC',
};
const clauses = [
`FOR ${options.command || 'ALL'}`,
...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;
}