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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@karin-js/mongoose

v1.1.15

Published

MongoDB integration plugin for Karin-JS using Mongoose ODM with enterprise-grade error handling.

Readme

@karin-js/mongoose

MongoDB integration plugin for Karin-JS using Mongoose ODM with enterprise-grade error handling.

Installation

bun add @karin-js/mongoose mongoose

Overview

The Mongoose plugin provides:

  • ✅ MongoDB connection management
  • ✅ Schema-based models with decorators
  • ✅ Automatic error handling (ValidationError, CastError, etc.)
  • ✅ Dependency injection for models
  • ✅ Lazy configuration resolution

Quick Start

import { Schema, Prop, InjectModel } from "@karin-js/mongoose";
import { Model } from "mongoose";

// Define a schema
@Schema()
class User {
  @Prop({ required: true })
  name: string;

  @Prop({ required: true, unique: true })
  email: string;

  @Prop()
  age?: number;
}

// Use in a service
@Service()
class UsersService {
  constructor(@InjectModel(User) private userModel: Model<User>) {}

  async create(data: Partial<User>) {
    return this.userModel.create(data);
  }

  async findAll() {
    return this.userModel.find();
  }
}

// Configure plugin
const app = await KarinFactory.create(adapter, {
  plugins: [
    new MongoosePlugin({
      uri: "mongodb://localhost:27017/mydb",
      models: [User], // Explicitly register models
    }),
  ],
});

Features

Schema Decorators

Define MongoDB schemas with TypeScript decorators:

import { Schema, Prop } from "@karin-js/mongoose";

@Schema({ timestamps: true })
class Product {
  @Prop({ required: true, minlength: 3 })
  name: string;

  @Prop({ required: true, min: 0 })
  price: number;

  @Prop({ type: [String], default: [] })
  tags: string[];

  @Prop({ type: Object })
  metadata?: Record<string, any>;
}

Model Injection

Inject models into services:

import { InjectModel } from "@karin-js/mongoose";
import { Model } from "mongoose";

@Service()
class ProductsService {
  constructor(@InjectModel(Product) private productModel: Model<Product>) {}

  async findByTag(tag: string) {
    return this.productModel.find({ tags: tag });
  }
}

Automatic Error Handling

The plugin automatically handles Mongoose errors:

// ValidationError → 400 Bad Request
{
  "statusCode": 400,
  "error": "Validation Error",
  "message": "The provided data is invalid",
  "details": [
    {
      "field": "email",
      "message": "Path `email` is invalid (not-an-email)."
    }
  ]
}

// CastError → 400 Bad Request
{
  "statusCode": 400,
  "error": "Invalid ID",
  "message": "The value 'invalid-id' is not a valid ID"
}

// Duplicate Key → 409 Conflict
{
  "statusCode": 409,
  "error": "Duplicate Entry",
  "message": "A record with this email already exists"
}

Lazy Configuration

Use with ConfigPlugin for dynamic configuration:

const config = new ConfigPlugin({
  requiredKeys: ["MONGO_URI", "DB_NAME"],
});

const app = await KarinFactory.create(adapter, {
  plugins: [
    config,
    new MongoosePlugin({
      uri: () => config.get("MONGO_URI"),
      options: () => ({
        dbName: config.get("DB_NAME"),
        authSource: "admin",
      }),
      models: [User, Product], // Always register models explicitly
    }),
  ],
});

Model Registration

It is required to explicitly register your models in the plugin configuration. This ensures that Mongoose loads them correctly in all environments, including Serverless (Cloudflare Workers, Deno Deploy) and standard Bun runtimes.

new MongoosePlugin({
  uri: "mongodb://...",
  models: [User, Product, Order], // Required for both Server and Serverless
})

Serverless Configuration

When running in serverless environments (AWS Lambda, Cloudflare Workers, etc.), it is crucial to optimize connection pooling to avoid exhausting database connections.

new MongoosePlugin({
  uri: process.env.MONGO_URI,
  options: () => ({
    maxPoolSize: 2, // Keep this low (1-10) for serverless
    minPoolSize: 0,
    serverSelectionTimeoutMS: 5000, // Fail fast
  }),
  models: [User],
})

API

MongoosePlugin Options

interface MongoosePluginOptions {
  // MongoDB connection URI
  uri: string | (() => string);

  // Mongoose connection options
  options?: ConnectOptions | (() => ConnectOptions);

  // Models to register (for serverless)
  models?: Function[];

  // Auto-register exception filter (default: true)
  autoRegisterExceptionFilter?: boolean;

  // Enable serverless mode (reuses connections, skips disconnect)
  // Default: Automatically detected via environment variables (AWS_LAMBDA_FUNCTION_NAME, VERCEL, etc.)
  serverless?: boolean;
}

Decorators

  • @Schema(options?) - Define a Mongoose schema
  • @Prop(options?) - Define a schema property
  • @InjectModel(model) - Inject a Mongoose model

Error Filter

Disable automatic error handling if needed:

new MongoosePlugin({
  uri: "...",
  autoRegisterExceptionFilter: false, // Provide your own filter
})

Best Practices

  1. Use schemas for data validation
  2. Inject models instead of importing them
  3. Enable timestamps for audit trails
  4. Use indexes for performance
  5. Handle errors at the service layer when needed

License

MIT