In an api I'm building, I have multiple routes that require an agency to exist before any action is performed on the requested agency.
To avoid repetition I've written some middleware to check that the agency does indeed exist before continuing (and throws if it does not).
The code I have works but I have to make two requests to the db, I'm wondering if I can accomplish the same goal with only a single query.
Here is my code:
// routes/v1/index.js
const express = require("express")
const router = require("express-promise-router")()
router.use(["/agencies/:id", "/agencies/:id/*"], agencies.checkExistsByReqId)
router
.route("/agencies/:id")
.get(agencies.getById)
.patch(agencies.patch)
.delete(agencies.delete)
export default router
// controllers/v1/agencies/checkExistsByReqId.js
import db from "../../../db"
import { AgencyDoesNotExistError } from "../../../errors"
export default async (req, res, next) => {
// note: To make the query as small as possible this just returns an id if exists
const exists = await db.agency.checkExistsById(req.params.id)
if (!exists) throw new AgencyDoesNotExistError()
next()
}
// /controllers/v1/agencies/getById.js
import db from "../../../db"
import { AgencyDoesNotExistError } from "../../../errors"
export default async (req, res) => {
const agency = await db.agency.getById(req.params.id)
const isMember = agency.members.find(member => member.authId === req.user.sub)
if (!isMember) throw new AgencyDoesNotExistError()
res.send(agency)
}
// db/index.js
export const agency = {
create: async data => new Agency(data).save(),
findOne: filter => Agency.findOne(filter),
findOneAndUpdate: (filter, update, options) =>
Agency.findOneAndUpdate(filter, update, options),
getById: async agencyId => Agency.findById(agencyId).populate("members"),
checkExistsById: agencyId => Agency.findById(agencyId, "_id")
}