首页 > 解决方案 > ObjectionJS:将选择/列附加到当前查询

问题描述

我正在使用 Objection.js 模型修饰符来按地理位置限制某些查询,我不确定如何在不替换它们的情况下将列附加到当前查询。

例如,在地理半径内搜索配置文件时,最好使用查询返回距离:

static modifiers = {
    withinMiles(query, profileId: string, miles: number = 100) {
        query
            .column({
                distance: raw(
                    `earth_distance(
                        ll_to_earth(targetProfile.geo_lat, targetProfile.geo_lon),
                        ll_to_earth(person.geo_lat, person.geo_lon)
                    )`
                ),
            })
            .join('profile as targetProfile', 'profile.id', raw(`'${profileId}'`))
            .whereRaw(
                `earth_box(ll_to_earth(targetProfile.geo_lat, targetProfile.geo_lon), 100) 
                 @> ll_to_earth(profile.geo_lat, profile.geo_lon)`
            );
    },
};

不幸的是,这将替换当前查询和任何图形连接中的列。

标签: javascriptobjection.js

解决方案


推荐阅读