SQL Database Integration in Node.js
From the Express.js REST API cheat sheet ยท Database Integration ยท verified Jul 2026
SQL Database Integration
Connect Express to PostgreSQL/MySQL using query builders or ORMs
javascript
const mysql = require('mysql2/promise');
// Database connection
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydb',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});
// Query examples
const getUsers = async () => {
const [rows] = await pool.execute('SELECT * FROM users WHERE active = ?', [1]);
return rows;
};๐ก Use Prisma or Sequelize for type-safe database access
๐ Knex.js for flexible SQL query building
โก Use prepared statements to prevent SQL injection
โ ๏ธ Always use parameterized queries, never concatenate SQL
๐ข Essential - Choose ORM based on project needs
๐ Related: pg for PostgreSQL, mysql2 for MySQL