2016-01-13 56 views
1

我有如下關係:Sequelize許多一對多格式化

var Order = sequelize.define('order', { 
    id : { 
    type   : Sequelize.INTEGER, 
    primaryKey : true, 
    autoIncrement : true 
    }, 
    amount  : { type : Sequelize.DOUBLE }, 
    change  : { type : Sequelize.DOUBLE }, 
    status  : { type : Sequelize.ENUM('draft', 'placed', 'in_transit', 'fulfilled', 'rejected') }, 
    statusReason : { type : Sequelize.STRING }, 
    address  : { type : Sequelize.STRING }, 
    latitude  : { type : Sequelize.DOUBLE }, 
    longitude : { type : Sequelize.DOUBLE } 
}) 

var Beverage = sequelize.define('beverage', { 
    id : { 
    type   : Sequelize.INTEGER, 
    primaryKey : true, 
    autoIncrement : true 
    }, 
    name : { type : Sequelize.STRING }, 
    picture : { type : Sequelize.STRING }, 
    price : { type : Sequelize.DOUBLE }, 
    max  : { type : Sequelize.DOUBLE }, 
}) 

var OrderBeverage = sequelize.define('orderBeverage', { 
    id : { 
    type   : Sequelize.INTEGER, 
    primaryKey : true, 
    autoIncrement : true 
    }, 
    orderId : { type : Sequelize.INTEGER }, 
    beverageId : { type : Sequelize.INTEGER }, 
    amount  : { type : Sequelize.INTEGER } 
}) 

Order.hasMany(OrderBeverage, { as : 'beverages' }) 
OrderBeverage.belongsTo(Beverage) 
OrderBeverage.belongsTo(Order) 

所以,如果我做一個簡單的查詢,包括關係,我有以下的源和結果:

let orders = yield Order.findAll({ 
    include : [{ 
    model  : Order.Beverage, 
    as   : 'beverages', 
    attributes : ['createdAt'], 
    include : [ Beverage ] 
    }] 
}) 

以及由此產生的json:

[ 
    { 
    "id": 2, 
    "amount": 0, 
    "change": 0, 
    "status": "placed", 
    "statusReason": "wrong address given", 
    "address": "street x, michigan", 
    "latitude": 0, 
    "longitude": 0, 
    "createdAt": "2016-01-12T22:39:32.000Z", 
    "updatedAt": "2016-01-13T21:05:06.000Z", 
    "beverages": [ 
     { 
     "createdAt": "2016-01-12T23:10:48.000Z", 
     "beverage": { 
      "id": 1, 
      "name": "Vodka", 
      "picture": "somepic", 
      "price": 30, 
      "max": 5, 
      "createdAt": "2016-01-12T22:26:10.000Z", 
      "updatedAt": "2016-01-12T22:26:10.000Z" 
     } 
     } 
    ] 
    } 
] 

我需要使屬性beverages成爲飲料內容的數組,而不是{ createdAt, beverage }這個飲料的內容是beverage密鑰的數組。

最後的結果會是什麼樣子:

[ 
    { 
    "id": 2, 
    "amount": 0, 
    "change": 0, 
    "status": "placed", 
    "statusReason": "wrong address given", 
    "address": "street x, michigan", 
    "latitude": 0, 
    "longitude": 0, 
    "createdAt": "2016-01-12T22:39:32.000Z", 
    "updatedAt": "2016-01-13T21:05:06.000Z", 
    "beverages": [ 
     { 
      "id": 1, 
      "name": "Vodka", 
      "picture": "somepic", 
      "price": 30, 
      "max": 5, 
      "createdAt": "2016-01-12T22:26:10.000Z", 
      "updatedAt": "2016-01-12T22:26:10.000Z" 
     } 
    ] 
    } 
] 

我已經通過文件搜索,但沒有找到它。有任何想法嗎?

回答

0

好吧,所以我想出瞭如何實現這一點。這是不直觀以任何方式。

而不是使用與OrderBeverage一個hasMany關係:

Order.hasMany(OrderBeverage, { as : 'beverages' }) 

,我們必須使用與BeveragebelongsToMany關係通過OrderBeverage

Order.belongsToMany(Beverage, { through : 'orderBeverage' }) 

這樣,我們只需修改查詢是這樣的:

let orders = yield Order.findAll({ 
    include : [{ 
    model : Beverage 
    }] 
}) 

我認爲這並不使多大意義,因爲飲料屬於多個orderBeverage,而不是爲了通過orderBeverage屬於多個飲料

在某種意義上,飲料通過orderBeverage屬於許多順序

也許我誤解了這個想法和概念......