mongoose-init 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/bin/index.js +21 -0
- package/package.json +15 -0
- package/templates/db.ts +20 -0
package/bin/index.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const path = require("path");
|
|
5
|
+
|
|
6
|
+
const targetDir = path.join(process.cwd(), "lib");
|
|
7
|
+
const targetFile = path.join(targetDir, "db.ts");
|
|
8
|
+
const templateFile = path.join(__dirname, "../templates/db.ts");
|
|
9
|
+
|
|
10
|
+
if (!fs.existsSync(targetDir)) {
|
|
11
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (fs.existsSync(targetFile)) {
|
|
15
|
+
console.log("lib/db.ts already exists, skipping.");
|
|
16
|
+
process.exit(0);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fs.copyFileSync(templateFile, targetFile);
|
|
20
|
+
console.log("Created lib/db.ts — Mongoose boilerplate ready!");
|
|
21
|
+
console.log("Add MONGODB_URI to your .env file");
|
package/package.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mongoose-init",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Instantly generate Mongoose boilerplate for Next.js",
|
|
5
|
+
"bin": {
|
|
6
|
+
"mongoose-init": "./bin/index.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"bin",
|
|
10
|
+
"templates"
|
|
11
|
+
],
|
|
12
|
+
"keywords": ["mongoose", "nextjs", "boilerplate"],
|
|
13
|
+
"author": "Sibasish2005",
|
|
14
|
+
"preferGlobal": false
|
|
15
|
+
}
|
package/templates/db.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
const MONGODB_URI = process.env.MONGODB_URI as string;
|
|
4
|
+
|
|
5
|
+
if (!MONGODB_URI) {
|
|
6
|
+
throw new Error("Please define MONGODB_URI in your .env file");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
let cached = (global as any).mongoose || { conn: null, promise: null };
|
|
10
|
+
|
|
11
|
+
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);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
cached.conn = await cached.promise;
|
|
19
|
+
return cached.conn;
|
|
20
|
+
}
|