-1

I have three tables companies, subscriptions and companySubscription. As name defined company can canbuy/have plan or one subscription belongs to many companies.

So in model/schema I have defined as follows:

companies.js

const sequelize = require("../utils/database");
const bcrypt = require("bcrypt");
const { DataTypes, Model } = require("sequelize");
const subscription = require("./subscriptions");
const CompanySubscription = require("./companySubscription");

class companies extends Model {}

companies.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    contactNo: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    companySize: {
      type: DataTypes.INTEGER,
      allowNull: true,
    },
  },
  { sequelize, modelName: "companies" }
);

subscription.belongsToMany(companies, { through: CompanySubscription });

module.exports = companies;

subscription.js

const sequelize = require("../utils/database");

const { DataTypes, Model } = require("sequelize");

class subscription extends Model {}

subscription.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
    },
    subscriptionPlanType: {
      type: DataTypes.ENUM,
      values: ["Yearly", "Monthly"],
      allowNull: false,
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
    memberCount: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    amount: {
      type: DataTypes.FLOAT,
      allowNull: false,
    },
  },
  { sequelize, modelName: "subscription" }
);

module.exports = subscription;

companySubscription.js

const sequelize = require("../utils/database");
const companies = require("./companies");
const subscription = require("./subscriptions");
const { DataTypes, Model } = require("sequelize");

class CompanySubscription extends Model {}

CompanySubscription.init(
  {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM,
      values: ["active", "inactive"],
    },
    subscriptionType: {
      type: DataTypes.ENUM,
      values: ["Yearly", "Monthly"],
    },
    subscriptionPlanStartDate: {
      type: DataTypes.DATE,
    },
    subscriptionPlanEndDate: {
      type: DataTypes.DATE,
    },
    paidStatus: {
      type: DataTypes.ENUM,
      values: ["paid", "unpaid"],
    },
    paidDate: {
      type: DataTypes.DATE,
    },
  },
  { sequelize, modelName: "CompanySubscription" }
);

module.exports = CompanySubscription;

In controller file I am able to manage to insert the data. Below is the code:

const addBIlling = async (req, res) => {
  const foundSubcscription = await subscription.create({
    subscriptionPlanType: "Monthly",
    name: "s1",
    memberCount: 15,
    amount: 50.55,
  });

  const foundCompany = await companies.create({
    name: "company1",
    email: "company1@gmail.com",
    contactNo: "87964644",
    companySize: 20,
  });

  const insertedData = await foundSubcscription.addCompany(foundCompany, {
    through: {
      status: "active",
      paidStatus: "paid",
      subscriptionType: "Monthly",
      subscriptionPlanEndDate: moment().add(1, "months"),
      paidDate: moment().add(1, "months"),
    },
  });

  console.log("inserted data ", insertedData);
  res.json({ data: insertedData });
};

Now I want to fetch the records from db as which company has bought which subscription plan! i.e. company name, subscription plan and its active and paid status and plan's expiry date.

I tried below code:

const billingList = async (req, res) => {
  const billingData = await CompanySubscription.findAll({
    include: [{ model: companies }],
  });

  console.log("billing data ", billingData);
};

Above code is throwing error "companies is not associated to CompanySubscription!".

Where have I made a mistake?

halfer
  • 19,824
  • 17
  • 99
  • 186
Jayna Tanawala
  • 475
  • 10
  • 27

1 Answers1

0

Don't try to import models to each other's modules directly. Define model registration functions in each model module and use them all to register models in one place/module and for associations you can define associate function inside each registration function and call them after ALL your models are already registered. That way you won't have cyclic dependencies and all associations will be correct.

See my answer here to get an idea how to do it.

Anatoly
  • 20,799
  • 3
  • 28
  • 42