Skip to content

Instantly share code, notes, and snippets.

@elarcoiris
Last active June 5, 2023 10:26
Show Gist options
  • Save elarcoiris/888d2b0c6a6bbcfe3676d8fd59b9ca0b to your computer and use it in GitHub Desktop.
Save elarcoiris/888d2b0c6a6bbcfe3676d8fd59b9ca0b to your computer and use it in GitHub Desktop.
Distance calculation - Sequelize model method
User.getDistance = async (me) => {
try {
// Distance algorithm with Earth curvature
const users = await User.findAll({
where: Sequelize.where(sequelize.literal(`6371 * acos(cos(radians(${me.latitude})) * cos(radians(latitude)) * cos(radians(${me.longitude}) - radians(longitude)) + sin(radians(${me.latitude})) * sin(radians(latitude)))`),
'<=', me.maxDistance),
attributes: ['id']
})
return users;
}
catch (error) {
console.log(error.message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment