Skip to content

Commit

Permalink
fix: export correct type declarations for addConstraint (#1154)
Browse files Browse the repository at this point in the history
  • Loading branch information
pigulla committed May 6, 2024
1 parent 0dacf09 commit 54db4f0
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/operations/tables/addConstraint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ export type CreateConstraintFn2 = (
expression: string
) => string;

export type CreateConstraintFn = CreateConstraintFn1 & CreateConstraintFn2;
export type CreateConstraintFn = CreateConstraintFn1 | CreateConstraintFn2;

export type CreateConstraint = Reversible<CreateConstraintFn>;

export function addConstraint(mOptions: MigrationOptions): CreateConstraint {
const _add: CreateConstraint = (tableName, constraintName, expression) => {
const _add: CreateConstraint = (
tableName: Name,
constraintName: string | null,
expressionOrOptions: string | (ConstraintOptions & DropConstraintOptions)
) => {
const { constraints, comments } =
typeof expression === 'string'
typeof expressionOrOptions === 'string'
? {
constraints: [
`${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expression}`,
`${constraintName ? `CONSTRAINT ${mOptions.literal(constraintName)} ` : ''}${expressionOrOptions}`,
],
comments: [],
}
: parseConstraints(
tableName,
expression,
expressionOrOptions,
constraintName,
mOptions.literal
);
Expand All @@ -47,21 +51,31 @@ export function addConstraint(mOptions: MigrationOptions): CreateConstraint {
].join('\n');
};

_add.reverse = (tableName, constraintName, options) => {
const reverse: CreateConstraintFn = (
tableName: Name,
constraintName: string | null,
expressionOrOptions: string | (ConstraintOptions & DropConstraintOptions)
) => {
if (constraintName === null) {
throw new Error(
'Impossible to automatically infer down migration for addConstraint without naming constraint'
);
}

if (typeof options === 'string') {
if (typeof expressionOrOptions === 'string') {
throw new Error(
'Impossible to automatically infer down migration for addConstraint with raw SQL expression'
);
}

return dropConstraint(mOptions)(tableName, constraintName, options);
return dropConstraint(mOptions)(
tableName,
constraintName,
expressionOrOptions
);
};

_add.reverse = reverse;

return _add;
}

0 comments on commit 54db4f0

Please sign in to comment.