-
Notifications
You must be signed in to change notification settings - Fork 179
/
createSchema.ts
33 lines (25 loc) · 1.05 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
import type { MigrationOptions } from '../../types';
import type { IfNotExistsOption, Reversible } from '../generalTypes';
import type { DropSchemaOptions } from './dropSchema';
import { dropSchema } from './dropSchema';
export interface CreateSchemaOptions extends IfNotExistsOption {
authorization?: string;
}
export type CreateSchemaFn = (
schemaName: string,
schemaOptions?: CreateSchemaOptions & DropSchemaOptions
) => string;
export type CreateSchema = Reversible<CreateSchemaFn>;
export function createSchema(mOptions: MigrationOptions): CreateSchema {
const _create: CreateSchema = (schemaName: string, options = {}) => {
const { ifNotExists = false, 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;
}