https://github.com/ankansaha/mongosuper
"MongoSuper is an npm package that enables secure connection to MongoDB. It provides automatic reconnection functionality, ensuring seamless connectivity even after disconnection."
https://github.com/ankansaha/mongosuper
github-codespaces gitkraken gitlens jetbrains learn mongodb student-vscode
Last synced: 8 months ago
JSON representation
"MongoSuper is an npm package that enables secure connection to MongoDB. It provides automatic reconnection functionality, ensuring seamless connectivity even after disconnection."
- Host: GitHub
- URL: https://github.com/ankansaha/mongosuper
- Owner: AnkanSaha
- License: mit
- Created: 2023-06-06T14:48:05.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-13T09:34:32.000Z (12 months ago)
- Last Synced: 2025-04-13T10:33:06.962Z (12 months ago)
- Topics: github-codespaces, gitkraken, gitlens, jetbrains, learn, mongodb, student-vscode
- Language: TypeScript
- Homepage: https://npmjs.com/package/mongosuper
- Size: 243 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Security: SECURITY.md
Awesome Lists containing this project
README
# MongoSuper
[](https://badge.fury.io/js/mongosuper)
[](https://github.com/AnkanSaha/MongoSuper/actions/workflows/github-code-scanning/codeql)
- MongoSuper is an superset of mongoose which makes it easier to connect to MongoDB and disconnect from it. also it has CRUD methods which makes it easier to create, read, update and delete data from MongoDB.
## Installation
```bash
npm install mongosuper@latest
```
## Usage
```javascript
const MongoSuper = require("mongosuper"); // Importing the package
const Connector = new MongoSuper.Mongo({
MongoURL: "mongodb://localhost:27017/",
Database_Name: "test",
NeverDisconnect: "Provide true/false",
Schema: "Provide the Schema Object",
CollectionName: "Provide the Collection Name",
isTimeStamps: true // set true if you want to create timestamp in record
}); // Creating an instance of the Mongo class
```
- Connector is an instance of the Mongo class
## Connection Methods
```javascript
Connector.Connect(); // Connects to MongoDB once and you can disconnect using Connector.disconnect()
Connector.disconnect(); // Disconnects from MongoDB if connected whenever you want (Only works when you pass NeverDisconnect as false in the constructor)
```
## Find Methods
if you want to use CRUD methods, you have to provide the Schema in the constructor
```javascript
Connector.find().then((data) => {
console.log(data)
}) // Finds all the data in the database
Connector.find('AND', [{name: 'John'}]).then((data) => {
console.log(data)
output: [{name: 'John'}, {name: 'John'}, {name: 'John'}]
}) // Set the array of objects to find the data in the database with Specific Filter with AND operator
Connector.find('OR', [{name: 'John'}]).then((data) => {
console.log(data)
output: [{name: 'John'}, {name: 'John'}, {name: 'John'}]
}) // Set the array of objects to find the data in the database with Specific Filter with OR operator
Connector.find('AND',[{name: 'John'}], 1).then((data) => {
console.log(data)
output: [{name: 'John'}]
}) // Set the array of objects to find the data in the database with Specific Filter and Limit with AND operator
Connector.find('OR',[{name: 'John'}], 1).then((data) => {
console.log(data)
output: [{name: 'John'}]
}) // Set the array of objects to find the data in the database with Specific Filter and Limit with OR operator
Connector.findAndCount('AND', [{name: 'John'}]).then((data) => {
console.log(data)
output: {
count: 3,
data: [{name: 'John'}, {name: 'John'}, {name: 'John'}]
}
}) // Set the array of objects to find the data in the database with Specific Filter and Count with AND operator
Connector.findAndCount('OR', [{name: 'John'}]).then((data) => {
console.log(data)
output: {
count: 3,
data: [{name: 'John'}, {name: 'John'}, {name: 'John'}]
}
}) // Set the array of objects to find the data in the database with Specific Filter and Count with OR operator
Connector.findAndCount('AND', [{name: 'John'}], 1).then((data) => {
console.log(data)
output: {
count: 1,
data: [{name: 'John'}]
}
}) // Set the array of objects to find the data in the database with Specific Filter, Count and Limit with AND operator
Connector.findAndCount('OR', [{name: 'John'}], 1).then((data) => {
console.log(data)
output: {
count: 1,
data: [{name: 'John'}]
}
}) // Set the array of objects to find the data in the database with Specific Filter, Count and Limit with OR operator
```
## You can also use the following filters for aggregation as per as Mongoose
```javascript
Connector.find([{ age: { $lt: 20 } }, { age: { $gt: 12 } }]).then((data) => {
console.log(data);
output: [
{ age: 13 },
{ age: 14 },
{ age: 15 },
{ age: 16 },
{ age: 17 },
{ age: 18 },
{ age: 19 }
];
}); // Set the array of objects to find the data in the database with less than and greater than filter
Connector.find([{ age: { $lte: 20 } }, { age: { $gte: 12 } }]).then((data) => {
console.log(data);
output: [
{ age: 12 },
{ age: 13 },
{ age: 14 },
{ age: 15 },
{ age: 16 },
{ age: 17 },
{ age: 18 },
{ age: 19 },
{ age: 20 }
];
}); // Set the array of objects to find the data in the database with less than or equal to and greater than or equal to filter
Connector.find([{ age: { $lt: 20 } }, { age: { $gt: 12 } }], 1).then((data) => {
console.log(data);
output: [{ age: 13 }];
}); // Set the array of objects to find the data in the database with less than and greater than filter and Limit
Connector.findAndCount([{ age: { $lt: 20 } }, { age: { $gt: 12 } }], 1, 1).then(
(data) => {
console.log(data);
output: {
Skipped: 1,
Limit: 1,
count: 1,
Data: [{ age: 13 }];
}
}
); // Set the array of objects to find the data in the database with less than and greater than filter, Limit with skip
```
# Insert Methods
```javascript
Connector.create({name: 'John'}).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Created Data",
NewCount: 1,
NewData: [{name: 'John'}]
}
}) // Creates a new document in the database
Connector.create({name:"Ankan Saha", Address:{ Street:"Address"}}).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Created Data",
NewCount: 1,
NewData: [{name: 'Ankan Saha', Address:{ Street:"Address"}}]
}
}) // Creates a new document in the database with nested objects
**Note: Make sure you provide the Right Schema in the constructor if you want to use the create method**
```
# Update Methods
```javascript
Connector.update([{name: 'John'}], {name: 'John Doe'}, false).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Updated Data",
UpdatedCount: 1,
UpdatedData: [{name: 'John Doe'}]
}
}) // Updates the data in the database with multi option set to false
Connector.update([{name:"John"}], {$set:{"User.name":"John", "User.Surname":"Doe"}}, false).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Updated Data",
UpdatedCount: 1,
UpdatedData: [{name: 'John', Surname: 'Doe'}]
}
}) // Updates the data in the database with nested objects
Connector.update([{name: 'John'}], {name: 'John Doe'}, true).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Updated Data",
UpdatedCount: 5,
UpdatedData: [{name: 'John Doe'}, {name: 'John Doe'}, {name: 'John Doe'}, {name: 'John Doe'}, {name: 'John Doe'}]
}
}) // Updates the data in the database with multi option set to true
```
# Delete Methods
```javascript
Connector.delete([{name: 'John'}], false).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Deleted Data",
DeletedCount: 1,
DeletedData: [{name: 'John'}]
}
}) // Deletes the data in the database with multi option set to false
Connector.delete([{name: 'John'}], true).then((data) => {
console.log(data)
output: {
status:true,
message: "Successfully Deleted Data",
DeletedCount: 5,
NewData: [{name:"Ankan"}]
}
}) // Deletes the data in the database with multi option set to true
```
# Important:
- If you Don't provide the URL, it will try to connect to the default URL: mongodb://localhost:27017 with log set to true.