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.
- package/lib/database.js +52 -35
- 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
|
-
|
|
16
|
-
|
|
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
|
-
|
|
18
|
+
for (const host of hostsToTry) {
|
|
24
19
|
try {
|
|
25
|
-
await
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
45
|
-
|
|
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
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
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
|
/**
|