1.0.38 • Published 4 months ago

abimongo v1.0.38

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

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.

1.0.38

4 months ago

1.0.33

4 months ago

1.0.32

4 months ago

1.0.31

4 months ago

1.0.37

4 months ago

1.0.36

4 months ago

1.0.35

4 months ago

1.0.34

4 months ago

1.0.19

5 months ago

1.0.18

5 months ago

1.0.17

5 months ago

1.0.22

5 months ago

1.0.21

5 months ago

1.0.20

5 months ago

1.0.26

5 months ago

1.0.25

5 months ago

1.0.24

5 months ago

1.0.23

5 months ago

1.0.29

4 months ago

1.0.28

4 months ago

1.0.27

4 months ago

1.0.30

4 months ago

1.0.16

5 months ago

1.0.15

5 months ago

1.0.14

5 months ago

1.0.13

5 months ago

1.0.12

5 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.0

5 months ago