Database Connection Pooling and ORM Patterns
Database Interaction: Connection Pools & ORMs
Efficient database interaction defines the throughput capability of enterprise Node.js APIs. Instantiating a new TCP connection to a database for every incoming HTTP request is prohibitively expensive.
Connection Pooling
A connection pool maintains a cache of pre-established, persistent database connections. When the application needs to execute a query, it leases an idle connection from the pool and immediately returns it upon completion. This vastly reduces connection overhead latency.
Data Access Patterns
- Query Builders (Knex.js): Provide programmatic abstraction over raw SQL strings, offering flexibility and SQL-injection protection while remaining close to the metal.
- Active Record (Sequelize, TypeORM): Domain objects encapsulate both data and database access methods.
- Data Mapper (MikroORM, Drizzle): A strict separation of concerns where entities are agnostic to the database, relying on repositories for persistence.