FeathersJS auk server populate hook not working

im trying to use the new populate hook, but it doesn't seem to work. I just don't know what i'm doing wrong.

I have the merchatTypeMap model related 1:1 to the merchants model, and when i query the merchantTypeMap, it should answer with the associated merchant attached.

The merchantTypeMap service:

'use strict';

const service = require('feathers-sequelize');
const merchantTypeMap = require('./merchantTypeMap-model');
const hooks = require('./hooks');

module.exports = function(){
  const app = this;

  const options = {
    Model: merchantTypeMap(app.get('sequelize')),
    paginate: {
      default: 5,
      max: 25
    }
  };

  // Initialize our service with any options it requires
  app.use('/merchantTypeMaps', service(options));

  // Get our initialize service to that we can bind hooks
  const merchantTypeMapService = app.service('/merchantTypeMaps');

  // Set up our before hooks
  merchantTypeMapService.before(hooks.before);

  // Set up our after hooks
  merchantTypeMapService.after(hooks.after);
};

This is my merchantTypeMap model code:

'use strict';

// merchantTypeMap-model.js - A sequelize model
// 
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const merchantTypeMap = sequelize.define('merchantTypeMaps', {
    id: {
      type: Sequelize.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    merchantId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'merchants',
        key: 'id'
      },
      field: 'merchant_id'
    },
    merchantTypeId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'merchantTypes',
        key: 'id'
      },
      field: 'merchantType_id'
    }
  }, {
    freezeTableName: true
  });

  merchantTypeMap.belongsTo(sequelize.models.merchants, {foreignKey: 'merchant_id'});

  merchantTypeMap.sync();

  return merchantTypeMap;
};

Here my merchantTypeMap service hooks:

'use strict';

const globalHooks = require('../../../hooks');
const hooks = require('feathers-hooks-common');

const schema = {
  include: [{
    service: 'merchants',
    nameAs: 'merchantItem',
    parentField: 'merchantId',
    childField: 'id',
  }]
};

exports.before = {
  all: [],
  find: [],
  get: [],
  create: [],
  update: [],
  patch: [],
  remove: []
};

exports.after = {
  all: [],
  find: [
    hooks.populate( { schema } )
  ],
  get: [
    hooks.populate( { schema } )
  ],
  create: [],
  update: [],
  patch: [],
  remove: []
};

The merchants service:

'use strict';

const service = require('feathers-sequelize');
const merchantTypeMap = require('./merchantTypeMap-model');
const merchants = require('./merchants-model');
const hooks = require('./hooks');

module.exports = function(){
  const app = this;

  merchantTypeMap(app.get('sequelize'));

  const options = {
    Model: merchants(app.get('sequelize')),
    paginate: {
      default: 5,
      max: 25
    }
  };

  // Initialize our service with any options it requires
  app.use('/merchants', service(options));

  // Get our initialize service to that we can bind hooks
  const merchantsService = app.service('/merchants');

  // Set up our before hooks
  merchantsService.before(hooks.before);

  // Set up our after hooks
  merchantsService.after(hooks.after);
};

And the merchants model:

'use strict';

// merchants-model.js - A sequelize model
// 
// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.

const Sequelize = require('sequelize');

module.exports = function(sequelize) {
  const merchants = sequelize.define('merchants', {
    id: {
      type: Sequelize.INTEGER(10),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
      field: 'id'
    },
    title: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'title'
    },
    latitude: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'latitude'
    },
    longitude: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'latitude'
    },
    address: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'address'
    },
    icon: {
      type: Sequelize.STRING,
      allowNull: true,
      field: 'icon'
    },
    zoneId: {
      type: Sequelize.INTEGER(10),
      allowNull: true,
      references: {
        model: 'zones',
        key: 'id'
      },
      field: 'zone_id'
    }
  }, {
    freezeTableName: true
  });

  merchants.belongsToMany(sequelize.models.merchantTypes, {through: sequelize.models.merchantTypeMaps});

  merchants.sync();

  return merchants;
};

Answers:

Answer

You need to configure Sequelize to return plain JS objects, rather than its ORM objects, by using the raw: true option.

As an aside, hooks v3 will be released soon. All hooks, including populate, will then convert Sequelize and Mongoose ORMs to plain JS objects automatically. This may be a breaking chnage for some.

Related: Sequelize - How can I return JSON objects of the database results only?

[edit] The conversion from ORM objects is likely to be done in the Feathers service than by the hooks. This way the problem won't arise in custom hooks people write.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.