Introduction
Monpulse is a package that helps you to connect to MongoDB database and perform CRUD operations for nodejs.
Why Monpulse?
There are few packages that manages MongoDB for nodejs applications, most popular one is Mongoose. But Mongoose is in somehow a little bit hard to use, also there is Prisma which is a great package, it manages multiple databases drivers, but not the best for MongoDB.
Monpulse Features
- Easy to use: Monpulse is very easy to use, it's just a wrapper around MongoDB driver.
- Supports multiple connections: You can perform multiple connections to different MongoDB connections and use each one of them separately.
- Supports multiple databases: Monpulse supports multiple databases, you can connect to multiple databases at the same time.
- Powerful Aggregate framework: Monpulse has a powerful aggregate framework that helps you to perform complex queries.
- Basic CRUD operations: Monpulse supports basic CRUD operations, you can perform create, read, update and delete operations.
- Events Driven: Monpulse is events driven, you can listen to events and perform actions, for example before creating, updating or deleting a document.
- Powerful Models: Monpulse has a powerful models system, a Model is a collection manager document based, it manages a collection's document easily with many utilities.
- Learning curve: Monpulse has a very small learning curve, you can learn it in few minutes.
- Pagination support: Monpulse supports pagination, you can paginate your results easily.
- Output formatting: Monpulse supports output formatting, you can format your output easily when model is sent as a response.
- Auto incremented id: Monpulse supports auto incremented id, you can use it as a primary key for your documents.
- Random or sequential id: Monpulse supports random or sequential id.
- Recycle Bin: Reduce collection documents by removing the document entirely from the collection, but move it to a separate collection trash.
- Migration system: Monpulse has a migration system, you can create migrations and run them easily.
- Data casting: You can cast your data to a specific type or using custom casting.
- Embedded documents: Monpulse supports single and multiple embedded documents, you can embed documents inside other documents.
- Syncing Models: Auto update documents when model's data is updated or deleted.
And many more features.
Peek inside Monpulse
Here is a simple example of defining a User model:
src/models/user.ts
import { Model } from "@mongez/monpulse";
export class User extends Model {
/**
* The collection name
* Must be defined explicitly.
*/
public static collection = "users";
}
A quick example of creating a user:
src/controllers/users.ts
import { User } from "src/models/user";
export async function createUser() {
const user = await User.create({
name: "Hasan Zohdy",
email: "hassanzohdy@gmail.com",
});
console.log(user.data);
}
Outputs something similar to:
{
"id": 1231412,
"_id": "fagtrw43qwedasjoijwq",
"name": "Hasan Zohdy",
"email": "hassanzohdy@gmail.com",
"createdAt": "2023-06-01 00:00:00",
"updatedAt": "2023-06-01 00:00:00"
}