diff --git a/CHANGELOG.md b/CHANGELOG.md index 6325c08c..468144df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Custom network support -- Option to disable token registery lookups +- Option to disable token registry lookups + +### Fixed + +- Issue with attempting to release an already-released PostgreSQL client during error handling ## [2.3.1] - 2024-11-04 diff --git a/src/routes/accounts/stake-address/addresses/assets.ts b/src/routes/accounts/stake-address/addresses/assets.ts index 9ecc54d9..4d6c9c3a 100644 --- a/src/routes/accounts/stake-address/addresses/assets.ts +++ b/src/routes/accounts/stake-address/addresses/assets.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../../utils/string-utils.js'; import * as ResponseTypes from '../../../../types/responses/accounts.js'; import * as QueryTypes from '../../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/addresses/index.ts b/src/routes/accounts/stake-address/addresses/index.ts index 0c8fde42..12c75117 100644 --- a/src/routes/accounts/stake-address/addresses/index.ts +++ b/src/routes/accounts/stake-address/addresses/index.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../../utils/string-utils.js'; import * as ResponseTypes from '../../../../types/responses/accounts.js'; import * as QueryTypes from '../../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/addresses/total.ts b/src/routes/accounts/stake-address/addresses/total.ts index f34759cb..f79abd8f 100644 --- a/src/routes/accounts/stake-address/addresses/total.ts +++ b/src/routes/accounts/stake-address/addresses/total.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as ResponseTypes from '../../../../types/responses/accounts.js'; import * as QueryTypes from '../../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -39,7 +39,7 @@ async function route(fastify: FastifyInstance) { [request.params.stake_address], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); let result_outputs = []; @@ -85,9 +85,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/delegations.ts b/src/routes/accounts/stake-address/delegations.ts index 92bcdab1..b0357fe5 100644 --- a/src/routes/accounts/stake-address/delegations.ts +++ b/src/routes/accounts/stake-address/delegations.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -53,7 +53,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -69,9 +69,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/history.ts b/src/routes/accounts/stake-address/history.ts index d997dea3..6df790e1 100644 --- a/src/routes/accounts/stake-address/history.ts +++ b/src/routes/accounts/stake-address/history.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -53,7 +53,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -69,9 +69,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/index.ts b/src/routes/accounts/stake-address/index.ts index 41576db3..d29d778b 100644 --- a/src/routes/accounts/stake-address/index.ts +++ b/src/routes/accounts/stake-address/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { request.params.stake_address, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -37,9 +37,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/mirs.ts b/src/routes/accounts/stake-address/mirs.ts index 18b9de82..39b555ca 100644 --- a/src/routes/accounts/stake-address/mirs.ts +++ b/src/routes/accounts/stake-address/mirs.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/registrations.ts b/src/routes/accounts/stake-address/registrations.ts index df10176e..4f65ff4d 100644 --- a/src/routes/accounts/stake-address/registrations.ts +++ b/src/routes/accounts/stake-address/registrations.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/rewards.ts b/src/routes/accounts/stake-address/rewards.ts index 75c22aff..88110a84 100644 --- a/src/routes/accounts/stake-address/rewards.ts +++ b/src/routes/accounts/stake-address/rewards.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/accounts/stake-address/withdrawals.ts b/src/routes/accounts/stake-address/withdrawals.ts index 09e69cff..6cf24cb1 100644 --- a/src/routes/accounts/stake-address/withdrawals.ts +++ b/src/routes/accounts/stake-address/withdrawals.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/accounts.js'; import * as QueryTypes from '../../../types/queries/accounts.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateStakeAddress } from '../../../utils/validation.js'; import { SQLQuery } from '../../../sql/index.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const isStakeAddressValid = validateStakeAddress(request.params.stake_address); if (!isStakeAddressValid) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed stake address format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -52,7 +52,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -68,9 +68,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/extended.ts b/src/routes/addresses/address/extended.ts index 54b4f978..b80a97a5 100644 --- a/src/routes/addresses/address/extended.ts +++ b/src/routes/addresses/address/extended.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/addresses.js'; import * as AssetQueryTypes from '../../../types/queries/assets.js'; import * as ResponseTypes from '../../../types/responses/addresses.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { getSchemaForEndpoint, getOnchainMetadata, @@ -43,7 +43,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } const { rows } = await clientDbSync.query( @@ -126,7 +126,7 @@ async function route(fastify: FastifyInstance) { } } - clientDbSync.release(); + gracefulRelease(clientDbSync); // quantities/amounts are returned as string from database so they won't overflow JS number const result: ResponseTypes.AddressExtended = rows[0].amount @@ -162,9 +162,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/index.ts b/src/routes/addresses/address/index.ts index 8e18c9ab..88ffcab4 100644 --- a/src/routes/addresses/address/index.ts +++ b/src/routes/addresses/address/index.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/addresses.js'; import * as ResponseTypes from '../../../types/responses/addresses.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred, @@ -34,7 +34,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -82,13 +82,11 @@ async function route(fastify: FastifyInstance) { script: rows[0].script, }; - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(result); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/total.ts b/src/routes/addresses/address/total.ts index 94b148c4..51dda0b0 100644 --- a/src/routes/addresses/address/total.ts +++ b/src/routes/addresses/address/total.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/addresses.js'; import * as ResponseTypes from '../../../types/responses/addresses.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred, @@ -34,7 +34,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -43,7 +43,7 @@ async function route(fastify: FastifyInstance) { [request.params.address, paymentCred], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); // if paymentCred is used we have to convert it back to bech32 if (paymentCred) { @@ -96,9 +96,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/transactions.ts b/src/routes/addresses/address/transactions.ts index 5b9395e2..57e9f9f9 100644 --- a/src/routes/addresses/address/transactions.ts +++ b/src/routes/addresses/address/transactions.ts @@ -4,7 +4,7 @@ import { isUnpaged } from '../../../utils/routes.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/addresses.js'; import * as ResponseTypes from '../../../types/responses/addresses.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404, handleInvalidAddress } from '../../../utils/error-handler.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import { getAdditionalParametersFromRequest } from '@blockfrost/blockfrost-utils/lib/fastify.js'; @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -42,7 +42,7 @@ async function route(fastify: FastifyInstance) { ); if (fromToParameters === 'outOfRangeOrMalformedErr') { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom( reply, 'Invalid (malformed or out of range) from/to parameter(s).', @@ -78,7 +78,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -90,9 +90,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/txs.ts b/src/routes/addresses/address/txs.ts index 0b357320..e4d30f0b 100644 --- a/src/routes/addresses/address/txs.ts +++ b/src/routes/addresses/address/txs.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/addresses.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred } from '../../../utils/validation.js'; @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -50,7 +50,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const list: string[] = []; @@ -69,9 +69,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/utxos/asset.ts b/src/routes/addresses/address/utxos/asset.ts index 25bdaba7..f6d47d99 100644 --- a/src/routes/addresses/address/utxos/asset.ts +++ b/src/routes/addresses/address/utxos/asset.ts @@ -7,7 +7,7 @@ import { toJSONStream } from '../../../../utils/string-utils.js'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/addresses.js'; import * as ResponseTypes from '../../../../types/responses/addresses.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred } from '../../../../utils/validation.js'; @@ -39,7 +39,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_address.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -50,7 +50,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_asset.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } } @@ -73,7 +73,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const result: ResponseTypes.AddressUtxos = []; @@ -119,9 +119,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/addresses/address/utxos/index.ts b/src/routes/addresses/address/utxos/index.ts index f3cb0694..b9a5bd1c 100644 --- a/src/routes/addresses/address/utxos/index.ts +++ b/src/routes/addresses/address/utxos/index.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../../utils/string-utils.js'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/addresses.js'; import * as ResponseTypes from '../../../../types/responses/addresses.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred } from '../../../../utils/validation.js'; @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -50,7 +50,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const result: ResponseTypes.AddressUtxos = []; @@ -95,9 +95,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/asset/addresses.ts b/src/routes/assets/asset/addresses.ts index 1ced3eb4..4e6b1499 100644 --- a/src/routes/assets/asset/addresses.ts +++ b/src/routes/assets/asset/addresses.ts @@ -4,7 +4,7 @@ import * as ResponseTypes from '../../../types/responses/assets.js'; import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { validateAsset } from '@blockfrost/blockfrost-utils/lib/validation.js'; @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -46,7 +46,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.asset], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -62,9 +62,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/asset/history.ts b/src/routes/assets/asset/history.ts index 4326bd70..2b0d4bc1 100644 --- a/src/routes/assets/asset/history.ts +++ b/src/routes/assets/asset/history.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import * as QueryTypes from '../../../types/queries/assets.js'; import * as ResponseTypes from '../../../types/responses/assets.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { validateAsset } from '@blockfrost/blockfrost-utils/lib/validation.js'; @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -46,7 +46,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.asset], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -58,9 +58,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/asset/index.ts b/src/routes/assets/asset/index.ts index b042fc57..58c76392 100644 --- a/src/routes/assets/asset/index.ts +++ b/src/routes/assets/asset/index.ts @@ -13,7 +13,7 @@ import { AssetFingerprint } from '../../../utils/cip14.js'; import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/assets.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { fetchAssetMetadata } from '../../../utils/token-registry.js'; @@ -37,7 +37,7 @@ async function route(fastify: FastifyInstance) { ]); if (rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -75,7 +75,7 @@ async function route(fastify: FastifyInstance) { } } - clientDbSync.release(); + gracefulRelease(clientDbSync); if (!onchainMetadata) { // validate CIP25 on-chain metadata if CIP68 metadata are not present (or not valid) @@ -107,9 +107,7 @@ async function route(fastify: FastifyInstance) { fingerprint, }); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/asset/transactions.ts b/src/routes/assets/asset/transactions.ts index 0902b3f6..2ffb9160 100644 --- a/src/routes/assets/asset/transactions.ts +++ b/src/routes/assets/asset/transactions.ts @@ -11,7 +11,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/assets.js'; import * as ResponseTypes from '../../../types/responses/assets.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -44,7 +44,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -59,7 +59,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.asset], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -71,9 +71,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/asset/txs.ts b/src/routes/assets/asset/txs.ts index 377fc55a..a8bf09a4 100644 --- a/src/routes/assets/asset/txs.ts +++ b/src/routes/assets/asset/txs.ts @@ -6,7 +6,7 @@ import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/assets.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -47,7 +47,7 @@ async function route(fastify: FastifyInstance) { request.params.asset, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); const list: string[] = []; @@ -64,9 +64,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/index.ts b/src/routes/assets/index.ts index 08544518..81e5581a 100644 --- a/src/routes/assets/index.ts +++ b/src/routes/assets/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../types/queries/assets.js'; import * as ResponseTypes from '../../types/responses/assets.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { SQLQuery } from '../../sql/index.js'; import { isUnpaged } from '../../utils/routes.js'; import { toJSONStream } from '../../utils/string-utils.js'; @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -43,9 +43,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/assets/policy/policy-id.ts b/src/routes/assets/policy/policy-id.ts index 573f164c..77c32561 100644 --- a/src/routes/assets/policy/policy-id.ts +++ b/src/routes/assets/policy/policy-id.ts @@ -7,7 +7,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/assets.js'; import * as ResponseTypes from '../../../types/responses/assets.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -48,7 +48,7 @@ async function route(fastify: FastifyInstance) { request.params.policy_id, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -64,9 +64,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/epoch/epoch-number/slot/slot-number.ts b/src/routes/blocks/epoch/epoch-number/slot/slot-number.ts index 634c6e50..2e891967 100644 --- a/src/routes/blocks/epoch/epoch-number/slot/slot-number.ts +++ b/src/routes/blocks/epoch/epoch-number/slot/slot-number.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../../../sql/index.js'; import * as QueryTypes from '../../../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../../../types/responses/blocks.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../../../utils/validation.js'; @@ -18,12 +18,12 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.epoch_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } if (!validatePositiveInRangeSignedInt(request.params.slot_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed slot_number.'); } @@ -33,7 +33,7 @@ async function route(fastify: FastifyInstance) { [request.params.epoch_number, request.params.slot_number], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -42,9 +42,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/hash-or-number/addresses.ts b/src/routes/blocks/hash-or-number/addresses.ts index 6342d6bf..5459d95c 100644 --- a/src/routes/blocks/hash-or-number/addresses.ts +++ b/src/routes/blocks/hash-or-number/addresses.ts @@ -1,5 +1,5 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; @@ -24,12 +24,12 @@ async function route(fastify: FastifyInstance) { try { if (isNumber(request.params.hash_or_number)) { if (!validatePositiveInRangeSignedInt(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed block number.'); } } else { if (!validateBlockHash(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing or malformed block hash.'); } } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -55,7 +55,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash_or_number, request.query.count, request.query.page], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -71,9 +71,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/hash-or-number/index.ts b/src/routes/blocks/hash-or-number/index.ts index c81f7b86..7ee9580c 100644 --- a/src/routes/blocks/hash-or-number/index.ts +++ b/src/routes/blocks/hash-or-number/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { @@ -22,12 +22,12 @@ async function route(fastify: FastifyInstance) { try { if (isNumber(request.params.hash_or_number)) { if (!validatePositiveInRangeSignedInt(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed block number.'); } } else { if (!validateBlockHash(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing or malformed block hash.'); } } @@ -37,7 +37,7 @@ async function route(fastify: FastifyInstance) { request.params.hash_or_number, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -46,9 +46,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/hash-or-number/next.ts b/src/routes/blocks/hash-or-number/next.ts index 590a1463..45f27789 100644 --- a/src/routes/blocks/hash-or-number/next.ts +++ b/src/routes/blocks/hash-or-number/next.ts @@ -3,7 +3,7 @@ import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import * as QueryTypes from '../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { @@ -24,12 +24,12 @@ async function route(fastify: FastifyInstance) { try { if (isNumber(request.params.hash_or_number)) { if (!validatePositiveInRangeSignedInt(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed block number.'); } } else { if (!validateBlockHash(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing or malformed block hash.'); } } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -56,7 +56,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -72,9 +72,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/hash-or-number/previous.ts b/src/routes/blocks/hash-or-number/previous.ts index 84c14016..d5fea1f8 100644 --- a/src/routes/blocks/hash-or-number/previous.ts +++ b/src/routes/blocks/hash-or-number/previous.ts @@ -3,7 +3,7 @@ import * as QueryTypes from '../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { @@ -24,12 +24,12 @@ async function route(fastify: FastifyInstance) { try { if (isNumber(request.params.hash_or_number)) { if (!validatePositiveInRangeSignedInt(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed block number.'); } } else { if (!validateBlockHash(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing or malformed block hash.'); } } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -55,7 +55,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash_or_number, request.query.count, request.query.page], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -71,9 +71,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/hash-or-number/txs.ts b/src/routes/blocks/hash-or-number/txs.ts index d3f0ef27..1866ca0d 100644 --- a/src/routes/blocks/hash-or-number/txs.ts +++ b/src/routes/blocks/hash-or-number/txs.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { isNumber, @@ -23,12 +23,12 @@ async function blocks(fastify: FastifyInstance) { try { if (isNumber(request.params.hash_or_number)) { if (!validatePositiveInRangeSignedInt(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed block number.'); } } else { if (!validateBlockHash(request.params.hash_or_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing or malformed block hash.'); } } @@ -39,7 +39,7 @@ async function blocks(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -59,7 +59,7 @@ async function blocks(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -81,9 +81,7 @@ async function blocks(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/latest/index.ts b/src/routes/blocks/latest/index.ts index 2b08587b..589d0e5c 100644 --- a/src/routes/blocks/latest/index.ts +++ b/src/routes/blocks/latest/index.ts @@ -4,7 +4,7 @@ import { FastifyInstance } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -18,7 +18,7 @@ async function route(fastify: FastifyInstance) { try { const { rows } = await clientDbSync.query(SQLQuery.get('blocks_latest')); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -36,9 +36,7 @@ async function route(fastify: FastifyInstance) { return reply.send(response); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/latest/txs.ts b/src/routes/blocks/latest/txs.ts index 0d068167..03e23fcc 100644 --- a/src/routes/blocks/latest/txs.ts +++ b/src/routes/blocks/latest/txs.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; async function route(fastify: FastifyInstance) { fastify.route({ @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -49,9 +49,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/blocks/slot/slot-number.ts b/src/routes/blocks/slot/slot-number.ts index d6920d1f..9dd07831 100644 --- a/src/routes/blocks/slot/slot-number.ts +++ b/src/routes/blocks/slot/slot-number.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/blocks.js'; import * as ResponseTypes from '../../../types/responses/blocks.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../utils/validation.js'; @@ -18,7 +18,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.slot_number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed slot_number.'); } @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { request.params.slot_number, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -36,9 +36,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/latest/index.ts b/src/routes/epochs/latest/index.ts index 855fa83f..76c47375 100644 --- a/src/routes/epochs/latest/index.ts +++ b/src/routes/epochs/latest/index.ts @@ -5,7 +5,7 @@ import { getConfig } from '../../../config.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { const { rows }: { rows: ResponseTypes.Epoch[] } = await clientDbSync.query(SQLQuery.get('epochs_latest'), [epochLength]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -29,9 +29,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/latest/parameters.ts b/src/routes/epochs/latest/parameters.ts index a5fd8ce6..297d5c22 100644 --- a/src/routes/epochs/latest/parameters.ts +++ b/src/routes/epochs/latest/parameters.ts @@ -4,7 +4,7 @@ import { FastifyInstance } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { costModelsMap } from '../../../utils/cost-models-map.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { SQLQuery.get('epochs_latest_parameters'), ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -35,9 +35,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/blocks/index.ts b/src/routes/epochs/number/blocks/index.ts index df6f215c..de48dedb 100644 --- a/src/routes/epochs/number/blocks/index.ts +++ b/src/routes/epochs/number/blocks/index.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../../../utils/string-utils.js'; import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/epochs.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handle400Custom } from '../../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../../utils/validation.js'; @@ -18,7 +18,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -28,7 +28,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -45,7 +45,7 @@ async function route(fastify: FastifyInstance) { request.params.number, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -67,9 +67,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/blocks/pool-id.ts b/src/routes/epochs/number/blocks/pool-id.ts index b10ff7d2..9d76d035 100644 --- a/src/routes/epochs/number/blocks/pool-id.ts +++ b/src/routes/epochs/number/blocks/pool-id.ts @@ -8,7 +8,7 @@ import { } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/epochs.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handle400Custom } from '../../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_epoch.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -39,7 +39,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -49,7 +49,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -70,7 +70,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -92,9 +92,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/index.ts b/src/routes/epochs/number/index.ts index 7a302d54..b8310e57 100644 --- a/src/routes/epochs/number/index.ts +++ b/src/routes/epochs/number/index.ts @@ -5,7 +5,7 @@ import { getConfig } from '../../../config.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../utils/validation.js'; @@ -19,7 +19,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { epochLength, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/next.ts b/src/routes/epochs/number/next.ts index a444caad..1db21b2f 100644 --- a/src/routes/epochs/number/next.ts +++ b/src/routes/epochs/number/next.ts @@ -6,7 +6,7 @@ import { getConfig } from '../../../config.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404, handle400Custom } from '../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../utils/validation.js'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -49,7 +49,7 @@ async function route(fastify: FastifyInstance) { epochLength, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -65,9 +65,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/parameters.ts b/src/routes/epochs/number/parameters.ts index 08432d58..17fc30e1 100644 --- a/src/routes/epochs/number/parameters.ts +++ b/src/routes/epochs/number/parameters.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { costModelsMap } from '../../../utils/cost-models-map.js'; import { validatePositiveInRangeSignedInt } from '../../../utils/validation.js'; @@ -19,7 +19,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { [request.params.number], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -42,9 +42,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/previous.ts b/src/routes/epochs/number/previous.ts index b61740ad..aedc3fa9 100644 --- a/src/routes/epochs/number/previous.ts +++ b/src/routes/epochs/number/previous.ts @@ -7,7 +7,7 @@ import { getConfig } from '../../../config.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../types/responses/epochs.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../utils/validation.js'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -50,7 +50,7 @@ async function route(fastify: FastifyInstance) { epochLength, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -66,9 +66,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/stakes/index.ts b/src/routes/epochs/number/stakes/index.ts index 898709d5..3f918930 100644 --- a/src/routes/epochs/number/stakes/index.ts +++ b/src/routes/epochs/number/stakes/index.ts @@ -5,7 +5,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../../types/responses/epochs.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { validatePositiveInRangeSignedInt } from '../../../../utils/validation.js'; @@ -19,7 +19,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -45,7 +45,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -61,9 +61,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/epochs/number/stakes/pool-id.ts b/src/routes/epochs/number/stakes/pool-id.ts index 1cb667b5..26a0ab3f 100644 --- a/src/routes/epochs/number/stakes/pool-id.ts +++ b/src/routes/epochs/number/stakes/pool-id.ts @@ -5,7 +5,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../../sql/index.js'; import * as QueryTypes from '../../../../types/queries/epochs.js'; import * as ResponseTypes from '../../../../types/responses/epochs.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { validateAndConvertPool, @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedInt(request.params.number)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed epoch_number.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_epoch.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -50,7 +50,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -65,7 +65,7 @@ async function route(fastify: FastifyInstance) { [request.params.number, request.query.count, request.query.page, pool_id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -81,9 +81,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/drep-id/delegators.ts b/src/routes/governance/dreps/drep-id/delegators.ts index d3dd7442..34a83f57 100644 --- a/src/routes/governance/dreps/drep-id/delegators.ts +++ b/src/routes/governance/dreps/drep-id/delegators.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../../utils/routes.js'; @@ -43,13 +43,11 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/drep-id/index.ts b/src/routes/governance/dreps/drep-id/index.ts index 42996183..df62ee76 100644 --- a/src/routes/governance/dreps/drep-id/index.ts +++ b/src/routes/governance/dreps/drep-id/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { [drepValidation.raw, drepValidation.id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; if (!row) { @@ -39,9 +39,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/drep-id/metadata.ts b/src/routes/governance/dreps/drep-id/metadata.ts index f2ff504d..faf6193c 100644 --- a/src/routes/governance/dreps/drep-id/metadata.ts +++ b/src/routes/governance/dreps/drep-id/metadata.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { [drepValidation.raw, drepValidation.id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -40,9 +40,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/drep-id/updates.ts b/src/routes/governance/dreps/drep-id/updates.ts index 47b62b4b..de75eedd 100644 --- a/src/routes/governance/dreps/drep-id/updates.ts +++ b/src/routes/governance/dreps/drep-id/updates.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../../utils/routes.js'; @@ -43,13 +43,11 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/drep-id/votes.ts b/src/routes/governance/dreps/drep-id/votes.ts index 9d80c9bb..871f7975 100644 --- a/src/routes/governance/dreps/drep-id/votes.ts +++ b/src/routes/governance/dreps/drep-id/votes.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../../utils/routes.js'; @@ -43,13 +43,11 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/dreps/index.ts b/src/routes/governance/dreps/index.ts index 2fd4dd23..eddd296a 100644 --- a/src/routes/governance/dreps/index.ts +++ b/src/routes/governance/dreps/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/governance.js'; import * as ResponseTypes from '../../../types/responses/governance.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../utils/routes.js'; @@ -26,13 +26,11 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/index.ts b/src/routes/governance/proposals/index.ts index 09d2f51f..f82734c8 100644 --- a/src/routes/governance/proposals/index.ts +++ b/src/routes/governance/proposals/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/governance.js'; import * as ResponseTypes from '../../../types/responses/governance.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../utils/routes.js'; @@ -28,13 +28,11 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/tx-hash/cert-index/index.ts b/src/routes/governance/proposals/tx-hash/cert-index/index.ts index 83e45d7e..f71c94c9 100644 --- a/src/routes/governance/proposals/tx-hash/cert-index/index.ts +++ b/src/routes/governance/proposals/tx-hash/cert-index/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { SQLQuery } from '../../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { handle404 } from '../../../../../utils/error-handler.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { [request.params.tx_hash, request.params.cert_index], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; if (!row) { @@ -30,9 +30,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/tx-hash/cert-index/metadata.ts b/src/routes/governance/proposals/tx-hash/cert-index/metadata.ts index ab8a0c3f..0cd44815 100644 --- a/src/routes/governance/proposals/tx-hash/cert-index/metadata.ts +++ b/src/routes/governance/proposals/tx-hash/cert-index/metadata.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { handle404 } from '../../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { [request.params.tx_hash, request.params.cert_index], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -30,9 +30,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/tx-hash/cert-index/parameters.ts b/src/routes/governance/proposals/tx-hash/cert-index/parameters.ts index bffbafcd..4378274a 100644 --- a/src/routes/governance/proposals/tx-hash/cert-index/parameters.ts +++ b/src/routes/governance/proposals/tx-hash/cert-index/parameters.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { handle404 } from '../../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { [request.params.tx_hash, request.params.cert_index], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const row = rows[0]; @@ -30,9 +30,7 @@ async function route(fastify: FastifyInstance) { } return reply.send(row); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/tx-hash/cert-index/votes.ts b/src/routes/governance/proposals/tx-hash/cert-index/votes.ts index 6ce25950..82d8f4aa 100644 --- a/src/routes/governance/proposals/tx-hash/cert-index/votes.ts +++ b/src/routes/governance/proposals/tx-hash/cert-index/votes.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { SQLQuery } from '../../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../../../utils/routes.js'; @@ -33,13 +33,11 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/governance/proposals/tx-hash/cert-index/withdrawals.ts b/src/routes/governance/proposals/tx-hash/cert-index/withdrawals.ts index d88c04f0..621a506d 100644 --- a/src/routes/governance/proposals/tx-hash/cert-index/withdrawals.ts +++ b/src/routes/governance/proposals/tx-hash/cert-index/withdrawals.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/governance.js'; import * as ResponseTypes from '../../../../../types/responses/governance.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { SQLQuery } from '../../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../../../../utils/routes.js'; @@ -33,13 +33,11 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/metadata/txs/labels.ts b/src/routes/metadata/txs/labels.ts index 98f032cf..fceefbf6 100644 --- a/src/routes/metadata/txs/labels.ts +++ b/src/routes/metadata/txs/labels.ts @@ -3,7 +3,7 @@ import { isUnpaged } from '../../../utils/routes.js'; import { toJSONStream } from '../../../utils/string-utils.js'; import * as QueryTypes from '../../../types/queries/metadata.js'; import * as ResponseTypes from '../../../types/responses/metadata.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -43,9 +43,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/metadata/txs/labels/label/cbor.ts b/src/routes/metadata/txs/labels/label/cbor.ts index 873a69b0..0c009ac4 100644 --- a/src/routes/metadata/txs/labels/label/cbor.ts +++ b/src/routes/metadata/txs/labels/label/cbor.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/metadata.js'; import * as ResponseTypes from '../../../../../types/responses/metadata.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../../utils/error-handler.js'; import { validatePositiveInRangeSignedBigInt } from '../../../../../utils/validation.js'; import { SQLQuery } from '../../../../../sql/index.js'; @@ -19,7 +19,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedBigInt(request.params.label)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed label.'); } @@ -34,7 +34,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.label], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -50,9 +50,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/metadata/txs/labels/label/index.ts b/src/routes/metadata/txs/labels/label/index.ts index e3762c4e..30a5760a 100644 --- a/src/routes/metadata/txs/labels/label/index.ts +++ b/src/routes/metadata/txs/labels/label/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../../types/queries/metadata.js'; import * as ResponseTypes from '../../../../../types/responses/metadata.js'; -import { getDbSync } from '../../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../../../utils/error-handler.js'; import { validatePositiveInRangeSignedBigInt } from '../../../../../utils/validation.js'; import { SQLQuery } from '../../../../../sql/index.js'; @@ -19,7 +19,7 @@ async function route(fastify: FastifyInstance) { try { if (!validatePositiveInRangeSignedBigInt(request.params.label)) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Missing, out of range or malformed label.'); } @@ -34,7 +34,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.label], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -50,9 +50,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/network/eras.ts b/src/routes/network/eras.ts index af755e4b..20787470 100644 --- a/src/routes/network/eras.ts +++ b/src/routes/network/eras.ts @@ -8,7 +8,7 @@ import { ByronEraParameters } from '../../types/common.js'; import { Block } from '../../types/queries/blocks.js'; import * as QueryTypes from '../../types/queries/network.js'; import * as LedgerResponseTypes from '../../types/responses/ledger.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { handle500 } from '../../utils/error-handler.js'; import { standardSafeZone } from '../../utils/routes.js'; @@ -33,7 +33,7 @@ async function route(fastify: FastifyInstance) { SQLQuery.get('network_protocols'), ); - clientDbSync.release(); + gracefulRelease(clientDbSync); // First summary item is Byron era parameters const first = { @@ -145,9 +145,7 @@ async function route(fastify: FastifyInstance) { return reply.send(summary); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/network/index.ts b/src/routes/network/index.ts index 63b5d630..28f6f6a1 100644 --- a/src/routes/network/index.ts +++ b/src/routes/network/index.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../sql/index.js'; import * as QueryTypes from '../../types/queries/network.js'; import * as ResponseTypes from '../../types/responses/network.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; async function route(fastify: FastifyInstance) { fastify.route({ @@ -18,12 +18,10 @@ async function route(fastify: FastifyInstance) { const { rows }: { rows: ResponseTypes.Network[] } = await clientDbSync.query(SQLQuery.get('network')); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/nutlink/address/index.ts b/src/routes/nutlink/address/index.ts index 795ad200..d4fb0105 100644 --- a/src/routes/nutlink/address/index.ts +++ b/src/routes/nutlink/address/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/nutlink.js'; import * as ResponseTypes from '../../../types/responses/nutlink.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import axios from 'axios'; import * as Sentry from '@sentry/node'; import { handle404, handleInvalidAddress } from '../../../utils/error-handler.js'; @@ -34,7 +34,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -44,7 +44,7 @@ async function route(fastify: FastifyInstance) { paymentCred, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); // if paymentCred is used we have to convert it back to bech32 if (paymentCred) { @@ -78,9 +78,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/nutlink/address/tickers/index.ts b/src/routes/nutlink/address/tickers/index.ts index a722238a..3e48fde7 100644 --- a/src/routes/nutlink/address/tickers/index.ts +++ b/src/routes/nutlink/address/tickers/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/nutlink.js'; import * as ResponseTypes from '../../../../types/responses/nutlink.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -51,7 +51,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -63,9 +63,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/nutlink/address/tickers/ticker.ts b/src/routes/nutlink/address/tickers/ticker.ts index 5bf0c3da..fec46427 100644 --- a/src/routes/nutlink/address/tickers/ticker.ts +++ b/src/routes/nutlink/address/tickers/ticker.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/nutlink.js'; import * as ResponseTypes from '../../../../types/responses/nutlink.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404, handleInvalidAddress } from '../../../../utils/error-handler.js'; import { getAddressTypeAndPaymentCred } from '../../../../utils/validation.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_address.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_ticker.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -62,7 +62,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -74,9 +74,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/nutlink/tickers/ticker.ts b/src/routes/nutlink/tickers/ticker.ts index 638f7a59..a97b9ae4 100644 --- a/src/routes/nutlink/tickers/ticker.ts +++ b/src/routes/nutlink/tickers/ticker.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/nutlink.js'; import * as ResponseTypes from '../../../types/responses/nutlink.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -23,7 +23,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -38,7 +38,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, request.params.ticker], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (unpaged) { // Use of Reply.raw functions is at your own risk as you are skipping all the Fastify logic of handling the HTTP response @@ -50,9 +50,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/extended.ts b/src/routes/pools/extended.ts index a8f09144..1a731605 100644 --- a/src/routes/pools/extended.ts +++ b/src/routes/pools/extended.ts @@ -3,7 +3,7 @@ import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { SQLQuery } from '../../sql/index.js'; import * as QueryTypes from '../../types/queries/pools.js'; import * as ResponseTypes from '../../types/responses/pools.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { isUnpaged } from '../../utils/routes.js'; import { toJSONStream } from '../../utils/string-utils.js'; @@ -28,7 +28,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -44,9 +44,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/index.ts b/src/routes/pools/index.ts index 6991ab08..c38c077c 100644 --- a/src/routes/pools/index.ts +++ b/src/routes/pools/index.ts @@ -3,7 +3,7 @@ import { isUnpaged } from '../../utils/routes.js'; import { toJSONStream } from '../../utils/string-utils.js'; import { SQLQuery } from '../../sql/index.js'; import * as QueryTypes from '../../types/queries/pools.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; async function pools(fastify: FastifyInstance) { @@ -26,7 +26,7 @@ async function pools(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -48,9 +48,7 @@ async function pools(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/blocks.ts b/src/routes/pools/pool-id/blocks.ts index 0bde487d..1ba350a1 100644 --- a/src/routes/pools/pool-id/blocks.ts +++ b/src/routes/pools/pool-id/blocks.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -49,7 +49,7 @@ async function route(fastify: FastifyInstance) { pool_id, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -71,9 +71,7 @@ async function route(fastify: FastifyInstance) { return reply.send(list); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/delegators.ts b/src/routes/pools/pool-id/delegators.ts index e112b109..fbf36803 100644 --- a/src/routes/pools/pool-id/delegators.ts +++ b/src/routes/pools/pool-id/delegators.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -47,7 +47,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, pool_id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -63,9 +63,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/history.ts b/src/routes/pools/pool-id/history.ts index 710a93e8..6b7b7777 100644 --- a/src/routes/pools/pool-id/history.ts +++ b/src/routes/pools/pool-id/history.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -47,7 +47,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, pool_id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -63,9 +63,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/index.ts b/src/routes/pools/pool-id/index.ts index db14ae9c..c0d8320a 100644 --- a/src/routes/pools/pool-id/index.ts +++ b/src/routes/pools/pool-id/index.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -21,14 +21,14 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } const { rows }: { rows: ResponseTypes.PoolID[] } = await clientDbSync.query(SQLQuery.get('pools_pool_id'), [pool_id]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -36,9 +36,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/metadata.ts b/src/routes/pools/pool-id/metadata.ts index b7344e9c..a404bf1e 100644 --- a/src/routes/pools/pool-id/metadata.ts +++ b/src/routes/pools/pool-id/metadata.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -41,11 +41,11 @@ async function route(fastify: FastifyInstance) { ); if (rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send({}); } - clientDbSync.release(); + gracefulRelease(clientDbSync); const poolMetadataText = rows[0].metadata_text; const poolMetadataTextChecked = @@ -76,9 +76,7 @@ async function route(fastify: FastifyInstance) { return reply.send(response); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/relays.ts b/src/routes/pools/pool-id/relays.ts index ad3426b8..356bfcc2 100644 --- a/src/routes/pools/pool-id/relays.ts +++ b/src/routes/pools/pool-id/relays.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -40,7 +40,7 @@ async function route(fastify: FastifyInstance) { pool_id, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -48,9 +48,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/updates.ts b/src/routes/pools/pool-id/updates.ts index 2066baa7..914e2e26 100644 --- a/src/routes/pools/pool-id/updates.ts +++ b/src/routes/pools/pool-id/updates.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -47,7 +47,7 @@ async function route(fastify: FastifyInstance) { [request.query.order, request.query.count, request.query.page, pool_id], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -63,9 +63,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/pool-id/votes.ts b/src/routes/pools/pool-id/votes.ts index 60235e7e..42967ae6 100644 --- a/src/routes/pools/pool-id/votes.ts +++ b/src/routes/pools/pool-id/votes.ts @@ -5,7 +5,7 @@ import { toJSONStream } from '../../../utils/string-utils.js'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/pools.js'; import * as ResponseTypes from '../../../types/responses/pools.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle400Custom, handle404 } from '../../../utils/error-handler.js'; import { validateAndConvertPool } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { const pool_id = validateAndConvertPool(request.params.pool_id); if (!pool_id) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle400Custom(reply, 'Invalid or malformed pool id format.'); } @@ -32,7 +32,7 @@ async function route(fastify: FastifyInstance) { ); if (query404_pool.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -49,7 +49,7 @@ async function route(fastify: FastifyInstance) { pool_id, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -65,9 +65,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/retired.ts b/src/routes/pools/retired.ts index 9348599f..fe19b33b 100644 --- a/src/routes/pools/retired.ts +++ b/src/routes/pools/retired.ts @@ -4,7 +4,7 @@ import { toJSONStream } from '../../utils/string-utils.js'; import { SQLQuery } from '../../sql/index.js'; import * as QueryTypes from '../../types/queries/pools.js'; import * as ResponseTypes from '../../types/responses/pools.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; async function route(fastify: FastifyInstance) { @@ -28,7 +28,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -44,9 +44,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/pools/retiring.ts b/src/routes/pools/retiring.ts index e0109da9..ecfbca1c 100644 --- a/src/routes/pools/retiring.ts +++ b/src/routes/pools/retiring.ts @@ -3,7 +3,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../sql/index.js'; import * as QueryTypes from '../../types/queries/pools.js'; import * as ResponseTypes from '../../types/responses/pools.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { isUnpaged } from '../../utils/routes.js'; import { toJSONStream } from '../../utils/string-utils.js'; @@ -28,7 +28,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -44,9 +44,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/datum/datum-hash/cbor.ts b/src/routes/scripts/datum/datum-hash/cbor.ts index 0ec4a478..34d4ef4b 100644 --- a/src/routes/scripts/datum/datum-hash/cbor.ts +++ b/src/routes/scripts/datum/datum-hash/cbor.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../../types/responses/scripts.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -21,7 +21,7 @@ async function network(fastify: FastifyInstance) { [request.params.datum_hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -29,9 +29,7 @@ async function network(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/datum/datum-hash/index.ts b/src/routes/scripts/datum/datum-hash/index.ts index 5f2ab2f6..a5e3b1af 100644 --- a/src/routes/scripts/datum/datum-hash/index.ts +++ b/src/routes/scripts/datum/datum-hash/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../../types/responses/scripts.js'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -20,7 +20,7 @@ async function network(fastify: FastifyInstance) { request.params.datum_hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -28,9 +28,7 @@ async function network(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/index.ts b/src/routes/scripts/index.ts index d64207b7..8b93067c 100644 --- a/src/routes/scripts/index.ts +++ b/src/routes/scripts/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../types/queries/scripts.js'; import * as ResponseTypes from '../../types/responses/scripts.js'; -import { getDbSync } from '../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../utils/database.js'; import { SQLQuery } from '../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; import { isUnpaged } from '../../utils/routes.js'; @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { request.query.page, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -43,9 +43,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/script_hash/cbor.ts b/src/routes/scripts/script_hash/cbor.ts index c6ce65f2..de1fd9e6 100644 --- a/src/routes/scripts/script_hash/cbor.ts +++ b/src/routes/scripts/script_hash/cbor.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../types/responses/scripts.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { [request.params.script_hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -29,9 +29,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/script_hash/index.ts b/src/routes/scripts/script_hash/index.ts index d8e2debc..650395d9 100644 --- a/src/routes/scripts/script_hash/index.ts +++ b/src/routes/scripts/script_hash/index.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../types/responses/scripts.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { request.params.script_hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -28,9 +28,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/script_hash/json.ts b/src/routes/scripts/script_hash/json.ts index 0883dbc3..d4593a20 100644 --- a/src/routes/scripts/script_hash/json.ts +++ b/src/routes/scripts/script_hash/json.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../types/responses/scripts.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { [request.params.script_hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -29,9 +29,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/scripts/script_hash/redeemers.ts b/src/routes/scripts/script_hash/redeemers.ts index 6431aee8..74bd12ad 100644 --- a/src/routes/scripts/script_hash/redeemers.ts +++ b/src/routes/scripts/script_hash/redeemers.ts @@ -1,7 +1,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../types/queries/scripts.js'; import * as ResponseTypes from '../../../types/responses/scripts.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; import { SQLQuery } from '../../../sql/index.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; @@ -26,7 +26,7 @@ async function route(fastify: FastifyInstance) { ); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -46,7 +46,7 @@ async function route(fastify: FastifyInstance) { ], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -62,9 +62,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/cbor.ts b/src/routes/txs/hash/cbor.ts index ec8edfb8..18145350 100644 --- a/src/routes/txs/hash/cbor.ts +++ b/src/routes/txs/hash/cbor.ts @@ -3,7 +3,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '@blockfrost/blockfrost-utils/lib/fastify.js'; async function route(fastify: FastifyInstance) { @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -28,9 +28,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows[0]); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/delegations.ts b/src/routes/txs/hash/delegations.ts index f1bf914a..ec3796cb 100644 --- a/src/routes/txs/hash/delegations.ts +++ b/src/routes/txs/hash/delegations.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/index.ts b/src/routes/txs/hash/index.ts index 6fdadece..a8347e80 100644 --- a/src/routes/txs/hash/index.ts +++ b/src/routes/txs/hash/index.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return handle404(reply); @@ -82,9 +82,7 @@ async function route(fastify: FastifyInstance) { return reply.send(result); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/metadata/cbor.ts b/src/routes/txs/hash/metadata/cbor.ts index 58006643..c868edeb 100644 --- a/src/routes/txs/hash/metadata/cbor.ts +++ b/src/routes/txs/hash/metadata/cbor.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/tx.js'; import * as ResponseTypes from '../../../../types/responses/tx.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/metadata/index.ts b/src/routes/txs/hash/metadata/index.ts index 2d3c6385..e3b93bc9 100644 --- a/src/routes/txs/hash/metadata/index.ts +++ b/src/routes/txs/hash/metadata/index.ts @@ -2,7 +2,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import * as QueryTypes from '../../../../types/queries/tx.js'; import * as ResponseTypes from '../../../../types/responses/tx.js'; import { getSchemaForEndpoint } from '@blockfrost/openapi'; -import { getDbSync } from '../../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../../utils/database.js'; import { handle404 } from '../../../../utils/error-handler.js'; import { SQLQuery } from '../../../../sql/index.js'; @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -37,9 +37,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/mirs.ts b/src/routes/txs/hash/mirs.ts index 1fe70d1b..670ee561 100644 --- a/src/routes/txs/hash/mirs.ts +++ b/src/routes/txs/hash/mirs.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/pool-retires.ts b/src/routes/txs/hash/pool-retires.ts index 5ffddca8..516de8e4 100644 --- a/src/routes/txs/hash/pool-retires.ts +++ b/src/routes/txs/hash/pool-retires.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -39,9 +39,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/pool-updates.ts b/src/routes/txs/hash/pool-updates.ts index 66dc8a29..887f1527 100644 --- a/src/routes/txs/hash/pool-updates.ts +++ b/src/routes/txs/hash/pool-updates.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404, handle500 } from '../../../utils/error-handler.js'; import { convertStakeAddress } from '../../../utils/validation.js'; @@ -22,7 +22,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } const { rows } = await clientDbSync.query( @@ -31,7 +31,7 @@ async function route(fastify: FastifyInstance) { ); if (rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send([]); } @@ -65,7 +65,7 @@ async function route(fastify: FastifyInstance) { } if (!reward_account) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle500(reply, 'Reward account conversion failed', request); } @@ -102,12 +102,10 @@ async function route(fastify: FastifyInstance) { }), ); - clientDbSync.release(); + gracefulRelease(clientDbSync); return reply.send(results); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/redeemers.ts b/src/routes/txs/hash/redeemers.ts index 57747218..5fadb27e 100644 --- a/src/routes/txs/hash/redeemers.ts +++ b/src/routes/txs/hash/redeemers.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/required-signers.ts b/src/routes/txs/hash/required-signers.ts index d3cebac3..2e232d2d 100644 --- a/src/routes/txs/hash/required-signers.ts +++ b/src/routes/txs/hash/required-signers.ts @@ -3,7 +3,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '@blockfrost/blockfrost-utils/lib/fastify.js'; async function route(fastify: FastifyInstance) { @@ -20,7 +20,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -29,7 +29,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -37,9 +37,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/stakes.ts b/src/routes/txs/hash/stakes.ts index 41e13edc..5aa39aad 100644 --- a/src/routes/txs/hash/stakes.ts +++ b/src/routes/txs/hash/stakes.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/utxos.ts b/src/routes/txs/hash/utxos.ts index b7ca9f83..a53c88cf 100644 --- a/src/routes/txs/hash/utxos.ts +++ b/src/routes/txs/hash/utxos.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -27,7 +27,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -41,7 +41,7 @@ async function route(fastify: FastifyInstance) { [request.params.hash], ); - clientDbSync.release(); + gracefulRelease(clientDbSync); const responseInputs: ResponseTypes.TxUtxoInputs = []; const responseOutputs: ResponseTypes.TxUtxoOutputs = []; @@ -112,9 +112,7 @@ async function route(fastify: FastifyInstance) { return reply.send(response); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } }, diff --git a/src/routes/txs/hash/withdrawals.ts b/src/routes/txs/hash/withdrawals.ts index 6f94bbe8..5ba5bb59 100644 --- a/src/routes/txs/hash/withdrawals.ts +++ b/src/routes/txs/hash/withdrawals.ts @@ -4,7 +4,7 @@ import { FastifyInstance, FastifyRequest } from 'fastify'; import { SQLQuery } from '../../../sql/index.js'; import * as QueryTypes from '../../../types/queries/tx.js'; import * as ResponseTypes from '../../../types/responses/tx.js'; -import { getDbSync } from '../../../utils/database.js'; +import { getDbSync, gracefulRelease } from '../../../utils/database.js'; import { handle404 } from '../../../utils/error-handler.js'; async function route(fastify: FastifyInstance) { @@ -21,7 +21,7 @@ async function route(fastify: FastifyInstance) { ]); if (query404.rows.length === 0) { - clientDbSync.release(); + gracefulRelease(clientDbSync); return handle404(reply); } @@ -30,7 +30,7 @@ async function route(fastify: FastifyInstance) { request.params.hash, ]); - clientDbSync.release(); + gracefulRelease(clientDbSync); if (rows.length === 0) { return reply.send([]); @@ -38,9 +38,7 @@ async function route(fastify: FastifyInstance) { return reply.send(rows); } catch (error) { - if (clientDbSync) { - clientDbSync.release(); - } + gracefulRelease(clientDbSync); throw error; } },