Junior — Middle
Have you worked with SQL queries written directly for the database?
sobes.tech AI
Answer from AI
Yes, I have experience writing SQL queries directly for databases in Node.js projects. I usually used libraries such as pg for PostgreSQL or mysql2 for MySQL to execute queries directly, which allowed flexible data management and performance optimization.
Here's a simple example of a Node.js 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, email FROM users WHERE active = $1', [true]);
await client.end();
return res.rows;
}
This approach allows writing complex queries, using transactions, and controlling database interactions directly.