Skip to content

Commit

Permalink
refactor!: reorganize structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Mar 6, 2024
1 parent 2f7c495 commit 89b60ca
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 170 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type {
DomainOptionsCreate,
DropDomain,
RenameDomain,
} from './operations/domainsTypes';
} from './operations/domains';
export type {
CreateExtension,
CreateExtensionOptions,
Expand Down
124 changes: 0 additions & 124 deletions src/operations/domains.ts

This file was deleted.

49 changes: 49 additions & 0 deletions src/operations/domains/alterDomain.ts
Original file line number Diff line number Diff line change
@@ -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;
}
67 changes: 67 additions & 0 deletions src/operations/domains/createDomain.ts
Original file line number Diff line number Diff line change
@@ -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;
}
21 changes: 21 additions & 0 deletions src/operations/domains/dropDomain.ts
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 16 additions & 0 deletions src/operations/domains/index.ts
Original file line number Diff line number Diff line change
@@ -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';
23 changes: 23 additions & 0 deletions src/operations/domains/renameDomain.ts
Original file line number Diff line number Diff line change
@@ -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;
}
44 changes: 0 additions & 44 deletions src/operations/domainsTypes.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 89b60ca

Please sign in to comment.