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

express-multitenancy-mongoose

v0.0.4

Published

MongoDB-based tenant storage and automatic filtering for express-multitenancy

Readme

express-multitenancy-mongoose

Build Status Version License Downloads TypeScript

A MongoDB-based tenant storage implementation for the express-multitenancy package

Getting StartedFeaturesInstallationUsageAPI ReferenceAdvanced UsageLicense

🌟 Overview

@express-multitenancy/mongoose-store provides seamless integration between express-multitenancy and Mongoose, enabling automatic tenant filtering and database separation in multi-tenant applications. This package makes building MongoDB-backed multi-tenant applications simple and secure.

✨ Features

  • 💾 MongoDB-based tenant storage for production-ready applications
  • 🔍 Automatic tenant filtering for all Mongoose queries
  • 🔐 Transparent tenant ID assignment for new documents
  • 🌐 Support for exempt models (global resources unaffected by tenant filtering)
  • 📝 TypeScript support with full type definitions

📦 Installation

# Using npm
npm install express-multitenancy-mongoose mongoose express-multitenancy

# Using yarn
yarn add express-multitenancy-mongoose mongoose express-multitenancy

# Using pnpm
pnpm add express-multitenancy-mongoose mongoose express-multitenancy

🚀 Quick Start

const express = require('express');
const mongoose = require('mongoose');
const { multitenancy, HeaderStrategy } = require('express-multitenancy');
const { MongooseStore, multitenancyPlugin } = require('express-multitenancy-mongoose');

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/multitenancy-app');

// Create a tenant store
const store = new MongooseStore({
  connection: mongoose.connection
});

// Apply multitenancy plugin globally to all Mongoose schemas
mongoose.plugin(multitenancyPlugin);

const app = express();

// Apply multitenancy middleware
app.use(multitenancy({
  strategies: [new HeaderStrategy('x-tenant-id')],
  store
}));

// All Mongoose queries will now be automatically filtered by tenant
const Product = mongoose.model('Product', new mongoose.Schema({
  name: String,
  price: Number
  // tenantId is added automatically by the plugin
}));

app.get('/products', async (req, res) => {
  // This query will only return products for the current tenant
  const products = await Product.find();
  res.json(products);
});

app.listen(3000);

📖 API Reference

MongooseStore

const store = new MongooseStore(options);

Options

  • connection (required): Mongoose database connection
  • modelName (optional): Name for the Mongoose model (default: 'Tenant')
  • schema (optional): Custom schema for the tenant model
  • model (optional): Custom pre-defined Mongoose model

multitenancyPlugin

// Apply to a specific schema
userSchema.plugin(multitenancyPlugin, options);

// Apply globally to all schemas
mongoose.plugin(multitenancyPlugin, options);

Options

  • exemptModels (optional): Array of model names that should be exempt from tenant filtering
  • hideTenantId (optional): Whether to hide tenantId in the response (default: false)
  • debug (optional): Whether to enable debug logging (default: false)

🔧 Advanced Usage

Using a Custom Tenant Schema

const mongoose = require('mongoose');
const { MongooseStore } = require('express-multitenancy-mongoose');

// Create custom tenant schema
const customTenantSchema = new mongoose.Schema({
  id: { type: String, required: true, unique: true },
  name: { type: String, required: true },
  // Add custom fields
  domain: String,
  plan: { type: String, enum: ['free', 'premium', 'enterprise'] },
  createdAt: { type: Date, default: Date.now }
});

// Use the custom schema in MongooseStore
const store = new MongooseStore({
  connection: mongoose.connection,
  schema: customTenantSchema
});

Exempting Models from Tenant Filtering

Some models in your application might be global and not tenant-specific (like settings or shared resources). You can exempt these models from tenant filtering:

const { multitenancyPlugin, exemptModels } = require('express-multitenancy-mongoose');

// Add models to exempt list
exemptModels.add('GlobalSettings');
exemptModels.add('SharedResources');

// Or use the plugin options
mongoose.plugin(multitenancyPlugin, {
  exemptModels: ['GlobalSettings', 'SharedResources']
});

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

📄 License

MIT