Search code examples
feathersjsfeathers-sequelize

In feathers.js, how do I create an associated object after creating the initial object?


In a feathersjs project, I have two models: user and company. I'm using Sequelize/MySQL.

Every user has one company. Every company belongs to one user.

When a user signs up (is created) I want to create the company object at the same time (with just blank data that can be edited later but with the correct association).

How do I do this with a user after:create hook?


Solution

  • Problem solved. The hook object has access to the app. So the solution:

    1. generate an after:create hook on the user service ("feathers generate hook")
    2. in the hook that is generated, create a company with:

      return hook.app.service('companies').create({userId:
      hook.result.id}).then(()=> {return hook});