I have a function which queries my database to find nearby users. However, my database stores the longitude and latitude of users, and to find the distance from the user, I need to call a function (distance) to check which users to return in the query. Is there a way to do what I have in the code (call my local distance function inside my querystring?)
exports.getNearby = function (longitude, latitude) {
var long1 = longitude;
var lat1 = latitude;
return new Promise(((resolve, reject) => {
const queryString = `SELECT ${userModel.userId}
FROM ${userModel.collectionName} where SELECT distance(${userLocationModel.latitude},
${userLocationModel.longitude}, ${lat1}, ${long1}) < 4`;
db.query(queryString, (err, res) => {
if (err) {
logger.log('error', ` getUser - ${userIdArr} - ${err}`);
reject(err);
} else {
resolve(res.rows[0]);
}
});
}));
};
function distance(lat1, lon1, lat2, lon2) { //returns in km
return 12742 * asin(sqrt(0.5 - cos((lat2 - lat1) * 0.01745329252)/2
+ cos(lat1 * 0.01745329252) * cos(lat2 * 0.01745329252) *
(1 - cos((lon2 - lon1) * 0.01745329252)) / 2));
}
Function written in Postgres (as suggested by @LaurenzAlbe)
CREATE FUNCTION distance(
lat1 double precision,
lng1 double precision,
lat2 double precision,
lng2 double precision
) RETURNS double precision AS
'SELECT 12742
* asin(
|/ (
0.5 - cos(
(lat2 - lat1) * 0.01745329252
)/2
+ cos(lat1 * 0.01745329252)
* cos(lat2 * 0.01745329252)
* (1 - cos(
(lon2 - lon1) * 0.01745329252
)) / 2
)
)'
LANGUAGE sql IMMUTABLE;