sequelize.jsfeathersjsfeathers-sequelizefeathers-hook

feathers-sequelize Left Join Database Postgresql


I am very new at feathersjs, i'm stuck on a database query which uses LEFT JOIN. I have read the sequelizejs, but i'm stuck. I have 2 tables.

Table 1:

                                              Table "public.ek_jabatan"
     Column     |           Type           |                            Modifiers                            
----------------+--------------------------+-----------------------------------------------------------------
 id_jabatan     | integer                  | not null default nextval('ek_jabatan_id_jabatan_seq'::regclass)
 id_instansi    | integer                  | not null
 nama_jabatan   | character varying(100)   | not null
 urutan_jabatan | integer                  | not null
 createdAt      | timestamp with time zone | 
 updatedAt      | timestamp with time zone | 
Indexes:
    "ek_jabatan_pkey" PRIMARY KEY, btree (id_jabatan)

Table 2 :

                                          Table "public.ek_instansi"
    Column     |           Type           |                             Modifiers                             
---------------+--------------------------+-------------------------------------------------------------------
 id_instansi   | integer                  | not null default nextval('ek_instansi_id_instansi_seq'::regclass)
 nama_instansi | character varying(100)   | not null
 lokasi        | character varying(200)   | not null
 createdAt     | timestamp with time zone | 
 updatedAt     | timestamp with time zone | 
Indexes:
    "ek_instansi_pkey" PRIMARY KEY, btree (id_instansi)

In PHP it is not difficult to make Query:

SELECT id_jabatan, a.id_instansi, nama_jabatan, urutan_jabatan, nama_instansi, lokasi 
FROM ek_jabatan a 
LEFT JOIN ek_instansi b 
ON b.id_instansi = a.id_instansi

How to make Query like that on FeatherJS ???

i'have tried,

users.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {

    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: false
    },
    statusId: {
      type: Sequelize.INTEGER,
      field: 'status_id'
    }


  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  // eslint-disable-next-line no-unused-vars
  users.associate = function(models){
    users.hasOne(models.userStatus, {
        as: 'status',
        foreignKey: 'id'
    });
};

  return users;
};

user_status.model.js

const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const userStatus = sequelizeClient.define('user_status', {
    name: {
      type: DataTypes.STRING,
      allowNull: true
    }
  }, {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  userStatus.associate = function (models) {
  };

  return userStatus;
};

users.hooks.js

    before: {
    all: [],
    find: [ 
      authenticate('jwt'),

      context => {
        const sequelize = context.params.sequelize || {};
        sequelize.raw = true;
        sequelize.include = [
          {
            model: context.app.services['userStatus'].Model,
            as: 'status'
          }
        ];
        return context;
      },



    ],
    get: [ authenticate('jwt') ],
    create: [ hashPassword() ],
    update: [ hashPassword(),  authenticate('jwt') ],
    patch: [ hashPassword(),  authenticate('jwt') ],
    remove: [ authenticate('jwt') ]
  },

When npm start, i got error. throw new Error(this.name + '.' + Utils.lowercaseFirst(Type.toString()) + ' called with something that\'s not a subclass of Sequelize.Model');


Solution

  • You're not setting the "context.params.sequelize" correctly in your hook. You need to do something like this:

    context => {
      // Make sure the object exists
      if(!context.params.sequelize) {
        context.params.sequelize = {};
      }
    
      const sequelize = context.params.sequelize;
      sequelize.raw = true;
      sequelize.include = [
        {
          model: context.app.services['userStatus'].Model,
          as: 'status',
          required: false, // Will always ensure LEFT JOIN
        }
      ];
      return context;
    }