Skip to content

Commit

Permalink
feat: remove overloaded pool#connect(); too many edge cases to cover
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 15, 2018
1 parent f00b9de commit fb595bf
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 84 deletions.
47 changes: 0 additions & 47 deletions src/binders/bindIsolatedPoolConnection.js

This file was deleted.

22 changes: 3 additions & 19 deletions src/binders/bindPool.js
Expand Up @@ -22,7 +22,6 @@ import {
query
} from '../connectionMethods';
import createPoolTransaction from '../factories/createPoolTransaction';
import bindIsolatedPoolConnection from './bindIsolatedPoolConnection';
import bindPoolConnection from './bindPoolConnection';

export default (
Expand All @@ -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)),
Expand Down
1 change: 0 additions & 1 deletion 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';
Expand Down
1 change: 0 additions & 1 deletion src/index.js
Expand Up @@ -23,7 +23,6 @@ types.setTypeParser(TIMESTAMP_OID, (value) => {

export type {
DatabaseConnectionType,
DatabaseIsolatedPoolConnectionType,
DatabasePoolConnectionType,
DatabasePoolType,
DatabaseSingleConnectionType,
Expand Down
33 changes: 17 additions & 16 deletions src/types.js
Expand Up @@ -55,7 +55,7 @@ export type DatabaseConfigurationType =
+user?: string
|};

export type DatabaseConnectionType = {|
type CommonQueryMethodsType = {|
+any: QueryAnyFunctionType<*>,
+anyFirst: QueryAnyFirstFunctionType<*>,
+many: QueryManyFunctionType<*>,
Expand All @@ -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<void>,
+transaction: (handler: TransactionFunctionType) => Promise<*>
|};

export type DatabasePoolConnectionType = {|
...DatabaseConnectionType,
...CommonQueryMethodsType,
+release: () => Promise<void>,
+transaction: (handler: TransactionFunctionType) => Promise<*>
|};

export type DatabaseIsolatedPoolConnectionType = {|
...DatabaseConnectionType,
+transaction: (handler: TransactionFunctionType) => Promise<*>
|};

// @todo Document `((connection: DatabaseIsolatedPoolConnectionType) => Promise<void>) => Promise<void>` API.
type DataPoolConnectMethodType =
() => Promise<DatabasePoolConnectionType> &
((connection: DatabaseIsolatedPoolConnectionType) => Promise<void>) => Promise<void>;

export type DatabasePoolType = {|
...DatabaseConnectionType,
+connect: DataPoolConnectMethodType,
...CommonQueryMethodsType,
+connect: () => Promise<DatabasePoolConnectionType>,
+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 = {
Expand Down

0 comments on commit fb595bf

Please sign in to comment.