abimongo v1.0.38
Abimongo: Interact Easy with MongoDB
Abimongo is a lightweight library. It provides an easy-to-use, object-oriented interface for performing MongoDB operations.
Installation
Make sure you have Node.js and MongoDB installed to use this library
To install the Abimongo library, use npm:
npm install abimongo
Usage
Connect to Database
Connect to a MongoDB database using the abimongo.connect function.
//import dbQuery from "abimongo";
import abimongo from 'abimongo'
export const connectDb = async () => {
try {
//const db = await dbQuery();
/**
* Do this
*/
const db = abimongo.connect(
'mongodb://127.0.0.1:27017',
{dbName: 'test', collection: 'testUser'}
)
// const client = await new db(
// 'mongodb://127.0.0.1:27017',
// 'test1',
// 'users'
// );
// await client.connect();
db.client?.connect() // You will be connected even if you dont do this.
console.log('Connected to database');
return db;
} catch (err) {
console.log(err);
}
}
Create a Schema
Use the Schema() method to create a schema. Import the Builder function. The Builder method have properties that are useful for performing other tasks.
import { Builder, SchemaDocType } from "abimongo";
const builder = Builder;
export interface userDocment extends Document {
name: string;
email: string;
password: string;
role: string;
created_at?: string;
updated_at?: string;
};
const userSchema = builder.Schema({
username: { type: String, required: true },
password: { type: String, required: true },
email: { type: String, required: true },
role: { type: String, required: true },
created_at: { type: Date },
updated_at: { type: Date }
}) as SchemaDocType<userDocment, {}>;
// Hear you can use abimongo.model as alternative instead of builder.model but
// abimongo has no Schema function Builder does.
const UserModel = builder.model("user", userSchema);
- or
const UserModel = abimongo.model<userDocment>("user", userSchema);
Consume the UserModel
The UserModel can be used to perform CRUD operations. Below are examples.
import { ObjectId } from "mongodb";
import { connectDb } from "./dbConfig";
type User = {
_id?: string;
username: string;
password: string;
email: string;
age: number;
role: string;
};
export const register = async (data: User) => {
const db = await connectDb();
const userDb = await db;
const userData = await UserModel.then((res: any) => {
res = data;
return res;
});
if (userData !== undefined) {
await userDb?.create(userData);
console.log('User registered');
}
return userData;
}
export const getUserById = async (id: ObjectId) => {
const db = await connectDb();
const userId = new ObjectId(id);
const user = await db?.findById(userId);
console.log(user);
if (!user) {
console.log('No user found with that id');
}
return user;
}
export const getAllUsers = async () => {
const db = await connectDb();
const users = await db?.find({});
if (!users) {
console.log('No data found');
}
return users;
}
export const updateUser = async (id: string, data: User) => {
const db = await connectDb();
const query = await db;
const userId = new ObjectId(id);
const user = await query?.updateOne(userId, data);
console.log(user);
return user;
}
export const deleteUser = async (id: string) => {
const db = await connectDb();
const query = await db;
const user = await query?.deleteOne({ _id: id });
console.log("User deleted");
return user?.deletedCount;
}
Contributing
Contributions are welcome! Please follow these steps to contribute:
Fork the repository. Create a new branch (git checkout -b feature-branch). Make your changes. Commit your changes (git commit -m 'Add some feature'). Push to the branch (git push origin feature-branch). Open a pull request.
Conclusion
Abimongo is a simple and lightweight ORM for MongoDB that makes it easy to perform CRUD operations in an object-oriented way. We hope you find it useful for your projects.
License
This project is licensed under the MIT License.
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago