@shamil-tp/connectdb 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +20 -0
- package/README.md +19 -3
- package/package.json +32 -3
- package/src/index.js +21 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shamil TP
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a
|
|
8
|
+
copy of this software and associated documentation files (the “Software”), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights to use,
|
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
|
|
11
|
+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included
|
|
14
|
+
in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
|
|
17
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
19
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
|
|
20
|
+
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @shamil-tp/connectdb
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@shamil-tp/connectdb)
|
|
4
|
+
[](https://www.npmjs.com/package/@shamil-tp/connectdb)
|
|
5
|
+
[](https://github.com/shamil-tp/connectDB/blob/main/LICENSE)
|
|
6
|
+
|
|
7
|
+
A lightweight MongoDB connection utility for Node.js using Mongoose.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Zero-configuration MongoDB setup for Mongoose.
|
|
12
|
+
- Fallback to `process.env.MONGO_URI` or local `mongodb://127.0.0.1:27017/my_database`.
|
|
13
|
+
- Supports custom connection URI parameters.
|
|
14
|
+
- Clean error handling for test runners and CLI verification.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @shamil-tp/connectdb
|
package/package.json
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shamil-tp/connectdb",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A simple function that sets up MongoDB connection for Mongoose",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"type": "commonjs",
|
|
7
|
+
"files": [
|
|
8
|
+
"src/"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"test": "node --test test/connect.test.js"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"js",
|
|
15
|
+
"connectdb",
|
|
16
|
+
"mongoose",
|
|
17
|
+
"mongodb"
|
|
18
|
+
],
|
|
19
|
+
"author": "shamil-tp",
|
|
20
|
+
"license": "MIT",
|
|
21
|
+
"homepage": "https://github.com/shamil-tp/connectDB#readme",
|
|
22
|
+
"bugs": {
|
|
23
|
+
"url": "https://github.com/shamil-tp/connectDB/issues"
|
|
24
|
+
},
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/shamil-tp/connectDB.git"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"mongoose": "^9.11.4"
|
|
34
|
+
}
|
|
6
35
|
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const mongoose = require('mongoose');
|
|
2
|
+
|
|
3
|
+
const connectDB = async (customUri) => {
|
|
4
|
+
try {
|
|
5
|
+
const uri = customUri || process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/my_database';
|
|
6
|
+
const conn = await mongoose.connect(uri);
|
|
7
|
+
console.log(`MongoDB Connected: ${conn.connection.host}`);
|
|
8
|
+
return conn;
|
|
9
|
+
} catch (error) {
|
|
10
|
+
console.error(`Database Connection Error: ${error.message}`);
|
|
11
|
+
|
|
12
|
+
// If called directly via CLI/tests, throw error so node:test catches it
|
|
13
|
+
if (process.env.NODE_ENV === 'test') {
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
module.exports = connectDB;
|