mongoose-init 1.0.0 → 1.0.1

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/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # mongoose-init
2
+
3
+ Instantly generate a Mongoose connection boilerplate in any JavaScript/TypeScript project with a single command.
4
+
5
+ ## Usage
6
+ ```bash
7
+ npx mongoose-init
8
+ ```
9
+
10
+ This will create a `lib/db.ts` file in your project with a ready-to-use Mongoose connection.
11
+
12
+ ## Generated file
13
+ ```ts
14
+ import mongoose from "mongoose";
15
+
16
+ const MONGODB_URI = process.env.MONGODB_URI || "";
17
+
18
+ if (!MONGODB_URI) {
19
+ throw new Error("Please define MONGODB_URI in your .env file");
20
+ }
21
+
22
+ export async function connectDB() {
23
+ try {
24
+ if (mongoose.connection.readyState >= 1) return;
25
+ await mongoose.connect(MONGODB_URI);
26
+ console.log("MongoDB connected successfully");
27
+ } catch (error) {
28
+ console.error("MongoDB connection failed:", error);
29
+ process.exit(1);
30
+ }
31
+ }
32
+ ```
33
+
34
+ ## Setup
35
+
36
+ After running the command, add your MongoDB URI to your `.env` file:
37
+ ```env
38
+ MONGODB_URI=your_mongodb_connection_string
39
+ ```
40
+
41
+ Then import and use it anywhere in your project:
42
+ ```ts
43
+ import { connectDB } from "./lib/db";
44
+
45
+ await connectDB();
46
+ ```
47
+
48
+ ## Works with
49
+
50
+ - Next.js
51
+ - Express
52
+ - Plain Node.js
53
+ - Any JavaScript/TypeScript project
54
+
55
+ ## Author
56
+
57
+ Sibasish2005
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mongoose-init",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Instantly generate Mongoose boilerplate for Next.js",
5
5
  "bin": {
6
6
  "mongoose-init": "./bin/index.js"
@@ -9,7 +9,11 @@
9
9
  "bin",
10
10
  "templates"
11
11
  ],
12
- "keywords": ["mongoose", "nextjs", "boilerplate"],
12
+ "keywords": [
13
+ "mongoose",
14
+ "nextjs",
15
+ "boilerplate"
16
+ ],
13
17
  "author": "Sibasish2005",
14
18
  "preferGlobal": false
15
- }
19
+ }
package/templates/db.ts CHANGED
@@ -1,20 +1,18 @@
1
1
  import mongoose from "mongoose";
2
2
 
3
- const MONGODB_URI = process.env.MONGODB_URI as string;
3
+ const MONGODB_URI = process.env.MONGODB_URI || "";
4
4
 
5
5
  if (!MONGODB_URI) {
6
6
  throw new Error("Please define MONGODB_URI in your .env file");
7
7
  }
8
8
 
9
- let cached = (global as any).mongoose || { conn: null, promise: null };
10
-
11
9
  export async function connectDB() {
12
- if (cached.conn) return cached.conn;
13
-
14
- if (!cached.promise) {
15
- cached.promise = mongoose.connect(MONGODB_URI).then((m) => m);
10
+ try {
11
+ if (mongoose.connection.readyState >= 1) return;
12
+ await mongoose.connect(MONGODB_URI);
13
+ console.log("MongoDB connected successfully");
14
+ } catch (error) {
15
+ console.error("MongoDB connection failed:", error);
16
+ process.exit(1);
16
17
  }
17
-
18
- cached.conn = await cached.promise;
19
- return cached.conn;
20
18
  }