postgresqlpostgetfeathersjsfeathers-sequelize

How to craft a GET and POST request for a One-To-Many Associated Database in FeathersJS?


I am using FeatherJS with feathers-sequelize and postgresql to expose an endpoint and store information in a database. I think I have been able to set up a 1 to Many relation in my Postgresql database, but I do not know how to test it.

I followed the POST request format in this tutorial: https://www.djamware.com/post/5bb1f05280aca74669894417/node-express-sequelize-and-postgresql-association-example but in Postman instead. IN pgAdmin, my company table is populated, but my branch table remains empty.

I set up the association like this:

In my Company script:

(company as any).associate = function (models: any) {
    company.hasMany(models.branches, { foreignKey: 'companyId', sourceKey: "companyId" });
  };

In my Branch script:

(branch as any).associate = function (models: any) {
    branch .belongsTo(models.company, { foreignKey: 'companyId', targetKey: 'companyId' });
  };

I expected both my tables to be populated with companyId as a foreign key in my branch table. However, only my company table is populated with the information from the POST request, while my branch table remains empty.


Solution

  • Have a look at this FAQ how to retrieve associations with Sequelize:

    // Find Hulk Hogan and include all the messages he sent
    app.service('branches').find({
      sequelize: {
        include: [{
          model: Company
        }]
      }
    });
    

    More information can be found in the adapter documentation.