create-web3cart-store 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.
Files changed (2) hide show
  1. package/lib/database.js +52 -35
  2. package/package.json +1 -1
package/lib/database.js CHANGED
@@ -12,54 +12,71 @@ const path = require('path');
12
12
  * Test database connection
13
13
  */
14
14
  async function testConnection(config) {
15
- try {
16
- const connection = await mysql.createConnection({
17
- host: config.host,
18
- user: config.user,
19
- password: config.password,
20
- connectTimeout: 10000
21
- });
15
+ const hostsToTry = config.host.toLowerCase() === 'localhost' ? ['127.0.0.1', 'localhost'] : [config.host];
16
+ let lastError = null;
22
17
 
23
- // Try to select the database
18
+ for (const host of hostsToTry) {
24
19
  try {
25
- await connection.query(`USE \`${config.name}\``);
26
- } catch (e) {
27
- // Database doesn't exist, that's OK - we'll create it
28
- }
20
+ const connection = await mysql.createConnection({
21
+ host: host,
22
+ user: config.user,
23
+ password: config.password,
24
+ connectTimeout: 5000 // Shorter timeout for faster fallback
25
+ });
29
26
 
30
- await connection.end();
31
- return { success: true };
32
- } catch (error) {
33
- return {
34
- success: false,
35
- error: `Connection failed: ${error.message}`
36
- };
27
+ // Try to select the database
28
+ try {
29
+ await connection.query(`USE \`${config.name}\``);
30
+ } catch (e) {
31
+ // Database doesn't exist, that's OK - we'll create it
32
+ }
33
+
34
+ await connection.end();
35
+ return { success: true, host: host };
36
+ } catch (error) {
37
+ lastError = error;
38
+ // Only continue to next host if it was a connection refusal
39
+ if (error.code !== 'ECONNREFUSED') break;
40
+ }
37
41
  }
42
+
43
+ return {
44
+ success: false,
45
+ error: `Connection failed: ${lastError.message} (${lastError.code})`
46
+ };
38
47
  }
39
48
 
40
49
  /**
41
50
  * Create database if it doesn't exist
42
51
  */
43
52
  async function createDatabase(config) {
44
- try {
45
- const connection = await mysql.createConnection({
46
- host: config.host,
47
- user: config.user,
48
- password: config.password
49
- });
53
+ const hostsToTry = config.host.toLowerCase() === 'localhost' ? ['127.0.0.1', 'localhost'] : [config.host];
54
+ let lastError = null;
50
55
 
51
- await connection.query(
52
- `CREATE DATABASE IF NOT EXISTS \`${config.name}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`
53
- );
56
+ for (const host of hostsToTry) {
57
+ try {
58
+ const connection = await mysql.createConnection({
59
+ host: host,
60
+ user: config.user,
61
+ password: config.password
62
+ });
54
63
 
55
- await connection.end();
56
- return { success: true };
57
- } catch (error) {
58
- return {
59
- success: false,
60
- error: `Database creation failed: ${error.message}`
61
- };
64
+ await connection.query(
65
+ `CREATE DATABASE IF NOT EXISTS \`${config.name}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`
66
+ );
67
+
68
+ await connection.end();
69
+ return { success: true };
70
+ } catch (error) {
71
+ lastError = error;
72
+ if (error.code !== 'ECONNREFUSED') break;
73
+ }
62
74
  }
75
+
76
+ return {
77
+ success: false,
78
+ error: `Database creation failed: ${lastError.message}`
79
+ };
63
80
  }
64
81
 
65
82
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-web3cart-store",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Create a Web3Cart crypto payment store with one command",
5
5
  "author": "Web3Cart <hello@web3cart.site>",
6
6
  "license": "MIT",