2012-12-22 21 views
1

在Sequelize QueryChainer中遇到一些困難。通常我不需要這樣做,但我決定儘早處理自己的關聯,而不是使用建立在關聯管理中的模塊(事實證明這是相當懲罰)。續集QueryChainer語法

我正在寫出我的REST例程,並試圖根據已獲取或需要獲取的其他記錄刪除一些記錄。而不是有4-5嵌套回調,我決定QueryChainer和一個串行運行將是適當的。

在這個例子中,我帶着一個用戶,如果找到了一個,我想拼接一行查詢來處理刪除特定的關聯。我遇到的問題主要是在使用模型EventEmitting方法之外理解chainer的語法。

 // Find user, destroy it and associations 
    db.Models.user.find({ where: {STUID: id} }) 
     .success(function (user) { 
      if (!user) { 
       res.locals.err = { 
        status: 'FAILED', 
        message: 'Could not delete user', 
        reason: 'No user resource found' 
       } 
      } else { 
       chain = new db._sqlize.Utils.QueryChainer(); 
       chain.add(user.destroy()) 

       // Check and destroy author association 
       if (user.AUTHOR) { 
        // Would like to fetch the specific author model 
        // db.models.author.find({}) 
        // ... and destroy it too, 
        // author.destroy() 
        // without having to nest 
       } 


      } 
     }); 

sequelize querychain語法指定做類似如下:

.add(Model, 'function', [param1, param2]) 

但我不上究竟意味着不太清楚。我可以在這裏定義內聯函數嗎?函數只是一個字符串尋找一個已經在其他地方定義過的字符串,或者它是模型的函數,我'添加'?文檔在這裏感覺有點匆忙。

回答