Created
July 25, 2025 12:18
-
-
Save leegilmorecode/50f4a68c5fe127bb205508cbbaa8ccb0 to your computer and use it in GitHub Desktop.
Closing connections in DSQL on each Lambda invocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
closeSequelizeConnection, | |
createSequelizeInstance, | |
} from '@shared/dsql-common'; | |
import { GetOrdersRequest, schema } from './get-orders.schema'; | |
import { MetricUnit } from '@aws-lambda-powertools/metrics'; | |
import { logger } from '@shared'; | |
import { withHttpHandler } from '@shared/http-handler'; | |
import { getOrdersUseCase } from '@use-cases/get-orders'; | |
export const handler = withHttpHandler(async ({ event, metrics }) => { | |
const sequelize = await createSequelizeInstance(); | |
try { | |
// Parse query parameters | |
const queryParams = event.queryStringParameters || {}; | |
const params: GetOrdersRequest = schema.parse({ | |
page: queryParams.page ? Number(queryParams.page) : undefined, | |
limit: queryParams.limit ? Number(queryParams.limit) : undefined, | |
status: queryParams.status?.toUpperCase(), | |
customerId: queryParams.customerId, | |
startDate: queryParams.startDate, | |
endDate: queryParams.endDate, | |
customerName: queryParams.customerName, | |
contactEmail: queryParams.contactEmail, | |
paymentMethod: queryParams.paymentMethod, | |
}); | |
const { orders, totalCount } = await getOrdersUseCase(params); | |
metrics.addMetric('SuccessfulGetOrders', MetricUnit.Count, 1); | |
return { | |
statusCode: 200, | |
body: { | |
orders, | |
totalCount, | |
currentPage: params.page, | |
totalPages: Math.ceil(totalCount / params.limit), | |
}, | |
}; | |
} catch (error) { | |
logger.error(`Error:, ${error}`); | |
metrics.addMetric('GetOrdersError', MetricUnit.Count, 1); | |
throw error; | |
} finally { | |
await closeSequelizeConnection(sequelize); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment