Feathers sequelize 通过关联的列查找

Feathers sequelize find by associated column

我正在尝试根据关联 table 的值过滤数据,当我查找全部时它工作正常但是当我尝试按名称搜索时我得到一个列不存在的错误

我已经按照 https://github.com/feathersjs-ecosystem/feathers-sequelize#associations

中的说明设置了一个挂钩

在邮递员中,我可以将关联视为子键,但我无法按任何列进行搜索

正在获取代码

service.find({
        query: {
          include: ["policyholders"],
          Name: {
            $like: `%somename%`,
          },
        },
      });

政策模型

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require("sequelize");
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get("sequelizeClient");
  const policies = sequelizeClient.define(
    "policies",
    {
      PolicyId: {
        autoIncrement: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true,
      },
      PolicyNumber: {
        type: DataTypes.STRING(30),
        allowNull: true,
      },
      PolicyHolderId: {
        type: DataTypes.INTEGER,
        allowNull: true
      },
    },
    {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        },
      },
    }
  );

  // eslint-disable-next-line no-unused-vars
  policies.associate = function (models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    const { policyholders } = models;
    policies.belongsTo(policyholders, { foreignKey: "PolicyholderId" });
  };

  return policies;
};

保单持有人模型

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const policyholders = sequelizeClient.define(
    'policyholders',
    {
      PolicyholderId: {
        autoIncrement: true,
        type: DataTypes.INTEGER,
        allowNull: false,
        primaryKey: true,
      },
      Name: {
        type: DataTypes.STRING(250),
        allowNull: true,
      },
    },
    {
      hooks: {
        beforeCount(options) {
          options.raw = true;
        },
      },
    }
  );

  // eslint-disable-next-line no-unused-vars
  policyholders.associate = function (models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    const { policies } = models;

    policyholders.hasMany(policies, { foreignKey: { name: 'PolicyholderId' } });
  };

  return policyholders;
};

政策挂钩

const { authenticate } = require('@feathersjs/authentication').hooks;

const getRelated = require('../../hooks/get-related');

module.exports = {
  before: {
    all: [ authenticate('jwt') ],
    find: [getRelated()],
    get: [getRelated()],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  after: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },

  error: {
    all: [],
    find: [],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  }
};

挂钩变得相关

module.exports = function (options = {}) {
  return async (context) => {
    const { include, ...query } = context.params.query;

    if (include) {


      const AssociatedModel = context.app.services.policyholders.Model;
      context.params.sequelize = {
        include: [{ model: AssociatedModel }],
        raw: false
      };

      
      // Update the query to not include `include`
      context.params.query = query;
    }

    return context;
  };
};

答案也太

 "$policyholder.Name%": {
            $like: `%somename%`,
          },

然后在feathers服务中将字符串添加到白名单数组