5

I have two models:

Order.js

module.exports = {
    attributes: {
        //Id is generated by mongo and is a primary key
        address: {
            type: 'string',
            required: true,
            size: 1000
        },
        status: {
            type: 'string',
            required: true,
            size: 15,
            defaultsTo: 'pending',
            enum: ['pending', 'processing', 'cancelled']
        },
        total: {
            type: 'string',
            required: true,
            size: 50
        },
        menues: {
            collection: 'menu',
            via: 'orders',
            dominant: true
        },
        customer: {
            model: 'customer'
        }
    }
};

and Menu.js

module.exports = {
    attributes: {
        //Id is generated by mongo
        name: {
            type: 'string',
            required: true,
            size: 255
        },
        description: {
            type: 'string',
            required: true,
            size: 1000
        },
        price: {
            type: 'string',
            required: true,
            size: 15
        },
        picture: {
            type: 'string',
            size: 255
        },
        //Relations
        orders: {
            collection: 'order',
            via: 'menues'
        }
    }
};

There is a many-to-many relation it perfectly works and creates a menu_orders__order_menues collection, everything is fine.

But I need to save quantity for each Menu in order. Like for menu id "1" quantity is 2 etc.

So an additional data is quantity: each order should have a definite amount of each menu.

What is the best way to do this, and were and how to store it in db? I thought to store it in relations table somehow, but I didn't fing how.

Please let me know the most correct and acceptable solution.

P.S. I currently use sails-mongo but will migrate to MySQL or Postgres soon so I need this to be done relative way.

Alexander Arutinyants
  • 1,619
  • 2
  • 23
  • 49

1 Answers1

2

Add quantity field to your Menu model schema and can use afterCreate function to set a value for it.

Order.js

module.exports = {
    attributes: {
        address: {
            type: 'string',
            required: true,
            size: 1000
        },
        status: {
            type: 'string',
            required: true,
            size: 15,
            defaultsTo: 'pending',
            enum: ['pending', 'processing', 'cancelled']
        },
        total: {
            type: 'string',
            required: true,
            size: 50
        },
        menues: {
            collection: 'menu',
            via: 'orders',
            dominant: true
        },
        customer: {
            model: 'customer'
        }
    },
    afterCreate: function (order, cb) {
        Menu.find(order.menues, function (err, menues) {
            if (err) return cb(err);
            async.each(menues, function (menu, next) {
                menu.quantity++;
                menu.save(next);
            }, cb);
        });
    }
};

Menu.js

module.exports = {
    attributes: {
        name: {
            type: 'string',
            required: true,
            size: 255
        },
        description: {
            type: 'string',
            required: true,
            size: 1000
        },
        price: {
            type: 'string',
            required: true,
            size: 15
        },
        picture: {
            type: 'string',
            size: 255
        },
        quantity: {
            type: 'number',
            defaultsTo: 0
        },
        orders: {
            collection: 'order',
            via: 'menues'
        }
    }
};

It is not a finished solution. You have to handle afterUpdate and afterDestroy functions too.

Nazar
  • 1,769
  • 1
  • 15
  • 31
  • Oh, I see, thank you, Leestex! But it will be a quantity of menu, not per order I suppose. I need per order, like if you order 5 bigBurger menues and 1 Diabetic. However I'll read on before/after(Update) methods maybe I'll figure something out... – Alexander Arutinyants Apr 20 '15 at 06:30
  • Hey Alex just wondering if you found a solution for this? I've come across the same hurdle. – eptgrant Apr 25 '15 at 05:46
  • Hey @eptgrant the progress happens here: https://github.com/balderdashy/waterline/issues/705 and it's very slow. So I'm kind of overwriting default queries injecting join table results yet... – Alexander Arutinyants May 18 '15 at 14:59