Vishesh Duggar bio photo

Vishesh Duggar

5x founder. Co-founder & CTO Product @vamstar. 15+ years building and advising startups of all sizes. If you're building something cool, I'd love to hear about it.

Looking for advice on product, strategy or engineering?

Book Appointment

Subscribe to my mailing list


Vamstar LinkedIn Twitter Github

Been playing with typegoose lately. The library has it’s quirks including this weird issue of queries stalling.

There are a couple of open issues highlighting the issue as I write this. For all methods like find, findOne etc the execution was simply stuck at the method call.

Essentially the problem is tha typgoose ships with mongoose as a strict dependency and expects you to pass your instance of mongoose or mongoose connection as an argument to getModelForClass.

I ended up doing that but it took forever to debug because the connection I passed wasn’t active. To make things easier to debug I wrote a small wrapper around getModelForClass.


import { Typegoose } from 'typegoose';
import { Database } from '@config/database';
import { log } from '@config/logger';


export async function getModel<T extends Typegoose>(Clazz: new (...args: any) => T) {
  const connection = await new Database().connect();

  if (connection.readyState !== 1) {
    throw new Error(`Can't get model as the database connection is not active.`);
  }

  const Model = new Clazz().getModelForClass(Clazz, {
    existingConnection: connection,
    schemaOptions: {
      timestamps: true,
    },
  });

  return Model;
}

Typegoose is quirky and some of that comes from the limitations of typescript reflection system itself. Hope this was of help.

15+ Years strategising and delivering growth, engineering, customer value and more. I have served as a CTO to multiple organizations, including Vamstar, AtruHelp, Billaway, SuperSehat, and more.

If you're a founder or CEO eager to move faster and seek tailored strategies for your unique challenges, don't navigate this journey alone. Reach out to me. Together, we can dissect, refine, and optimize your enterprise's trajectory to withstand the tests of time and innovation. Let's make your vision not just a goal, but an impending reality.

I have limited open hours. Book Appointment