import { Document, model, Schema, Types, Error } from 'mongoose'; import { accessibleRecordsPlugin } from '@casl/mongoose'; import { Enum } from '../../library/enum'; export const RecipeCollectionType = Enum('MANUAL', 'SMART'); export type RecipeCollectionType = Enum; export interface RecipeCollectionItem extends Types.Embedded { recipe: Types.ObjectId; } export interface RecipeCollectionItemProps { recipe: string; } // ** Generic Collection export interface RecipeCollection extends Document { name: string; ownerId: string; nodes: Types.DocumentArray; kind?: RecipeCollectionType; } export interface RecipeCollectionProps { name: string; ownerId: string; nodes: RecipeCollectionItemProps[]; kind?: RecipeCollectionType; } // ** Mongoose Setup export const recipeCollectionItemSchema = new Schema( { recipe: { type: Schema.Types.ObjectId, ref: 'Recipe', required: true, }, }, { _id: false } ); export const recipeCollectionSchema = new Schema( { name: { type: String }, nodes: [recipeCollectionItemSchema], ownerId: { type: Schema.Types.ObjectId, required: true }, }, { discriminatorKey: 'kind', timestamps: true, } ); // // Custom Methods // recipeCollectionSchema.methods = {}; // // Plugins // recipeCollectionSchema.plugin(accessibleRecordsPlugin); // // Model // export const RecipeCollection = model( 'RecipeCollection', recipeCollectionSchema );