question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How are people dealing with circular dependencies?

See original GitHub issue

I have models define in separate files that reference each other. Is there a way to deal with circular dep issues without combining the models into one file?

import { Table, Column, Model, HasOne } from 'sequelize-typescript';
import User from './User.model';

@Table({ timestamps: true })
export default class Post extends Model {
  @HasOne(() => User)
  author!: User;

  @Column
  title!: string;

  @Column
  content!: string;
}
import { Table, Column, Model, HasMany } from 'sequelize-typescript';
import Post from './Post.model';

@Table({ timestamps: true })
export default class User extends Model {
  @Column
  name!: string;

  @HasMany(() => Post)
  posts!: Post[];
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

3reactions
mschipperheyncommented, Jan 25, 2022

IMHO the code below should be adjusted to help avoid the need for recursive imports:

private associateModels(models: ModelCtor[]): void {
    models.forEach((model) => {
      const associations = getAssociations(model.prototype);

      if (!associations) return;

      associations.forEach((association) => {
        const options = association.getSequelizeOptions(model, this);
        // const associatedClass = this.model(association.getAssociatedClass());
        
        // should become
        const associatedClass = this.model(association.getAssociatedClass(models));
        
        if (!associatedClass.isInitialized) {
          throw new ModelNotInitializedError(
            associatedClass,
            `Association between ${associatedClass.name} and ${model.name} cannot be resolved.`
          );
        }
        model[association.getAssociation() as any](associatedClass, options as any);
      });
    });
  }

So that

import Company from './Company';
[...]
@BelongsTo(() => Company, {
    foreignKey: 'companyId',
    as: 'company',
})
public company!: Company;

becomes

@BelongsTo((models) => models.Company, {
    foreignKey: 'companyId',
    as: 'company',
})
public company!: Company;
2reactions
mschipperheyncommented, Jan 25, 2022

@RobinBuschmann I took an initial stab at this. Would be great if someone could give me some feedback on whether this is a good idea or not: https://github.com/mschipperheyn/sequelize-typescript/commit/89bbef67201394d2fe4d832a8e2e8c4565e67bb7

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Eliminate Circular Dependencies from Your JavaScript ...
Circular dependencies are usually an indication of bad code design, and they should be refactored and removed if at all possible.
Read more >
How to solve circular dependency?
Technically, you can resolve any cyclic dependency by using interfaces, as shown in the other answers. However, I recommend to rethink your ...
Read more >
Circular Dependencies in Spring - Baeldung
A quick writeup on dealing with circular dependencies in Spring: how they occur and several ways to work around them.
Read more >
How to fix nasty circular dependency issues once and for all in ...
Although there are many strategies and best practices on how to avoid circular dependencies. There is very little on how to fix them...
Read more >
Circular dependency - Wikipedia
In software engineering, a circular dependency is a relation between two or more modules ... (those that use reference counting) from deallocating unused...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found