# Express + a Database — Express.js

Source: https://www.geekswithgeeks.com/en/expressjs/ex-database

> Express itself is database-agnostic — you bring the driver or ODM.

## No database built in

Express has no opinion on storage. For MongoDB, apps commonly use **Mongoose** (an ODM); for PostgreSQL/MySQL, the `pg` driver or an ORM like **Prisma**.

## A minimal Mongoose route

Connect once at startup, then use a model inside route handlers.

```javascript
import mongoose from 'mongoose';

await mongoose.connect(process.env.MONGO_URI);

const User = mongoose.model('User', new mongoose.Schema({
  name: String,
  email: String,
}));

app.get('/users', async (req, res, next) => {
  try {
    const users = await User.find();
    res.json(users);
  } catch (err) {
    next(err);
  }
});
```

**Quiz:** What is Mongoose?

- [ ] A CSS framework
- [x] An ODM for modeling MongoDB data in Node
- [ ] A replacement for Express itself

*Answer:* An ODM for modeling MongoDB data in Node. Mongoose is an Object Data Modeling library that sits on top of the MongoDB driver.
