2016-04-12 15 views
3

includesのように、Sequelizeで複合語findAllを作成しています。Sequelizeの結果を再編成する

{ 
    "description": "Wow. Nice one.", 
    "createdAt": "2016-04-11T23:05:15.736Z", 
    "body": "hi", 
    "id": 10, 
    "Section": { 
    "slug": "ttyl", 
    "Type": { 
     "slug": "Test", 
     "Group": { 
     "slug": "python2", 
     } 
    } 
    } 
} 

へのネストされたが、で動作するように少し不快な結果になることを奥行き:

const versions = yield Version.findAndCountAll({ 
    order: [['createdAt', 'DESC']], 
    attributes: ['description', 'createdAt', 'id'], 
    include: [{ 
    model: Section, 
    attributes: ['slug'], 
    where: { slug: section }, 
    include: [{ 
     model: Type, 
     attributes: ['slug'], 
     where: { slug: type }, 
     include: [{ 
     model: Group, 
     attributes: ['slug'], 
     where: { slug: group } 
     }] 
    }] 
    }] 
}); 

結果は、次のように出てきます。可能であれば、このようにしたいと思います。

{ 
    "description": "Wow. Nice one.", 
    "createdAt": "2016-04-11T23:05:15.736Z", 
    "body": "hi", 
    "id": 10, 
    "Section": "ttyl", 
    "Type": "Test", 
    "Group": "python2" 
} 

これは可能ですか?

答えて

1

の範囲に平坦化するには、raw:trueを設定します。 Raw trueは、model.subproperty.propertyなどに展開されます。include 'as'オプションを使用すると、これらのプロパティに別名を付けることができます。私はあなたが自動的に完全にオブジェクトを形作ることはできないと信じています(少なくとも私はドキュメントに何も見つかりませんでした)。上記の例から、最終的には、自分のマッピングステップを追加する必要があると私は信じています。

Model.findAndCountAll({ 
    ... 
}).then(function(results){ 
    return results.map(function(items){ 
     //map into another "object' 
     //or into anonymous {}; 
    }); 
}); 
+0

あなたは後世のために共有した関連部分をコピーできますか? – Noah

+0

@Noahは、平坦化に関する私のコメントを明確化/更新しました。 – scottjustin5000

関連する問題