ingredient.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const isSanitary = require("../validator.js").isSanitary;
  2. const mongoose = require("mongoose");
  3. const IngredientSchema = new mongoose.Schema({
  4. name: {
  5. type: String,
  6. minlength: [2, "INGREDIENT NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
  7. required: [true, "INGREDIENT NAME IS REQUIRED"],
  8. validate: {
  9. validator: isSanitary,
  10. message: "INGREDIENT NAME CONTAINS ILLEGAL CHARACTERS"
  11. }
  12. },
  13. category: {
  14. type: String,
  15. minlength: [2, "INGREDIENT CATEGORY MUST CONTAIN AT LEAST 2 CHARACTERS"],
  16. required: [true, "INGREDIENT CATEGORY IS REQUIRED"],
  17. validate: {
  18. validator: isSanitary,
  19. message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
  20. }
  21. },
  22. unit: {
  23. type: String,
  24. required: true
  25. },
  26. altUnit: {
  27. type: String,
  28. required: false
  29. },
  30. convert: {
  31. toMass: Number,
  32. toVolume: Number,
  33. toLength: Number
  34. },
  35. ingredients: [{
  36. ingredient: {
  37. type: mongoose.Schema.Types.ObjectId,
  38. ref: "Ingredient",
  39. required: true
  40. },
  41. quantity: {
  42. type: Number,
  43. required: true,
  44. min: 0
  45. },
  46. unit: {
  47. type: String,
  48. required: true
  49. }
  50. }],
  51. });
  52. module.exports = mongoose.model("Ingredient", IngredientSchema);