Skip to content

This project provides a simple way to manage multiple MongoDB databases for a multi-tenant application using Mongoose.

License

Notifications You must be signed in to change notification settings

waleedahmad-dev/multi-tenant-manager-mongdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Tenant MongoDB Manager

This project provides a simple way to manage multiple MongoDB databases for a multi-tenant application using Mongoose.

Installation

npm install mongoose

Usage

Importing the MongoDBManager

const MongoDBManager = require("./mongodb-manager");

Adding Databases

await MongoDBManager.addDatabase(
  "tenant1",
  "mongodb://localhost:27017/tenant1"
);
await MongoDBManager.addDatabase(
  "tenant2",
  "mongodb://localhost:27017/tenant2"
);

Defining Schemas

const userSchema = {
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  createdAt: { type: Date, default: Date.now },
};

const productSchema = {
  name: String,
  price: Number,
  inventory: Number,
};

Registering Models

MongoDBManager.getModel("tenant1", "User", userSchema, { timestamps: true });
MongoDBManager.getModel("tenant2", "User", userSchema);
MongoDBManager.getModel("tenant1", "Product", productSchema);

Creating Documents

const user1 = await MongoDBManager.create("tenant1", "User", {
  name: "John Doe",
  email: "john@example.com",
});

Finding Documents

const products = await MongoDBManager.find("tenant1", "Product", {
  price: { $lt: 100 },
});

Updating Documents

await MongoDBManager.updateOne(
  "tenant2",
  "User",
  { email: "john@example.com" },
  { $set: { name: "Johnny Doe" } }
);

Deleting Documents

await MongoDBManager.deleteOne("tenant1", "User", {
  email: "john@example.com",
});

Aggregating Documents

const results = await MongoDBManager.aggregate("tenant1", "Product", [
  { $match: { price: { $gt: 50 } } },
  { $group: { _id: "$category", total: { $sum: "$price" } } },
]);

Disconnecting Databases

await MongoDBManager.disconnect("tenant1");
await MongoDBManager.disconnectAll();

Error Handling

All methods throw errors if the specified database or model is not found. Ensure to use try-catch blocks to handle these errors gracefully.

try {
  // ...your code...
} catch (error) {
  console.error("Application error:", error);
}

License

This project is licensed under the MIT License.

About

This project provides a simple way to manage multiple MongoDB databases for a multi-tenant application using Mongoose.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published