From 8a3923c5b9eefaaa59a081c2a77d0a225494ea45 Mon Sep 17 00:00:00 2001 From: Mateja Date: Tue, 27 Jul 2021 17:18:41 +0200 Subject: Improve `Order` model. Added timestamps and now using `ItemSchema` as subschema for array of items. --- server/models/Order.js | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'server/models/Order.js') diff --git a/server/models/Order.js b/server/models/Order.js index 9615373..a4b2508 100644 --- a/server/models/Order.js +++ b/server/models/Order.js @@ -1,5 +1,20 @@ const mongoose = require('mongoose'); +const ItemSchema = new mongoose.Schema({ + productId: { + type: mongoose.Schema.Types.ObjectId, + required: true + }, + name: { + type: String, + required: false + }, + quantity: { + type: Number, + required: true + } +}, {_id: false}); + const OrderSchema = new mongoose.Schema({ status: { type: String, @@ -14,16 +29,7 @@ const OrderSchema = new mongoose.Schema({ required: false }, items: { - type: [{ - productId: { - type: mongoose.Schema.Types.ObjectId, - required: true - }, - quantity: { - type: Number, - required: true - } - }], + type: [ItemSchema], required: true }, shipping: { @@ -43,7 +49,7 @@ const OrderSchema = new mongoose.Schema({ }, required: false } -}); +}, {timestamps: true}); const OrderModel = new mongoose.model('Order', OrderSchema); -- cgit v1.2.3