@seip/blue-bird 0.3.4 → 0.3.6

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/core/cli/init.js CHANGED
@@ -114,7 +114,7 @@ if (command === "react") import("./react.js");
114
114
  else if (command === "route") import("./route.js")
115
115
  else if (command === "component") import("./component.js")
116
116
  else if (command === "swagger-install") import("./swagger.js")
117
- else if (command === "scaffolding-auth") import("./scaffolding-auth.js")
117
+ else if (command === "scaffolding-auth") import("./scaffolding-auth.js").then(m => m.default && m.default.run ? m.default.run() : null)
118
118
  else initializer.run();
119
119
 
120
120
 
@@ -29,7 +29,7 @@ class ScaffoldingAuth {
29
29
  console.log(chalk.white("Update your App.jsx to use the newly created React components."));
30
30
  console.log(chalk.yellow("Running setup script to execute database migrations..."));
31
31
  try {
32
- execSync('node backend/databases/setup_tables.js', { stdio: "inherit", cwd: this.appDir });
32
+ execSync('node --env-file=.env backend/databases/setup_tables.js', { stdio: "inherit", cwd: this.appDir });
33
33
  console.log(chalk.green("✓ Database migrations success. Users and LoginHistory tables created."));
34
34
  } catch (e) {
35
35
  console.log(chalk.red("Failed to execute setup_tables.js. You may need to run it manually."));
@@ -225,7 +225,61 @@ class DatabaseConnection {
225
225
  }
226
226
 
227
227
  generateSetupTables(dbDir, dialect) {
228
- let content = `import db from './connection.js';\n\nasync function setup() {\n`;
228
+ let content = `import db from './connection.js';\n\n`;
229
+
230
+ if (dialect === "mysql" || dialect === "postgres") {
231
+ content += `const dbUrl = process.env.DATABASE_URL;\n\nasync function createDatabaseIfNotExists() {\n`;
232
+ if (dialect === "mysql") {
233
+ content += ` try {
234
+ const mysql = await import('mysql2/promise');
235
+ const createConnection = mysql.createConnection || (mysql.default && mysql.default.createConnection);
236
+ const url = new URL(dbUrl);
237
+ const dbName = url.pathname.replace('/', '');
238
+ const connectionParams = {
239
+ host: url.hostname,
240
+ user: decodeURIComponent(url.username),
241
+ password: decodeURIComponent(url.password),
242
+ port: url.port ? Number(url.port) : 3306
243
+ };
244
+ const connection = await createConnection(connectionParams);
245
+ await connection.query(\`CREATE DATABASE IF NOT EXISTS \\\`\${dbName}\\\`;\`);
246
+ await connection.end();
247
+ console.log(\`Database '\${dbName}' checked/created successfully.\`);
248
+ } catch (err) {
249
+ console.error("Failed to create database automatically:", err.message);
250
+ }
251
+ `;
252
+ } else if (dialect === "postgres") {
253
+ content += ` try {
254
+ const pkg = await import('pg');
255
+ const Client = pkg.Client || (pkg.default && pkg.default.Client);
256
+ const url = new URL(dbUrl);
257
+ const dbName = url.pathname.replace('/', '');
258
+ const connectionString = \`postgres://\${url.username}:\${url.password}@\${url.hostname}:\${url.port ? url.port : 5432}/postgres\`;
259
+ const client = new Client({ connectionString });
260
+ await client.connect();
261
+
262
+ const res = await client.query('SELECT 1 FROM pg_database WHERE datname = $1', [dbName]);
263
+ if (res.rowCount === 0) {
264
+ await client.query(\`CREATE DATABASE "\${dbName}"\`);
265
+ console.log(\`Database '\${dbName}' created successfully.\`);
266
+ } else {
267
+ console.log(\`Database '\${dbName}' already exists.\`);
268
+ }
269
+ await client.end();
270
+ } catch (err) {
271
+ console.error("Failed to create database automatically:", err.message);
272
+ }
273
+ `;
274
+ }
275
+ content += `}\n\n`;
276
+ }
277
+
278
+ content += `async function setup() {\n`;
279
+
280
+ if (dialect === "mysql" || dialect === "postgres") {
281
+ content += ` await createDatabaseIfNotExists();\n`;
282
+ }
229
283
 
230
284
  let usersTable = "";
231
285
  let historyTable = "";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seip/blue-bird",
3
- "version": "0.3.4",
3
+ "version": "0.3.6",
4
4
  "description": "Express + React opinionated framework with SPA or API architecture and built-in JWT auth",
5
5
  "type": "module",
6
6
  "bin": {