Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(extensions): reorganize operations extensions #1073

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ export type {
} from './operations/domains';
export type {
CreateExtension,
CreateExtensionFn,
CreateExtensionOptions,
DropExtension,
Extension,
} from './operations/extensionsTypes';
StringExtension,
} from './operations/extensions';
export type {
CreateFunction,
DropFunction,
Expand Down
42 changes: 0 additions & 42 deletions src/operations/extensions.ts

This file was deleted.

37 changes: 37 additions & 0 deletions src/operations/extensions/createExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions, IfNotExistsOption } from '../generalTypes';
import { dropExtension } from './dropExtension';
import type { StringExtension } from './shared';

export interface CreateExtensionOptions extends IfNotExistsOption {
schema?: string;
}

export type CreateExtensionFn = (
extension: StringExtension | StringExtension[],
options?: CreateExtensionOptions & DropOptions
) => string | string[];

export type CreateExtension = CreateExtensionFn & {
reverse: CreateExtensionFn;
};

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;
}
26 changes: 26 additions & 0 deletions src/operations/extensions/dropExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { MigrationOptions } from '../../types';
import type { DropOptions } from '../generalTypes';
import type { StringExtension } from './shared';

export type DropExtension = (
extension: StringExtension | StringExtension[],
dropOptions?: DropOptions
) => string | string[];

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;
}
3 changes: 3 additions & 0 deletions src/operations/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './createExtension';
export * from './dropExtension';
export * from './shared';
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import type {
DropOptions,
IfNotExistsOption,
LiteralUnion,
} from './generalTypes';
import type { LiteralUnion } from '../generalTypes';

export type Extension =
| 'adminpack'
Expand Down Expand Up @@ -51,22 +47,4 @@ export type Extension =
| 'uuid-ossp'
| 'xml2';

export interface CreateExtensionOptions extends IfNotExistsOption {
schema?: string;
}

type StringExtension = LiteralUnion<Extension>;

type CreateExtensionFn = (
extension: StringExtension | StringExtension[],
options?: CreateExtensionOptions & DropOptions
) => string | string[];

export type CreateExtension = CreateExtensionFn & {
reverse: CreateExtensionFn;
};

export type DropExtension = (
extension: StringExtension | StringExtension[],
dropOptions?: DropOptions
) => string | string[];
export type StringExtension = LiteralUnion<Extension>;
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
QueryResult,
} from 'pg';
import type * as domains from './operations/domains';
import type * as extensions from './operations/extensionsTypes';
import type * as extensions from './operations/extensions';
import type * as functions from './operations/functionsTypes';
import type { Name } from './operations/generalTypes';
import type * as indexes from './operations/indexesTypes';
Expand Down