@ajayos/nodemongoose
v1.0.0
Published
Nodemongoose is a simple key-value store for Node.js, built on top of mongoose.
Downloads
5
Readme
NodeMongoose
NodeMongoose is a Node.js library that simplifies MongoDB database interactions using the Mongoose ODM (Object Data Modeling). It provides an intuitive class-based interface to perform common MongoDB operations with ease.
Installation
You can install NodeMongoose using npm:
npm install @ajayos/nodemongoose
Usage
- Import the NodeMongoose class and instantiate it with your MongoDB URL:
const NodeMongoose = require('@ajayos/nodemongoose');
const url = 'your-mongodb-url'; // Replace with your MongoDB URL
const db = new NodeMongoose(url);
- Utilize the available class methods to interact with the database, as demonstrated in the examples below.
Project Overview
NodeMongoose streamlines the process of connecting to MongoDB and performing operations on your data. Whether you're building a small application or a complex system, NodeMongoose provides a consistent and convenient API for managing your data in a MongoDB database.
Functions and Usage
setDB(tableName, rowName, data)
Inserts or updates a row in the specified table with the specified key and data.
Example Usage:
await db.deleteDB('users', 'ajay');
Example Usage (Delete Table):
await db.deleteDB('users');
setDATA(tableName, rowName, data: any)
Similar to setDB
, but stores data without JSON stringifying it.
Example Usage:
await db.setDATA('metadata', 'version', 1.2);
getDATA(tableName, rowName)
Similar to getDB
, but retrieves data without JSON parsing it.
Example Usage:
const version = await db.getDATA('metadata', 'version');
console.log(version); // 1.2
Example Usage (All Rows):
const allVersions = await db.getDATA('metadata');
console.log(allVersions); // [ { rowName: 'version', data: 1.2 }, ... ]
deleteDATA(tableName, rowName)
Similar to deleteDB
, but deletes data without JSON parsing it.
Example Usage:
await db.deleteDATA('metadata', 'version');
Example Usage (Delete Table):
await db.deleteDATA('metadata');
Example
Here's a complete example of using NodeMongoose to interact with a MongoDB database:
const NodeMongoose = require('@ajayos/nodemongoose');
async function main() {
const url = 'your-mongodb-url'; // Replace with your MongoDB URL
const db = new NodeMongoose(url);
// Set data using setDB
await db.setDB('users', 'ajay', { name: 'Ajay o s', age: 20 });
// Get data using getDB
const ajay = await db.getDB('users', 'ajay');
console.log('Retrieved Data:', ajay);
// Delete a specific row using deleteDB
await db.deleteDB('users', 'ajay');
await db.disconnect();
}
// Run the main function
main().catch((error) => {
console.error('An error occurred:', error);
});
Note
For functions like deleteDB
and deleteDATA
, if you provide only the tableName
, the entire table will be deleted. To delete a specific row, provide both tableName
and rowName
.
License
NodeMongoose is licensed under the Apache License 2.0. See the LICENSE file for details.
Repository
You can find the repository for NodeMongoose on GitHub at https://github.com/Ajayos/nodemongoose