0

I have a Mongodb based Feathers service : Categories and the model is as follows

{
  _id : 'SOMEKEY',
  categoryName : 'XYZ'
  categoryItems : [
    { item_id : 'ITEMID' }
  ]
}

Now I have After hooks on my Items service that on each Create I want to push the reference of that Item into the categoryItems array in the Categories document. How can I achieve that ? Neither of the built in methods for Category service will help. Should I be writing Mongoose queries within my hook? If I do that I am depending on Mongoose MongoDB and if my database changes I will have to change all my hooks.

TJ_
  • 255
  • 3
  • 12

1 Answers1

0
const categoryService = hook.app.service('categories');
      categoryService.update(hook.data.item_category,
                            {
                                $push: {
                                "category_items": {
                                        item_id: hook.result._id
                                    }
                                }
                            },
                             {
                                 safe: true, // This value is safely stored in the DB
                                 upsert: true // It creates a new object if
                                              // a previous one does not exist
                             });
TJ_
  • 255
  • 3
  • 12