Junior — Middle
Tell about your experience using pure SQL without ORM or other abstractions.
sobes.tech AI
Answer from AI
Using pure SQL without ORM in Node.js provides full control over queries and allows optimization for specific tasks. In my experience, I have written queries manually using libraries such as pg for PostgreSQL or mysql2 for MySQL.
This is useful when:
- writing complex queries with optimal performance,
- utilizing specific database functions,
- avoiding ORM overhead,
- quickly debugging and understanding exactly what is executed in the database.
Example of a query using pg:
const { Client } = require('pg');
const client = new Client({ connectionString: process.env.DATABASE_URL });
async function getUsers() {
await client.connect();
const res = await client.query('SELECT id, name FROM users WHERE active = $1', [true]);
await client.end();
return res.rows;
}
This approach requires careful handling of parameters to avoid SQL injections and a good understanding of SQL.