From fb595bf1d1fe12385d26250a439bc2e0a31740e9 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Thu, 15 Nov 2018 00:46:03 +0000 Subject: [PATCH] feat: remove overloaded pool#connect(); too many edge cases to cover --- src/binders/bindIsolatedPoolConnection.js | 47 ----------------------- src/binders/bindPool.js | 22 ++--------- src/binders/index.js | 1 - src/index.js | 1 - src/types.js | 33 ++++++++-------- 5 files changed, 20 insertions(+), 84 deletions(-) delete mode 100644 src/binders/bindIsolatedPoolConnection.js diff --git a/src/binders/bindIsolatedPoolConnection.js b/src/binders/bindIsolatedPoolConnection.js deleted file mode 100644 index ae261d9f..00000000 --- a/src/binders/bindIsolatedPoolConnection.js +++ /dev/null @@ -1,47 +0,0 @@ -// @flow - -import { - mapTaggedTemplateLiteralInvocation -} from '../utilities'; -import type { - ClientConfigurationType, - TransactionFunctionType, - DatabaseIsolatedPoolConnectionType, - InternalDatabaseConnectionType, - InternalDatabasePoolType, - LoggerType -} from '../types'; -import { - any, - anyFirst, - many, - manyFirst, - maybeOne, - maybeOneFirst, - one, - oneFirst, - query -} from '../connectionMethods'; -import createPoolTransaction from '../factories/createPoolTransaction'; - -export default ( - parentLog: LoggerType, - pool: InternalDatabasePoolType, - connection: InternalDatabaseConnectionType, - clientConfiguration: ClientConfigurationType -): DatabaseIsolatedPoolConnectionType => { - return { - any: mapTaggedTemplateLiteralInvocation(any.bind(null, parentLog, pool, clientConfiguration)), - anyFirst: mapTaggedTemplateLiteralInvocation(anyFirst.bind(null, parentLog, pool, clientConfiguration)), - many: mapTaggedTemplateLiteralInvocation(many.bind(null, parentLog, pool, clientConfiguration)), - manyFirst: mapTaggedTemplateLiteralInvocation(manyFirst.bind(null, parentLog, pool, clientConfiguration)), - maybeOne: mapTaggedTemplateLiteralInvocation(maybeOne.bind(null, parentLog, pool, clientConfiguration)), - maybeOneFirst: mapTaggedTemplateLiteralInvocation(maybeOneFirst.bind(null, parentLog, pool, clientConfiguration)), - one: mapTaggedTemplateLiteralInvocation(one.bind(null, parentLog, pool, clientConfiguration)), - oneFirst: mapTaggedTemplateLiteralInvocation(oneFirst.bind(null, parentLog, pool, clientConfiguration)), - query: mapTaggedTemplateLiteralInvocation(query.bind(null, parentLog, pool, clientConfiguration)), - transaction: async (handler: TransactionFunctionType) => { - return createPoolTransaction(parentLog, pool, clientConfiguration, handler); - } - }; -}; diff --git a/src/binders/bindPool.js b/src/binders/bindPool.js index c8c5ef9c..fa9a4ffb 100644 --- a/src/binders/bindPool.js +++ b/src/binders/bindPool.js @@ -22,7 +22,6 @@ import { query } from '../connectionMethods'; import createPoolTransaction from '../factories/createPoolTransaction'; -import bindIsolatedPoolConnection from './bindIsolatedPoolConnection'; import bindPoolConnection from './bindPoolConnection'; export default ( @@ -33,25 +32,10 @@ export default ( return { any: mapTaggedTemplateLiteralInvocation(any.bind(null, parentLog, pool, clientConfiguration)), anyFirst: mapTaggedTemplateLiteralInvocation(anyFirst.bind(null, parentLog, pool, clientConfiguration)), + connect: async () => { + const connection: InternalDatabaseConnectionType = await pool.connect(); - // @see https://stackoverflow.com/questions/42539202/flowtype-how-can-i-overload-function-return-types-by-argument-count-types - // eslint-disable-next-line consistent-return, flowtype/no-weak-types - connect: async (handler): any => { - if (handler) { - const connection: InternalDatabaseConnectionType = await pool.connect(); - - try { - await handler(bindIsolatedPoolConnection(parentLog, pool, connection, clientConfiguration)); - } catch (error) { - connection.release(); - - throw error; - } - } else { - const connection: InternalDatabaseConnectionType = await pool.connect(); - - return bindPoolConnection(parentLog, pool, connection, clientConfiguration); - } + return bindPoolConnection(parentLog, pool, connection, clientConfiguration); }, many: mapTaggedTemplateLiteralInvocation(many.bind(null, parentLog, pool, clientConfiguration)), manyFirst: mapTaggedTemplateLiteralInvocation(manyFirst.bind(null, parentLog, pool, clientConfiguration)), diff --git a/src/binders/index.js b/src/binders/index.js index 2d742967..7a3245f4 100644 --- a/src/binders/index.js +++ b/src/binders/index.js @@ -1,6 +1,5 @@ // @flow -export {default as bindIsolatedPoolConnection} from './bindIsolatedPoolConnection'; export {default as bindPool} from './bindPool'; export {default as bindPoolConnection} from './bindPoolConnection'; export {default as bindSingleConnection} from './bindSingleConnection'; diff --git a/src/index.js b/src/index.js index 337d67af..82f39199 100644 --- a/src/index.js +++ b/src/index.js @@ -23,7 +23,6 @@ types.setTypeParser(TIMESTAMP_OID, (value) => { export type { DatabaseConnectionType, - DatabaseIsolatedPoolConnectionType, DatabasePoolConnectionType, DatabasePoolType, DatabaseSingleConnectionType, diff --git a/src/types.js b/src/types.js index e4f59b4f..50c7ea64 100644 --- a/src/types.js +++ b/src/types.js @@ -55,7 +55,7 @@ export type DatabaseConfigurationType = +user?: string |}; -export type DatabaseConnectionType = {| +type CommonQueryMethodsType = {| +any: QueryAnyFunctionType<*>, +anyFirst: QueryAnyFirstFunctionType<*>, +many: QueryManyFunctionType<*>, @@ -68,39 +68,40 @@ export type DatabaseConnectionType = {| |}; export type DatabaseTransactionConnectionType = {| - ...DatabaseConnectionType + ...CommonQueryMethodsType |}; export type TransactionFunctionType = (connection: DatabaseTransactionConnectionType) => Promise<*>; export type DatabaseSingleConnectionType = {| - ...DatabaseConnectionType, + ...CommonQueryMethodsType, +end: () => Promise, +transaction: (handler: TransactionFunctionType) => Promise<*> |}; export type DatabasePoolConnectionType = {| - ...DatabaseConnectionType, + ...CommonQueryMethodsType, +release: () => Promise, +transaction: (handler: TransactionFunctionType) => Promise<*> |}; -export type DatabaseIsolatedPoolConnectionType = {| - ...DatabaseConnectionType, - +transaction: (handler: TransactionFunctionType) => Promise<*> -|}; - -// @todo Document `((connection: DatabaseIsolatedPoolConnectionType) => Promise) => Promise` API. -type DataPoolConnectMethodType = - () => Promise & - ((connection: DatabaseIsolatedPoolConnectionType) => Promise) => Promise; - export type DatabasePoolType = {| - ...DatabaseConnectionType, - +connect: DataPoolConnectMethodType, + ...CommonQueryMethodsType, + +connect: () => Promise, +transaction: (handler: TransactionFunctionType) => Promise<*> |}; +/** + * This appears to be the only sane way to have a generic database connection type + * that can be refined, i.e. DatabaseConnectionType => DatabasePoolType. + */ +export type DatabaseConnectionType = + $Shape<{ + ...DatabasePoolConnectionType, + ...DatabasePoolType, + ...DatabaseSingleConnectionType + }>; + type QueryResultRowColumnType = string | number; export type QueryResultRowType = {