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

FeathersJS通过feathers-sequelize从MySQL返回指定状态的posts记录

Fixing FeathersJS /posts Route to Return Only Published Records

Hey there! Let's sort out why your /posts route is spitting out every record instead of just the published ones. With FeathersJS and feathers-sequelize, there are a couple of clean ways to fix this—here’s what you can do:

Feathers hooks are the most flexible way to modify query logic without messing with your core service code. We’ll add a before hook to the find method that automatically adds the post_status: 'published' condition to every request.

Open your posts service's hook file (usually src/services/posts/posts.hooks.js) and update the before section:

// src/services/posts/posts.hooks.js
module.exports = {
  before: {
    all: [],
    find: [
      (context) => {
        // Force all find requests to only return published posts
        context.query.post_status = 'published';
        
        // Optional: Let clients override the status if needed
        // context.query.post_status = context.query.post_status || 'published';
      }
    ],
    get: [],
    create: [],
    update: [],
    patch: [],
    remove: []
  },
  // ... keep your existing after/error hooks here
};

This hook runs right before the find method executes, ensuring every query includes the published status filter. If you want to let clients request other statuses (like drafts), use the commented line instead—it prioritizes the client's query parameter, falling back to published if none is provided.

2. Override the Service's find Method

If you prefer a more direct approach, you can rewrite the find method in your posts service to manually inject the filter:

// src/services/posts/posts.service.js
const { Service } = require('feathers-sequelize');

exports.Posts = class Posts extends Service {
  async find(params) {
    // Merge the published status filter with any existing query params
    params.query = {
      ...params.query,
      post_status: 'published'
    };
    
    // Call the original feathers-sequelize find method with the modified query
    return super.find(params);
  }
};

This works well if you want to encapsulate the filter directly in the service logic, though hooks are generally more reusable across different services or scenarios.

3. Use Sequelize Default Scopes

For a database-level solution, you can define a default scope in your Sequelize model. This will apply the published filter to every query made through the model, including Feathers service calls:

// src/models/posts.model.js
const { DataTypes } = require('sequelize');

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const posts = sequelizeClient.define('posts', {
    // Your existing field definitions
    post_status: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    },
    // Define a default scope to only fetch published posts
    defaultScope: {
      where: {
        post_status: 'published'
      }
    },
    // Optional: Add other scopes for different statuses
    scopes: {
      draft: {
        where: {
          post_status: 'draft'
        }
      }
    }
  });

  posts.associate = function (models) {
    // Keep your existing associations here
  };

  return posts;
};

With a default scope, every query will automatically include the post_status: 'published' condition. If you ever need to bypass this (e.g., for admin endpoints), you can use this.Model.unscoped() in your service methods to fetch all records.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:16:49