Rename collection-exists

This commit is contained in:
rijkvanzanten
2020-06-24 17:01:58 -04:00
parent 036dea86b8
commit e74b7a8608

View File

@@ -1,19 +1,23 @@
/**
* Check if requested collection exists, and save it to req.collection
*/
import { RequestHandler } from 'express';
import asyncHandler from 'express-async-handler';
import database from '../database';
import APIError, { ErrorCode } from '../error';
const collectionExists: RequestHandler = asyncHandler(async (req, res, next) => {
const validateCollection: RequestHandler = asyncHandler(async (req, res, next) => {
if (!req.params.collection) return next();
const exists = await database.schema.hasTable(req.params.collection);
if (exists) {
res.locals.collection = req.params.collection;
req.collection = req.params.collection;
return next();
}
throw new APIError(ErrorCode.NOT_FOUND, `Collection "${req.params.collection}" doesn't exist.`);
});
export default collectionExists;
export default validateCollection;