-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathextensions.ts
42 lines (30 loc) · 1.39 KB
/
extensions.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 { CreateExtension, DropExtension } from './extensionsTypes';
export type { CreateExtension, DropExtension };
export function dropExtension(mOptions: MigrationOptions): DropExtension {
const _drop: DropExtension = (_extensions, options = {}) => {
const { ifExists, cascade } = options;
const extensions = Array.isArray(_extensions) ? _extensions : [_extensions];
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
return extensions.map((extension) => {
const extensionStr = mOptions.literal(extension);
return `DROP EXTENSION${ifExistsStr} ${extensionStr}${cascadeStr};`;
});
};
return _drop;
}
export function createExtension(mOptions: MigrationOptions): CreateExtension {
const _create: CreateExtension = (_extensions, options = {}) => {
const { ifNotExists, schema } = options;
const extensions = Array.isArray(_extensions) ? _extensions : [_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;
}