-
Notifications
You must be signed in to change notification settings - Fork 175
/
createSchema.ts
36 lines (28 loc) · 1.01 KB
/
createSchema.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 {
DropOptions,
IfNotExistsOption,
Reversible,
} from '../generalTypes';
import { dropSchema } from './dropSchema';
export interface CreateSchemaOptions extends IfNotExistsOption {
authorization?: string;
}
export type CreateSchemaFn = (
schemaName: string,
schemaOptions?: CreateSchemaOptions & DropOptions
) => string | string[];
export type CreateSchema = Reversible<CreateSchemaFn>;
export function createSchema(mOptions: MigrationOptions): CreateSchema {
const _create: CreateSchema = (schemaName: string, options = {}) => {
const { ifNotExists, authorization } = options;
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const schemaNameStr = mOptions.literal(schemaName);
const authorizationStr = authorization
? ` AUTHORIZATION ${authorization}`
: '';
return `CREATE SCHEMA${ifNotExistsStr} ${schemaNameStr}${authorizationStr};`;
};
_create.reverse = dropSchema(mOptions);
return _create;
}