You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express.js+PostgreSQL+Knex中动态获取筛选数组的最优方案咨询

Knex whereIn 动态筛选数组与城市验证最佳方案

不需要新建表存储筛选数组,直接基于现有数据或维护一个合法城市字典表(更优)即可实现需求,以下是具体方案:

核心思路

  1. 当请求参数city为"All"时,从数据库获取所有合法城市列表作为筛选数组
  2. 当city为指定值时,先验证该城市是否存在于数据库,避免无效筛选
  3. 优先使用独立的cities表维护合法城市(若没有则从person表去重获取),确保数据一致性

代码实现

1. 封装城市合法性校验与数组获取函数

async function getValidCities(pool, cityParam) {
  if (cityParam === "All") {
    // 方案1:从独立的cities表获取所有合法城市(推荐)
    // const cities = await pool.select('city_name').from('cities');
    // return cities.map(item => item.city_name);

    // 方案2:从person表去重获取城市(无cities表时使用)
    const cities = await pool.select('city').from('person').distinct();
    return cities.map(item => item.city);
  } else {
    // 验证城市是否存在(优先查cities表,无则查person表)
    // const exists = await pool.select('id').from('cities').where('city_name', cityParam).first();
    const exists = await pool.select('city').from('person').where('city', cityParam).first();
    
    if (!exists) {
      throw new Error('指定城市不存在');
    }
    return [cityParam];
  }
}

2. 修改查询接口为异步函数

const getSpecialsits = async (req, res) => {
  try {
    // 参数类型转换与默认值处理
    const page = parseInt(req.query.page) || 1;
    const limit = parseInt(req.query.limit) || 28;
    const city = req.query.city || "All";

    // 获取合法筛选城市数组
    const city_array = await getValidCities(pool, city);

    // 执行分页查询
    const data = await pool.select('*')
      .from('person')
      .limit(limit)
      .offset((page - 1) * limit)
      .whereIn('city', city_array);

    res.json(data);
  } catch (err) {
    console.error(err);
    res.status(400).json({ error: err.message });
  }
};

module.exports = {
  getSpecialsits,
};

优化建议

  • 建立独立cities表:专门存储所有合法城市(如id、city_name字段),便于统一维护城市列表,避免person表中出现脏数据,同时验证查询效率更高
  • 缓存城市列表:若城市数据不频繁更新,可将所有合法城市列表缓存到Redis或内存中,减少重复数据库查询
  • 增强参数校验:对page、limit添加数值范围校验(如page >=1、limit <=100),避免非法参数导致的异常

内容的提问来源于stack exchange,提问作者S0V3T

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 04:06:25