npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jperezmart/nest-mongodb

v0.1.1

Published

Native MongoDB driver integration for NestJS — @nestjs/mongoose-style API without an ODM. ESM + CJS.

Readme

@jperezmart/nest-mongodb

npm version license

Native MongoDB driver integration for NestJS — the same surface as @nestjs/mongoose, but using the official mongodb driver (MongoClient / Db / Collection) instead of an ODM. Ships ESM + CJS with type definitions for both.

Why

@nestjs/mongoose is great, but Mongoose brings schemas, hooks, plugins and discriminators you may not want. This package keeps the ergonomic NestJS module API — forRoot, forFeature, injection decorators, connection retry and clean shutdown — and hands you the raw driver objects.

Install

pnpm add @jperezmart/nest-mongodb mongodb

@nestjs/common, @nestjs/core, reflect-metadata and rxjs are peer dependencies (you already have them in a Nest app).

API at a glance (vs @nestjs/mongoose)

| @nestjs/mongoose | @jperezmart/nest-mongodb | | ----------------------------------- | -------------------------------------------- | | MongooseModule.forRoot(uri, opts) | MongoModule.forRoot(uri, opts) | | MongooseModule.forRootAsync(opts) | MongoModule.forRootAsync(opts) | | MongooseModule.forFeature(models) | MongoModule.forFeature(collections) | | @InjectConnection() | @InjectClient() (sessions) / @InjectDb() | | @InjectModel('Cat') | @InjectCollection('cats') | | connection.startSession() | MongoTransactionService.withTransaction() |

Option names and defaults mirror @nestjs/mongoose: retryAttempts (default 9), retryDelay (default 3000 ms), connectionName, connectionFactory, connectionErrorFactory, lazyConnection, onConnectionCreate, verboseRetryLog, plus dbName and any MongoClientOptions inline.

Usage

Root connection

import { MongoModule } from '@jperezmart/nest-mongodb';

@Module({
  imports: [
    MongoModule.forRoot('mongodb://localhost:27017', { dbName: 'app' }),
  ],
})
export class AppModule {}

Async configuration:

MongoModule.forRootAsync({
  imports: [ConfigModule],
  inject: [ConfigService],
  useFactory: (config: ConfigService) => ({
    uri: config.getOrThrow('MONGO_URI'),
    dbName: 'app',
  }),
});

Enable shutdown hooks so the client closes on exit:

const app = await NestFactory.create(AppModule);
app.enableShutdownHooks();

Collections

import { MongoModule } from '@jperezmart/nest-mongodb';

@Module({ imports: [MongoModule.forFeature(['cats'])] })
export class CatsModule {}
import { InjectCollection } from '@jperezmart/nest-mongodb';
import type { Collection } from 'mongodb';

@Injectable()
export class CatsService {
  constructor(
    @InjectCollection('cats') private readonly cats: Collection<Cat>,
  ) {}

  findAll() {
    return this.cats.find().toArray();
  }
}

forFeature also accepts full definitions and a connection name:

MongoModule.forFeature([
  { name: 'cats', options: { readPreference: 'secondary' } },
]);
MongoModule.forFeature(['events'], 'analytics');

Transactions

Requires a replica set (or mongos). withTransaction commits on success and aborts + rethrows on error:

import { MongoTransactionService } from '@jperezmart/nest-mongodb';

@Injectable()
export class BankService {
  constructor(
    @InjectCollection('accounts')
    private readonly accounts: Collection<Account>,
    private readonly tx: MongoTransactionService,
  ) {}

  transfer(from: string, to: string, amount: number) {
    return this.tx.withTransaction(async session => {
      await this.accounts.updateOne(
        { _id: from },
        { $inc: { balance: -amount } },
        { session },
      );
      await this.accounts.updateOne(
        { _id: to },
        { $inc: { balance: amount } },
        { session },
      );
    });
  }
}

Multiple connections

MongoModule.forRoot('mongodb://primary', { dbName: 'app' });
MongoModule.forRoot('mongodb://analytics', {
  dbName: 'metrics',
  connectionName: 'analytics',
});
constructor(
  @InjectClient('analytics') private readonly client: MongoClient,
  @InjectDb('analytics') private readonly db: Db,
) {}

MongoTransactionService is bound to the default connection. For a named connection, inject its client with @InjectClient(name) and call startSession() yourself.

License

MIT