Gajus Kuizinas
postgres://...
.createPool
. This is because Slonik needs to query the database in order to setup the connection pool. This requires us to slightly modify our app.createPool
function. Then, we added a main
function that creates the connection pool and starts the Express.js server. The only reason we need to do this is because we need to await
the createPool
function. Since we can't use await
at the top level, we need to wrap it in a function.transaction
or connect
methods.transaction
method that allows you to execute queries within the same connection and a single transaction:transaction
method to execute two queries within the same connection and a single transaction. This ensures that both queries either succeed together or fail together. If either query fails, the transaction is rolled back and the error is thrown.connect
method allows you to reserve a connection from the pool and execute queries on it.AsyncLocalStorage
is a core module introduced in Node.js that provides a mechanism to store and retrieve data based on the current execution context. It might be tempting to use this to pass a connection to a function:await
in our routes. You can use express-promise-router or express-async-handler to support asynchronous routes in your application.