-
Notifications
You must be signed in to change notification settings - Fork 180
/
createExtension.ts
37 lines (28 loc) · 1.26 KB
/
createExtension.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
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { IfNotExistsOption, Reversible } from '../generalTypes';
import type { DropExtensionOptions } from './dropExtension';
import { dropExtension } from './dropExtension';
import type { StringExtension } from './shared';
export interface CreateExtensionOptions extends IfNotExistsOption {
schema?: string;
}
export type CreateExtensionFn = (
extension: StringExtension | StringExtension[],
extensionOptions?: CreateExtensionOptions & DropExtensionOptions
) => string | string[];
export type CreateExtension = Reversible<CreateExtensionFn>;
export function createExtension(mOptions: MigrationOptions): CreateExtension {
const _create: CreateExtension = (_extensions, options = {}) => {
const { ifNotExists = false, schema } = options;
const extensions = toArray(_extensions);
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const schemaStr = schema ? ` SCHEMA ${mOptions.literal(schema)}` : '';
return extensions.map((extension) => {
const extensionStr = mOptions.literal(extension);
return `CREATE EXTENSION${ifNotExistsStr} ${extensionStr}${schemaStr};`;
});
};
_create.reverse = dropExtension(mOptions);
return _create;
}