Stack Exchange
How to import other Sequelize models in a model for Class methods. Sequelize v 5
I am trying to define Class Methods in Sequelize Models like this
const db = require('../models');
'use strict';
module.exports = (sequelize, DataTypes) => {
  const Product = sequelize.define('Product', {
    name: DataTypes.STRING,

    categoryId: {
      type: DataTypes.INTEGER,
      references: {
        model: sequelize.Category,
        key: 'id'
      }
    },
 }, {});
  Product.associate = function(models) {
    // associations can be defined here
    Product.belongsTo(models.Category, {foreignKey: 'categoryId', as: 'category'});
    Product.hasMany(models.ProductPacking, {foreignKey: 'productId', as: 'packings'});
    Product.hasMany(models.ProductAnimal, {foreignKey: 'productId', as: 'animals'});
  };

  /*Returns all products who have active packings with details*/
  Product.getAllWithPackings = function(onlyProductsWithPacking = false) {
    return Product.findAll({include: [
        {
          model: db.ProductPacking,
          as: 'packings',
          where: {isActive: true},
          required: onlyProductsWithPacking,
          attributes: ['name', 'price', 'id', 'image'],
        },
        {
          model: db.Category,
          as: 'category',
          attributes: ['name', 'id'],
          include: {
            model: db.Category,
            as: 'parentCategory',
            attributes: ['name', 'id'],
          }
        },
        {
          model: db.ProductAnimal,
          as: 'animals',
          attributes: ['name']
        }
      ]}).then(resp => {
        return Promise.resolve(resp);
    });
  };


  return Product;
};

However when I run the class method 'getAllWithPacking()', I get the error Include unexpected. Element has to be either a Model, an Association or an object.
The associations are defined correctly and the same function will give the expected result in a controller. The issue here is that the constant 'db' is empty in that function. If I const db = require('../models'); inside the class method, the function runs fine.
Is there something that I may be missing here?

Does const db = require('../models'); contain a registration of a Product model as well?

@Anatoly Yes, it does

I recommend to move this method outside (something like ProductRepository module that may contain all CRUD and related operations with Product model) or to pass models into it instead of using db.

I may end up writing many class and instance methods, and many would use data from associations. Is this not possible in sequelize or not the recommended way?

I suppose this is not recommended way. Nevertheless you can try to use the sequelize static prop of a model to access all models you need.

You can access the model's sequelize instance, and hence the other models, through this._modelOptions.sequelize.
But if you can do it using associations, do, as otherwise some additional features (such as transactions) may not behave correctly. At least, not without some additional tinkering.
let { sequelize = { models } } = this._modelOptions;

[...]
    model: models.Name,
[...]

let { sequelize = { models } } = this._modelOptions;
let result = await models.ModelName.findAll()


(In 6.0, replace this._modelOptions with this.constructor.options)
