Junior — Middle
What tools or libraries do you use for interacting with databases in Node.js applications?
sobes.tech AI
Answer from AI
For interacting with databases in Node.js applications, the following tools and libraries are often used:
- Sequelize — ORM for SQL databases (PostgreSQL, MySQL, SQLite, etc.), allows working with the database through objects and models.
- TypeORM — another popular ORM with support for TypeScript.
- Mongoose — ODM for MongoDB, convenient for working with document-based databases.
- Knex.js — SQL query builder, provides flexibility when writing queries.
- pg — native client for PostgreSQL.
The choice depends on the type of database and project requirements. For example, Mongoose is usually used for MongoDB, while Sequelize or TypeORM are used for relational databases.
Example of using Mongoose:
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb');
const UserSchema = new mongoose.Schema({ name: String, age: Number });
const User = mongoose.model('User', UserSchema);
User.find({ age: { $gt: 18 } }).then(users => {
console.log(users);
});