我正在为一家多城市的鞋业公司(例如耐克)建立一个中央数据库 在处理价格和库存时确实陷入了模式设计。
我在想以下几点:
const productSchema = new mongoose.Schema({
title: {type: String, required: true},
category: {type: String, required: true},
backgroundColor: {type: String, default: 'white'},
imgSrc: {type: String, required: true},
varieties: {
type: [varietySchema],
},
});
const varietySchema = new mongoose.Schema({
title: {type: String, required: true},
imgSrc: {type: String, required: true},
backgroundColor: {type: String, default: 'white'},
});
mongoose.model('Product', productSchema);
const storeSchema = new mongoose.Schema({
title: {type: String, required: true},
inventory: [{
productRef: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product'
},
varieties: [{
varietyRef: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Product.varieties'
},
stock: Number,
price: Number
}]
}]
});
我强烈认为这不是设计方案的最佳解决方案,但我想不出一种满足我要求的解决方法。同样,在商店模型中创建和更新条目似乎本身就是一项艰巨的任务,因为每次更新时我都必须检查产品和品种的存在,并且在商店库存中缺少两者或其中任何一个时都需要备用处理程序。有人可以帮我吗? :)