Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | const mongoose = require('mongoose'); // schema export const categorySchema = new mongoose.Schema({ title: String, slug: String, index: Number, }); mongoose.model('Category', categorySchema); export const categoryModel = mongoose.model('Category'); // queries export const getCategories = async (conditions?: object) => categoryModel.find(conditions || {}).sort({ index: 1 }); export const getCategory = async (id: string) => categoryModel.findById(id); export const getCategoryBySlug = async (slug: string) => categoryModel.findOne({ slug }); export const deleteCategory = async (id: string) => { const category = await categoryModel.findByIdAndDelete(id); if (!category) { throw new Error('Category Not Found'); } return category._id; }; export const getNewCategoryIndex = async (): Promise<number> => { const lastCategory = await categoryModel.findOne({}).sort({ index: -1 }); return lastCategory ? lastCategory.index + 1 : 0; }; |