We need to implement three Express routes that handle aggregated data from a database containing cats and toys:
// GET /toys-summary
app.get('/toys-summary', async (req, res, next) => {
// Get total count of toys
const count = await Toy.count();
// Find minimum price across all toys
const minPrice = await Toy.min('price');
// Find maximum price across all toys
const maxPrice = await Toy.max('price');
// Calculate sum of all toy prices
const sumPrice = await Toy.sum('price');
res.json({
count,
minPrice,
maxPrice,
sumPrice
});
});
// GET /cats/:catId
app.get('/cats/:catId', async (req, res, next) => {
const { catId } = req.params;
// Find cat and eagerly load associated toys
const cat = await Cat.findByPk(catId, {
include: { model: Toy }
});
const toys = cat.Toys;
// Calculate toy statistics using array methods
const toyCount = toys.length;
const toyTotalPrice = toys.reduce((sum, toy) => {
return sum + toy.price;
}, 0);
const toyAvgPrice = Math.round(toyTotalPrice / toyCount);
res.json({
toyCount,
toyTotalPrice,
toyAvgPrice,
...cat.toJSON()
});
});
// GET /toys/:toyId
app.get('/toys/:toyId', async (req, res, next) => {
const { toyId } = req.params;
// Find toy and eagerly load associated cats
const toy = await Toy.findByPk(toyId, {
include: { model: Cat }
});
const cats = toy.Cats;
// Calculate cat statistics
const catCount = cats.length;
const orangeCatCount = cats.filter(cat =>
cat.color === 'Orange'
).length;
// Calculate percentage with rounding
const orangeCatPercentage = Math.round(
(orangeCatCount / catCount) * 100
) + '%';
res.json({
catCount,
orangeCatCount,
orangeCatPercentage,
...toy.toJSON()
});
});
Sequelize provides built-in methods for common database aggregations:
There are two main approaches to getting aggregate data:
For small datasets, JavaScript calculations can be simpler and more flexible. For large datasets, database aggregations are more efficient.
When working with Sequelize models, toJSON() converts the model instance into a plain JavaScript object. This is important for:
Think of this like an e-commerce system: