feathers-sequelize左联接数据库Postgresql

时间:2018-11-29 16:50:02

标签: sequelize.js feathersjs feathers-sequelize feathers-hook

我在feathersjs上是一个新手,我被困在使用LEFT JOIN的数据库查询中。我已经读过sequelizejs,但是我被卡住了。我有2张桌子。

表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)

表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)

在PHP中进行查询并不困难:

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

如何在FeatherJS上进行类似的查询?

我已经尝试过

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') ]
  },

启动npm时,出现错误。 抛出新的错误(this.name +'。'+ Utils.lowercaseFirst(Type.toString())+'以不是Sequelize.Model的子类的东西调用);

1 个答案:

答案 0 :(得分:0)

您没有在钩子中正确设置“ context.params.sequelize”。您需要执行以下操作:

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;
}