create-butterfly-app-v2 2.1.1 → 2.1.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-butterfly-app-v2",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "Full-stack boilerplate: Node.js + React + Tailwind + MySQL. Choose from Hotel, Parking, or Student Management projects with auth + full CRUD.",
5
5
  "bin": {
6
6
  "create-butterfly-app-v2": "cli.js"
@@ -1,13 +1,25 @@
1
1
  const mysql = require("mysql2/promise");
2
2
 
3
- const pool = mysql.createPool({
3
+ const DB_NAME = process.env.DB_NAME || "__PROJECT_NAME__";
4
+ const config = {
4
5
  host: process.env.DB_HOST || "localhost",
5
6
  user: process.env.DB_USER || "root",
6
7
  password: process.env.DB_PASSWORD || "",
7
- database: process.env.DB_NAME || "__PROJECT_NAME__",
8
- port: process.env.DB_PORT || 3306,
8
+ port: parseInt(process.env.DB_PORT) || 3306,
9
+ };
10
+
11
+ async function ensureDatabase() {
12
+ const conn = await mysql.createConnection(config);
13
+ await conn.query(`CREATE DATABASE IF NOT EXISTS \`${DB_NAME}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`);
14
+ await conn.end();
15
+ }
16
+
17
+ const pool = mysql.createPool({
18
+ ...config,
19
+ database: DB_NAME,
9
20
  waitForConnections: true,
10
21
  connectionLimit: 10,
11
22
  });
12
23
 
13
24
  module.exports = pool;
25
+ module.exports.ensureDatabase = ensureDatabase;
@@ -18,12 +18,20 @@ app.use("/api", require("./routes/api"));
18
18
 
19
19
  async function start() {
20
20
  try {
21
+ console.log(" Connecting to MySQL...");
22
+ await db.ensureDatabase();
21
23
  await db.query("SELECT 1");
22
- console.log("MySQL connected");
24
+ console.log("MySQL connected");
23
25
  await require("./db/init")();
24
- app.listen(PORT, () => console.log(`Backend on http://localhost:${PORT}`));
26
+ app.listen(PORT, () => console.log(`Backend running on http://localhost:${PORT}\n`));
25
27
  } catch (err) {
26
- console.error("Startup failed:", err.message);
28
+ console.error("\n ✗ Startup failed:");
29
+ console.error(" " + (err.code || "Error") + ": " + (err.sqlMessage || err.message));
30
+ console.error("\n Possible fixes:");
31
+ console.error(" 1. Start XAMPP (click Start next to Apache & MySQL)");
32
+ console.error(" 2. If MySQL runs on a different port, update .env (DB_PORT)");
33
+ console.error(" 3. If you changed MySQL password, update .env (DB_PASSWORD)");
34
+ console.error("\n Press Ctrl+C to stop, fix the issue, then run 'npm run dev' again\n");
27
35
  process.exit(1);
28
36
  }
29
37
  }