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

More Node.js tasks

Back to the full Express.js REST API cheat sheet