1 - download this
2 - copy this _templates folder to your app
3 - install hygen
npm i hygen --save-dev
4 - inside your src/service/index.js
after the last app.configure(..) add comment
// services
module.exports = function (app) {
....
app.configure(services.userAbilities);
// services
}
5- add this script to your package.json
"create-service": "hygen generator service"Done!
We going to create posts service
1- Create posts folder in the src/service folder with this files
src/services/posts/
posts.class.js
posts.hooks.js
posts.model.js
posts.service.js
posts.validators.js
4 - inside your src/service/index.js
const posts = require('./posts/posts.service.js');
module.exports = function (app) {
...
// services
app.configure(posts);
}
3 - Copy and past each file content from this snippet
posts.class.js
const { Service } = require('feathers-mongoose');
exports.Posts = class Posts extends Service {
};
hook.authenticate
- Feathers local, token, and OAuth authentication over REST and Websockets using JSON Web Tokens (JWT) with PassportJS.
validateAbilities
This is a wrapper of Casl, in this hook, we will define abilities and block client without the ability to run this request
Casl will add to mongoose query object a relevant key value before making the request, and validate Abilities will remove fields from user request by id abilities
validateSchema
This hook will use JOI to validate request data follow the scheme
sanitizedData
This hook will remove data from response follow the user abilities
7. Commit changes
git add .
git commit -m "create posts service"
dashboard:
Try to create a new posts from the dashboard
Now you can see the posts service inside the dashboard
Anyone can read the posts title
User can create/update only if he the author
Only admin user can delete posts