From 89b60caea953feb23077fbb5d08227356cf83bc4 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Tue, 5 Mar 2024 16:14:04 +0100 Subject: [PATCH] refactor!: reorganize structure --- src/index.ts | 2 +- src/operations/domains.ts | 124 ------------------------- src/operations/domains/alterDomain.ts | 49 ++++++++++ src/operations/domains/createDomain.ts | 67 +++++++++++++ src/operations/domains/dropDomain.ts | 21 +++++ src/operations/domains/index.ts | 16 ++++ src/operations/domains/renameDomain.ts | 23 +++++ src/operations/domainsTypes.ts | 44 --------- src/types.ts | 2 +- 9 files changed, 178 insertions(+), 170 deletions(-) delete mode 100644 src/operations/domains.ts create mode 100644 src/operations/domains/alterDomain.ts create mode 100644 src/operations/domains/createDomain.ts create mode 100644 src/operations/domains/dropDomain.ts create mode 100644 src/operations/domains/index.ts create mode 100644 src/operations/domains/renameDomain.ts delete mode 100644 src/operations/domainsTypes.ts diff --git a/src/index.ts b/src/index.ts index f6c52c1e3..57b31aa9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,7 +6,7 @@ export type { DomainOptionsCreate, DropDomain, RenameDomain, -} from './operations/domainsTypes'; +} from './operations/domains'; export type { CreateExtension, CreateExtensionOptions, diff --git a/src/operations/domains.ts b/src/operations/domains.ts deleted file mode 100644 index c0cc341a3..000000000 --- a/src/operations/domains.ts +++ /dev/null @@ -1,124 +0,0 @@ -import type { MigrationOptions } from '../types'; -import { applyType, escapeValue } from '../utils'; -import type { - AlterDomain, - CreateDomain, - DropDomain, - RenameDomain, -} from './domainsTypes'; - -export type { CreateDomain, DropDomain, AlterDomain, RenameDomain }; - -export function dropDomain(mOptions: MigrationOptions): DropDomain { - const _drop: DropDomain = (domainName, options = {}) => { - const { ifExists, cascade } = options; - - const ifExistsStr = ifExists ? ' IF EXISTS' : ''; - const cascadeStr = cascade ? ' CASCADE' : ''; - const domainNameStr = mOptions.literal(domainName); - - return `DROP DOMAIN${ifExistsStr} ${domainNameStr}${cascadeStr};`; - }; - - return _drop; -} - -export function createDomain(mOptions: MigrationOptions): CreateDomain { - const _create: CreateDomain = (domainName, type, options = {}) => { - const { - default: defaultValue, - collation, - notNull, - check, - constraintName, - } = options; - - const constraints: string[] = []; - - if (collation) { - constraints.push(`COLLATE ${collation}`); - } - - if (defaultValue !== undefined) { - constraints.push(`DEFAULT ${escapeValue(defaultValue)}`); - } - - if (notNull && check) { - throw new Error('"notNull" and "check" can\'t be specified together'); - } else if (notNull || check) { - if (constraintName) { - constraints.push(`CONSTRAINT ${mOptions.literal(constraintName)}`); - } - - if (notNull) { - constraints.push('NOT NULL'); - } else if (check) { - constraints.push(`CHECK (${check})`); - } - } - - const constraintsStr = constraints.length - ? ` ${constraints.join(' ')}` - : ''; - - const typeStr = applyType(type, mOptions.typeShorthands).type; - const domainNameStr = mOptions.literal(domainName); - - return `CREATE DOMAIN ${domainNameStr} AS ${typeStr}${constraintsStr};`; - }; - - _create.reverse = (domainName, type, options) => - dropDomain(mOptions)(domainName, options); - - return _create; -} - -export function alterDomain(mOptions: MigrationOptions): AlterDomain { - const _alter: AlterDomain = (domainName, options) => { - const { - default: defaultValue, - notNull, - allowNull, - check, - constraintName, - } = options; - - const actions: string[] = []; - - if (defaultValue === null) { - actions.push('DROP DEFAULT'); - } else if (defaultValue !== undefined) { - actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`); - } - - if (notNull) { - actions.push('SET NOT NULL'); - } else if (notNull === false || allowNull) { - actions.push('DROP NOT NULL'); - } - - if (check) { - actions.push( - `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}CHECK (${check})` - ); - } - - return `${actions.map((action) => `ALTER DOMAIN ${mOptions.literal(domainName)} ${action}`).join(';\n')};`; - }; - - return _alter; -} - -export function renameDomain(mOptions: MigrationOptions): RenameDomain { - const _rename: RenameDomain = (domainName, newDomainName) => { - const domainNameStr = mOptions.literal(domainName); - const newDomainNameStr = mOptions.literal(newDomainName); - - return `ALTER DOMAIN ${domainNameStr} RENAME TO ${newDomainNameStr};`; - }; - - _rename.reverse = (domainName, newDomainName) => - _rename(newDomainName, domainName); - - return _rename; -} diff --git a/src/operations/domains/alterDomain.ts b/src/operations/domains/alterDomain.ts new file mode 100644 index 000000000..30fb3867b --- /dev/null +++ b/src/operations/domains/alterDomain.ts @@ -0,0 +1,49 @@ +import type { DomainOptions } from '.'; +import type { MigrationOptions } from '../../types'; +import { escapeValue } from '../../utils'; +import type { Name } from '../generalTypes'; + +export interface DomainOptionsAlter extends DomainOptions { + allowNull?: boolean; +} + +export type AlterDomain = ( + domainName: Name, + domainOptions: DomainOptionsAlter +) => string | string[]; + +export function alterDomain(mOptions: MigrationOptions): AlterDomain { + const _alter: AlterDomain = (domainName, options) => { + const { + default: defaultValue, + notNull, + allowNull, + check, + constraintName, + } = options; + + const actions: string[] = []; + + if (defaultValue === null) { + actions.push('DROP DEFAULT'); + } else if (defaultValue !== undefined) { + actions.push(`SET DEFAULT ${escapeValue(defaultValue)}`); + } + + if (notNull) { + actions.push('SET NOT NULL'); + } else if (notNull === false || allowNull) { + actions.push('DROP NOT NULL'); + } + + if (check) { + actions.push( + `${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}CHECK (${check})` + ); + } + + return `${actions.map((action) => `ALTER DOMAIN ${mOptions.literal(domainName)} ${action}`).join(';\n')};`; + }; + + return _alter; +} diff --git a/src/operations/domains/createDomain.ts b/src/operations/domains/createDomain.ts new file mode 100644 index 000000000..d5f96fec9 --- /dev/null +++ b/src/operations/domains/createDomain.ts @@ -0,0 +1,67 @@ +import type { DomainOptions } from '.'; +import { dropDomain } from '.'; +import type { MigrationOptions } from '../../types'; +import { applyType, escapeValue } from '../../utils'; +import type { DropOptions, Name, Type } from './../generalTypes'; + +export interface DomainOptionsCreate extends DomainOptions { + collation?: string; +} + +type CreateDomainFn = ( + domainName: Name, + type: Type, + domainOptions?: DomainOptionsCreate & DropOptions +) => string | string[]; + +export type CreateDomain = CreateDomainFn & { reverse: CreateDomainFn }; + +export function createDomain(mOptions: MigrationOptions): CreateDomain { + const _create: CreateDomain = (domainName, type, options = {}) => { + const { + default: defaultValue, + collation, + notNull, + check, + constraintName, + } = options; + + const constraints: string[] = []; + + if (collation) { + constraints.push(`COLLATE ${collation}`); + } + + if (defaultValue !== undefined) { + constraints.push(`DEFAULT ${escapeValue(defaultValue)}`); + } + + if (notNull && check) { + throw new Error('"notNull" and "check" can\'t be specified together'); + } else if (notNull || check) { + if (constraintName) { + constraints.push(`CONSTRAINT ${mOptions.literal(constraintName)}`); + } + + if (notNull) { + constraints.push('NOT NULL'); + } else if (check) { + constraints.push(`CHECK (${check})`); + } + } + + const constraintsStr = constraints.length + ? ` ${constraints.join(' ')}` + : ''; + + const typeStr = applyType(type, mOptions.typeShorthands).type; + const domainNameStr = mOptions.literal(domainName); + + return `CREATE DOMAIN ${domainNameStr} AS ${typeStr}${constraintsStr};`; + }; + + _create.reverse = (domainName, type, options) => + dropDomain(mOptions)(domainName, options); + + return _create; +} diff --git a/src/operations/domains/dropDomain.ts b/src/operations/domains/dropDomain.ts new file mode 100644 index 000000000..5aaf56587 --- /dev/null +++ b/src/operations/domains/dropDomain.ts @@ -0,0 +1,21 @@ +import type { MigrationOptions } from '../../types'; +import type { DropOptions, Name } from './../generalTypes'; + +export type DropDomain = ( + domainName: Name, + dropOptions?: DropOptions +) => string | string[]; + +export function dropDomain(mOptions: MigrationOptions): DropDomain { + const _drop: DropDomain = (domainName, options = {}) => { + const { ifExists, cascade } = options; + + const ifExistsStr = ifExists ? ' IF EXISTS' : ''; + const cascadeStr = cascade ? ' CASCADE' : ''; + const domainNameStr = mOptions.literal(domainName); + + return `DROP DOMAIN${ifExistsStr} ${domainNameStr}${cascadeStr};`; + }; + + return _drop; +} diff --git a/src/operations/domains/index.ts b/src/operations/domains/index.ts new file mode 100644 index 000000000..596c53d4d --- /dev/null +++ b/src/operations/domains/index.ts @@ -0,0 +1,16 @@ +import type { Value } from './../generalTypes'; + +export interface DomainOptions { + default?: Value; + + notNull?: boolean; + + check?: string; + + constraintName?: string; +} + +export * from './alterDomain'; +export * from './createDomain'; +export * from './dropDomain'; +export * from './renameDomain'; diff --git a/src/operations/domains/renameDomain.ts b/src/operations/domains/renameDomain.ts new file mode 100644 index 000000000..7e46734fe --- /dev/null +++ b/src/operations/domains/renameDomain.ts @@ -0,0 +1,23 @@ +import type { MigrationOptions } from '../../types'; +import type { Name } from '../generalTypes'; + +type RenameDomainFn = ( + oldDomainName: Name, + newDomainName: Name +) => string | string[]; + +export type RenameDomain = RenameDomainFn & { reverse: RenameDomainFn }; + +export function renameDomain(mOptions: MigrationOptions): RenameDomain { + const _rename: RenameDomain = (domainName, newDomainName) => { + const domainNameStr = mOptions.literal(domainName); + const newDomainNameStr = mOptions.literal(newDomainName); + + return `ALTER DOMAIN ${domainNameStr} RENAME TO ${newDomainNameStr};`; + }; + + _rename.reverse = (domainName, newDomainName) => + _rename(newDomainName, domainName); + + return _rename; +} diff --git a/src/operations/domainsTypes.ts b/src/operations/domainsTypes.ts deleted file mode 100644 index 21af0b70e..000000000 --- a/src/operations/domainsTypes.ts +++ /dev/null @@ -1,44 +0,0 @@ -import type { DropOptions, Name, Type, Value } from './generalTypes'; - -export interface DomainOptions { - default?: Value; - - notNull?: boolean; - - check?: string; - - constraintName?: string; -} - -export interface DomainOptionsCreate extends DomainOptions { - collation?: string; -} - -export interface DomainOptionsAlter extends DomainOptions { - allowNull?: boolean; -} - -type CreateDomainFn = ( - domainName: Name, - type: Type, - domainOptions?: DomainOptionsCreate & DropOptions -) => string | string[]; - -export type CreateDomain = CreateDomainFn & { reverse: CreateDomainFn }; - -export type DropDomain = ( - domainName: Name, - dropOptions?: DropOptions -) => string | string[]; - -export type AlterDomain = ( - domainName: Name, - domainOptions: DomainOptionsAlter -) => string | string[]; - -type RenameDomainFn = ( - oldDomainName: Name, - newDomainName: Name -) => string | string[]; - -export type RenameDomain = RenameDomainFn & { reverse: RenameDomainFn }; diff --git a/src/types.ts b/src/types.ts index 2fb7765d0..6b298da36 100644 --- a/src/types.ts +++ b/src/types.ts @@ -6,7 +6,7 @@ import type { QueryConfig, QueryResult, } from 'pg'; -import type * as domains from './operations/domainsTypes'; +import type * as domains from './operations/domains'; import type * as extensions from './operations/extensionsTypes'; import type * as functions from './operations/functionsTypes'; import type { Name } from './operations/generalTypes';