Skip to content

Instantly share code, notes, and snippets.

@RobertDeniszczyc
Last active July 14, 2020 12:43
Show Gist options
  • Save RobertDeniszczyc/218d54569af4b57907d6dec2a471c65b to your computer and use it in GitHub Desktop.
Save RobertDeniszczyc/218d54569af4b57907d6dec2a471c65b to your computer and use it in GitHub Desktop.
Feathers-sequelize retrieval of MySQL data
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('featherstutorial', 'databaseUser', 'databasePassword', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
operatorsAliases: false
});
const Post = sequelize.define('posts', {
content: Sequelize.STRING,
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: {
status: 'published'
}
});
return Promise.resolve(posts);
},
get(id, params) {
let post = Post.findOne({
where: {
id: id
}
});
return Promise.resolve(post);
},
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}`);
});
@kadambs
Copy link

kadambs commented Jul 14, 2020

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment