From 54db4f0e68766bf00da949a65ff311f79b6d813e Mon Sep 17 00:00:00 2001 From: Raphael Pigulla Date: Mon, 6 May 2024 07:09:01 +0200 Subject: [PATCH] fix: export correct type declarations for addConstraint (#1154) --- src/operations/tables/addConstraint.ts | 30 +++++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/operations/tables/addConstraint.ts b/src/operations/tables/addConstraint.ts index 3fe7f134a..211574cd3 100644 --- a/src/operations/tables/addConstraint.ts +++ b/src/operations/tables/addConstraint.ts @@ -18,23 +18,27 @@ export type CreateConstraintFn2 = ( expression: string ) => string; -export type CreateConstraintFn = CreateConstraintFn1 & CreateConstraintFn2; +export type CreateConstraintFn = CreateConstraintFn1 | CreateConstraintFn2; export type CreateConstraint = Reversible; 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 ); @@ -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; }