$ feathers generate service
? What kind of service is it? Mongoose
? What is the name of the service? comments
? Which path should the service be registered on? /comments
? Does the service require authentication? No
? Does the service require authentication? No
We authentication all the services from app.hook
2- Create validator file
inside validators create comments.validators.js with this contnet
// comments-model.js - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
const commentsValidators = require('../validators/comments.validators.js');
const {createModelFromJoi} = require('feathers-mongoose-casl');
module.exports = function (app) {
return createModelFromJoi(app, 'comments', commentsValidators);
};
replace feathers-mongoose with createService from feathers-mongoose-casl
feathers-mongoose-casl.createService is a wrapper of feathers-mongoose but we add mongoose-to-swagger to let you see the schema inside swagger docs and we handle validation with joi
// Initializes the `comments` service on path `/comments`
const {createService} = require('feathers-mongoose-casl');
const createModel = require('../../models/comments.model');
const hooks = require('./posts.hooks');
module.exports = function (app) {
const Model = createModel(app);
const paginate = app.get('paginate');
const options = {
Model,
paginate,
serviceRules: [
{'actions': ['manage'], 'roles': ['admin']},
{'actions': ['read'], anonymousUser: true},
]
};
// Initialize our service with any options it requires
app.use('/comments', createService(options));
// Get our initialized service so that we can register hooks
const service = app.service('comments');
service.hooks(hooks);
};