电子商务产品的猫鼬模式

时间:2021-01-02 14:49:45

标签: node.js mongodb typescript rest express

所以我编写电子商务商店只是为了好玩,但我卡住了。我想创建带有变体和不带变体的产品,例如黑色 T 恤,尺寸为 M,但也例如没有任何选项可供选择的钢笔(它只是黑色钢笔 xd)。我的问题是为产品创建通用模型,该模型将适用于具有变体的产品和没有变体的产品。

这是我的架构

产品

import { model, Schema, Document, Types } from "mongoose";

const ProductSchema = new Schema({
  name: {
    type: String,
    required: true
  },
  description: {
    type: String,
    required: true
  },
  categories: [
    {
      type: Types.ObjectId,
      ref: "categories"
    }
  ],
  variants: [
    {
      type: Types.ObjectId,
      ref: "variants"
    }
  ],
  options: [
    {
      type: Types.ObjectId,
      ref: "options"
    }
  ]
});

export interface IProduct extends Document {
  name: string;
  description: string;
  categories: [string];
  variants: [string];
  options: [string];
}

export const Product = model<IProduct>("product", ProductSchema);

选项

import { model, Schema, Document, Types } from "mongoose";

const OptionSchema = new Schema(
  {
    name: {
      type: String,
      required: true
    },
    values: {
      type: [String],
      required: true
    },
    product_id: {
      type: Types.ObjectId,
      ref: "products"
    }
  },
  { timestamps: true }
);

export interface IOption extends Document {
  name: string;
  values: string[];
}

export const Option = model<IOption>("option", OptionSchema);

变体

import { Schema, Types, Document, model } from "mongoose";

const VariantSchema = new Schema(
  {
    sku: String,
    barcode: String,
    product_id: {
      type: Types.ObjectId,
      ref: "products"
    },
    pricing: {
      type: Types.ObjectId,
      ref: "prices"
    },
    variation_options: [
      {
        name: {
          type: String,
          required: true
        },
        value: {
          type: String,
          required: true
        }
      }
    ]
  },
  { timestamps: true }
);

export interface IVariant extends Document {
  sku: string;
  barcode: string;
  product_id: string;
  pricing: string;
  variation_options: [{ name: string; value: string }];
}

export const Variant = model<IVariant>("variant", VariantSchema);

价格

import { Schema, Types, Document, model } from "mongoose";

const PriceSchema = new Schema(
  {
    price: {
      type: Number,
      required: true,
      min: 0.01
    },
    discount: {
      type: Types.ObjectId,
      ref: "discounts"
    }
  },
  { timestamps: true }
);

export interface IPrice extends Document {
  price: number;
  discount: string;
}

export const Price = model<IPrice>("price", PriceSchema);

折扣

import { model, Schema, Document } from "mongoose";

const DiscountSchema = new Schema(
  {
    discount: {
      type: Number,
      required: true,
      min: 0.01
    },
    end_date: {
      type: Date,
      required: true
    }
  },
  { timestamps: true }
);

export interface IDiscount extends Document {
  discount: number;
  start_date: string;
  end_date: string;
}

export const Discount = model<IDiscount>("discount", DiscountSchema);

所以我想:

能够创建产品,如果我想为其添加变体(我希望变体是可选的,但我的大脑告诉我它们现在不是,因为没有它们我无法创建产品)。

0 个答案:

没有答案
相关问题