Product.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import mongoose, {
  2. Document,
  3. Schema
  4. } from "mongoose";
  5. export interface IProduct extends Document {
  6. categoryIDs: mongoose.Types.ObjectId[];
  7. userID: mongoose.Types.ObjectId;
  8. photos: {
  9. coverPhotoPath: string;
  10. isPrimary: boolean;
  11. }[];
  12. isAvailable: boolean;
  13. description: string;
  14. isActive: boolean;
  15. title: string;
  16. price: number;
  17. }
  18. const productSchema = new Schema<IProduct>(
  19. {
  20. categoryIDs: {
  21. type: [mongoose.Schema.Types.ObjectId],
  22. index: true,
  23. default: []
  24. },
  25. userID: {
  26. type: mongoose.Schema.Types.ObjectId,
  27. index: true
  28. },
  29. title: {
  30. type: String,
  31. required: true,
  32. trim: true
  33. },
  34. description: {
  35. type: String,
  36. default: "",
  37. trim: true
  38. },
  39. price: {
  40. type: Number,
  41. required: true,
  42. min: 0
  43. },
  44. photos: [
  45. {
  46. coverPhotoPath: {
  47. type: String
  48. },
  49. isPrimary: {
  50. type: Boolean,
  51. default: false
  52. },
  53. _id: false
  54. }
  55. ],
  56. isActive: {
  57. type: Boolean,
  58. default: true
  59. },
  60. isAvailable: {
  61. type: Boolean,
  62. default: true
  63. },
  64. },
  65. {
  66. timestamps: true,
  67. }
  68. );
  69. export const Product = mongoose.models.Product || mongoose.model<IProduct>("Product", productSchema);