Created
December 29, 2022 11:19
-
-
Save kusakawazeusu/ce83e8e6608d769df6996264f8efee68 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 案例一:toJSON() | |
const data = tweets.map(t => ({ | |
...t.toJSON(), | |
})) | |
// 改為使用下面的參數 | |
const tweets = await Tweet.findAll({ | |
nest: true, | |
raw: true, | |
}) | |
// 案例二:sort | |
const topFollowings = allUsers | |
.sort((a, b) => { | |
b.Followers.length - a.Followers.length | |
}) | |
.slice(0, 10) | |
.map(topFollowing => { | |
return { | |
...topFollowing.toJSON(), | |
isFollowed: followingList.includes(topFollowing.id) | |
} | |
}) | |
// 改為用 aggregation | |
const users = await User.findAll({ | |
attributes: [ | |
[sequelize.literal('(SELECT COUNT(*) FROM Followers WHERE Followers.userId = User.id)'), 'FollowerCount'] | |
], | |
order: [[sequelize.literal('FollowerCount'), 'DESC']] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment