HomeBytesheetsRoadmapsUI TemplatesUI ToolsStack Discussion
3 Topics
Chapter•100 Minutes

Databases and Data Persistence

Learn how Node.js applications communicate with databases, design data-access layers, handle MongoDB and SQL concepts, and manage transactions.

Database Connections

Node.js applications should manage database connections efficiently. Creating a new database connection for every request is usually expensive. Database clients commonly maintain a connection pool or reusable connection state.

Connection configuration should come from environment variables or secure configuration management.

MongoDB Connection with Mongoose
import mongoose from 'mongoose';

await mongoose.connect(process.env.MONGODB_URI);
console.log('MongoDB connected');
💡 The database URI is read from the environment and Mongoose establishes a reusable database connection.
Key Takeaways
  • Reuse database connections instead of reconnecting per request.
  • Keep credentials outside source code.
  • Handle connection failures and shutdowns explicitly.
Knowledge Check

Where should a production MongoDB connection string normally be stored?

MongoDB and Mongoose

MongoDB is a document-oriented database. Mongoose provides schemas, models, validation, middleware, and convenient querying on top of MongoDB.

A schema describes document structure and a model provides an interface for interacting with a MongoDB collection.

Mongoose Schema and Query
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true }
});

const User = mongoose.model('User', userSchema);

const users = await User.find({ name: /sur/i }).lean();
console.log(users);
💡 The schema defines user fields and the model queries matching documents. lean() returns plain objects instead of full Mongoose documents.
Key Takeaways
  • Mongoose schemas define application-level document rules.
  • Indexes should support actual query patterns.
  • Use lean() when document methods and change tracking are unnecessary.
Knowledge Check

What does a Mongoose model primarily provide?

Transactions and Data Consistency

Some operations require multiple database changes to succeed or fail together. Database transactions provide atomicity for supported workloads.

Transactions should be used when consistency requirements justify their cost. They are not a substitute for good data modeling, validation, idempotency, and appropriate constraints.

Transactions Are Not Free

Transactions can increase resource usage and contention. Use them when atomicity is genuinely required.

Key Takeaways
  • Transactions provide atomicity across supported database operations.
  • Use constraints and validation alongside transactions.
  • Design operations to be idempotent where retries are possible.
Knowledge Check

What is a key benefit of a database transaction?