Create a new service
1- feathers generate service
$ 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
const {Joi} = require('feathers-mongoose-casl');
const getJoiObject = function(withRequired){
const required = withRequired ? 'required' : 'optional';
return Joi.object({
post: Joi.objectId().meta({ type: 'ObjectId', ref: 'roles' })[required](),
text: Joi.string()[required](),
});
};
module.exports = getJoiObject;
3- Update comments.model.js
// 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);
};
4- comments.service.js
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
Before
const createService = require('feathers-mongoose')
After
const {createService} = require('feathers-mongoose-casl');
// 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);
};
Last updated
Was this helpful?