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!: reorganize structure #1006

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ export type {
DomainOptionsCreate,
DropDomain,
RenameDomain,
} from './operations/domainsTypes';
} from './operations/domains';
export type {
CreateExtension,
CreateExtensionOptions,
DropExtension,
Extension,
} from './operations/extensionsTypes';
StringExtension,
} from './operations/extensions';
export type {
CreateFunction,
DropFunction,
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 { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import type { Name } from '../generalTypes';
import type { DomainOptions } from './shared';

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 { MigrationOptions } from '../../types';
import { applyType, escapeValue } from '../../utils';
import type { DropOptions, Name, Type } from './../generalTypes';
import { dropDomain } from './dropDomain';
import type { DomainOptions } from './shared';

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;
}
5 changes: 5 additions & 0 deletions src/operations/domains/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export * from './alterDomain';
export * from './createDomain';
export * from './dropDomain';
export * from './renameDomain';
export * from './shared';
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;
}
11 changes: 11 additions & 0 deletions src/operations/domains/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { Value } from '../generalTypes';

export interface DomainOptions {
default?: Value;

notNull?: boolean;

check?: string;

constraintName?: string;
}
44 changes: 0 additions & 44 deletions src/operations/domainsTypes.ts

This file was deleted.

Loading
Loading