我有一个 MySQL 数据库,其中包含一个名为“posts”的表,我正在通过FeathersJS和feathers-sequelize 读取。目前我有一个带有以下代码的工作原型,它返回所需的结果,但只返回到控制台,还有帖子表的全部内容到 /posts 路由,但是我只想从表中返回一组特定的记录到/帖子。
该表有一个名为 post_status 的列,其中的帖子可以是“已发布”或“草稿”。我只想将“已发布”记录返回到 /posts,但想要实现此服务器端而不是 /posts?status=published。如何做到这一点?
见下面的代码:
const path = require('path');
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');
const Sequelize = require('sequelize');
const service = require('feathers-sequelize');
const sequelize = new Sequelize('sandbox', 'sandbox', 'secretpassword', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
operatorsAliases: false
});
const Post = sequelize.define('posts', {
post_title: Sequelize.STRING
},
{
timestamps: false,
underscored: true,
});
// Create an Express compatible Feathers application instance.
const app = express(feathers());
// Turn on JSON parser for REST services
app.use(express.json());
// Turn on URL-encoded parser for REST services
app.use(express.urlencoded({ extended: true }));
// Enable REST services
app.configure(express.rest());
// Enable Socket.io services
app.configure(socketio());
app.use(express.errorHandler());
//This works fine to return to the console but not to /posts
Post.findAll({
where: {
post_status: 'published'
}
}) .then(posts => {
console.log(JSON.stringify(posts));
});
//This returns the entire contents of the posts table to /posts
app.use('/posts', service({
Model: Post,
paginate: {
default: 10,
max: 100
},
}));
// Start the server
const port = 3030;
app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});
我尝试了服务中 findAll 方法的“位置”,但这并没有改变输出,也没有产生任何错误。