user8066411
user8066411

Reputation:

Cache MongoDb connection with Next.js 10 TypeScript Project - API Route

I'm trying to convert next.js/examples/with-mongodb/util/mongodb.js to TS so I can cache and resue my connections to Mongo within a TS next.js project. I'm getting a TS error on cache.promise that says: Type 'Promise<MongoClient | { client: MongoClient; db: Db; }>' is not assignable to type 'Promise<MongoClient>'

How should I properly declare the mongo property on global to appease the TS gods?

import { MongoClient, Db } from "mongodb";

const { DATABASE_URL, DATABASE_NAME } = process.env;

declare global {
  namespace NodeJS {
    interface Global {
      mongo: {
        conn: MongoClient | null;
        promise: Promise<MongoClient> | null;
      };
    }
  }
}

let cached = global.mongo;

if (!cached) {
  cached = global.mongo = { conn: null, promise: null };
}

async function connect() {
  if (cached.conn) {
    return cached.conn;
  }

  if (!cached.promise) {
    const opts = {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    };

    cached.promise = MongoClient.connect(DATABASE_URL, opts).then((client) => {
      return {
        client,
        db: client.db(DATABASE_NAME),
      };
    });
  }
  cached.conn = await cached.promise;
  return cached.conn;
}

export { connect };

Upvotes: 5

Views: 3044

Answers (3)

illia chill
illia chill

Reputation: 2056

You don't need to cache your connection, check latest nextjs with mongodb example. The official mongodb forum experts have navigated me to this example project.

Try to use native solutions

Upvotes: 2

lpke
lpke

Reputation: 599

The 'conn' property you are storing contains both MongoClient and Db.

In your global declaration for mongo, you have only included MongoClient. I have the exact same code in my project and the way I handle this is to simply create a basic type called MongoConnection which contains both. Code below.

type MongoConnection = {
  client: MongoClient;
  db: Db;
};

declare global {
  namespace NodeJS {
    interface Global {
      mongo: {
        conn: MongoConnection | null;
        promise: Promise<MongoConnection> | null;
      }
    }
  }
}

Upvotes: 1

user8066411
user8066411

Reputation:

seems like the answer is to just make the mongo property an any like this:

declare global {
  namespace NodeJS {
    interface Global {
      mongo: any;
    }
  }
}

Upvotes: 0

Related Questions