Vishesh Duggar bio photo

Vishesh Duggar

Founder @ CauseCode. Startup tech guy with a keen interest in product development and usability. Interested in working on high impact ideas.

Subscribe to my mailing list

Are you looking for web/mobile development help or a CTO?

Contact

CauseCode LinkedIn Twitter Facebook 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 a quirky and some of that comes from the limitations of typescript reflection system itself. Hope this was of help.

We do web/mobile apps and have expertise in building scalable applications 30% faster.

Whether you are starting out or have already made some progress, 
we can help you build a scalable app faster and cheaper.

Contact me personally at http://scr.im/visheshd