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

2- Create validator file

inside validators create comments.validators.js with this contnet

comments.validators.js
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

Need to create a native mongos schema?

createModelFromJoi(app, 'comments', commentsValidators, mongooseSchema);

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

After

You can add to options serviceRules to allow "Admin" or any other roles to manage this new service

Last updated

Was this helpful?