I have a MySQL database with a table named 'posts' I'm reading from via FeathersJS and feathers-sequelize. Currently I have a working prototype with the following code which returns the desired result but only to the console, and also the entire contents of the posts table to the /posts route, however I only want to return a specific set of records from the table to /posts.
The table has a column called post_status, in which a post could be 'published' or 'draft'. I only want to return the 'published' records to /posts, but want to achieve this serverside as opposed to /posts?status=published. How can this be achieved?
See below code:
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}`);
});
I tried the 'where' from the findAll method in the service but this did not change the output, or produce any error.
I resolved this by using a where clause in the find method on the service, see below complete code:
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,
post_status: 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());
const myService = {
find(params) {
let posts = Post.findAll({
where: {
post_status: 'published'
}
});
return Promise.resolve(posts);
},
get(id, params) {},
create(data, params) {},
update(id, data, params) {},
patch(id, data, params) {},
remove(id, params) {},
setup(app, path) {}
}
app.use('/posts', myService);
// Start the server
const port = 3030;
app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});