My First NPM Package: `create-dbconnect` — No More Repeating Boilerplate!

June 9, 2025 (6mo ago)

🚀 Introduction

Every time I created a new Next.js project with MongoDB, I found myself repeating the same task —
writing a dbConnect.ts file with the same mongoose.connect logic, .env handling, and global caching.

It wasn’t just repetitive — it was boring and error-prone when done in a rush.

So I decided to turn that repetition into a reusable, zero-config CLI tool:

npx create-dbconnect

This was my first ever published NPM package, and I’m excited to share what it does, how to use it, and how it works under the hood.


💡 What Problem Does It Solve?

When you're working with Next.js + TypeScript + MongoDB, you often need the same exact dbConnect.ts file to:

Instead of:

You can now just run:

npx create-dbconnect

And you're done ✅

This tool:


⚙️ How to Use It

Inside your Next.js project directory, run:

npx create-dbconnect

It will generate the following structure:

src/
└── app/
    └── lib/
        └── dbConnect.ts

And it installs:

Now you can simply use the connection function like this:

import dbConnect from "@/app/lib/dbConnect";
 
await dbConnect();

No extra setup needed. It just works.


🛠️ What’s Inside the File?

The generated dbConnect.ts file contains:

Here’s the full file:

import mongoose from 'mongoose';
 
declare global {
  var mongoose: {
    conn: typeof mongoose | null;
    promise: Promise<typeof mongoose> | null;
  } | undefined;
}
 
const MONGODB_URI = process.env.MONGODB_URI || 'your-mongo-uri-here';
 
let cached = global.mongoose;
 
if (!cached) {
  cached = global.mongoose = { conn: null, promise: null };
}
 
const connectToDatabase = async () => {
  if (cached.conn) return cached.conn;
 
  if (!cached.promise) {
    cached.promise = mongoose.connect(MONGODB_URI, {
      bufferCommands: true,
    }).then((mongoose) => mongoose);
  }
 
  cached.conn = await cached.promise;
  return cached.conn;
};
 
export default connectToDatabase;

🧠 How It Works Internally

Behind the scenes, the package does the following:

  1. Locates or creates the src/app/lib/ folder
  2. Copies a pre-written dbConnect.ts template file into it
  3. Checks if mongoose is already installed
  4. If not, it runs:

npm install mongoose

All this is handled through a Node.js CLI using:

The goal was to keep it simple, clean, and automatic.


📦 Why This Package Matters to Me

This wasn’t just about boilerplate — it was also about:

It’s a small step, but a meaningful one in my developer journey.


✍️ A Blog on “How I Built This” is Coming Soon

This post was about what the tool does and why it’s useful.

But if you're curious about:

👉 I’ll be publishing a follow-up blog soon explaining how I built create-dbconnect from scratch.


🙌 Wrap-Up

Thanks for reading! If you try out create-dbconnect or find it useful, feel free to share feedback or contribute.

You can check out the package here: 👉 https://www.npmjs.com/package/create-dbconnect

More updates coming soon — including a detailed guide on publishing your first CLI package to NPM.

Stay tuned 🚀