数据库
首页 > 数据库> > mysql – sails.js多对多连接表上的额外字段

mysql – sails.js多对多连接表上的额外字段

作者:互联网

我有两个型号:

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'
        }
    }
};

和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'
        }
    }
};

有一个多对多的关系,它完美地工作,并创建一个menu_orders__order_menues集合,一切都很好.

但我需要按顺序为每个菜单保存数量.对于菜单ID“1”,数量为2等.

因此,额外的数据是数量:每个订单应该有一定数量的每个菜单.

这样做的最佳方法是什么,以及如何将其存储在db中?
我想以某种方式将它存储在关系表中,但我没有说明如何.

请让我知道最正确和可接受的解决方案.

附:我目前使用sails-mongo但很快就会迁移到MySQL或Postgres,所以我需要以相对的方式完成.

解决方法:

将数量字段添加到Menu模型架构,并可以使用afterCreate函数为其设置值.

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'
        }
    }
};

它不是一个完成的解决方案.您还必须处理afterUpdate和afterDestroy函数.

标签:mysql,sql,node-js,sails-js,waterline
来源: https://codeday.me/bug/20190702/1360598.html