mysqlsyncr 1.0.38 → 1.0.42

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": "mysqlsyncr",
3
- "version": "1.0.38",
3
+ "version": "1.0.42",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -45,62 +45,64 @@ class DBStructureChecker {
45
45
  }
46
46
 
47
47
  // Export the DBStructureChecker class
48
- export { DBStructureChecker };
48
+ export { DBStructureChecker, dumpAllDatabases, compareAllDatabases, startApiServer };
49
49
 
50
- // Start the server only if the 'ui' command is used
51
- (async () => {
52
- const { command, config, argv } = cli();
53
-
54
- if (command === 'ui') {
55
- const PORT = argv.uiPort
56
- startApiServer(PORT); // Start the Socket.IO server
57
- console.log('Server started on port', PORT);
58
- console.log('Open http://localhost:' + PORT + ' in your browser to use the UI');
59
- if(argv.openUI){
60
- await open(`http://localhost:${PORT}`);
61
- }
62
- } else {
63
- // Handle other commands (dump, compare, update) as before
64
- const checker = new DBStructureChecker(config, argv.database);
65
- checker.connection.on('connect', async () => {
66
- console.log('Connected to the database');
67
- checker.targetDatabase = argv.database;
68
- checker.command = command;
69
- checker.options = argv;
70
- try {
71
- if (command === 'dump') {
72
- console.log('Dumping the database structure to files');
73
- await dumpAllDatabases(checker.connection, command, argv);
74
- } else if (command === 'compare') {
75
- console.log('Comparing the database structure with JSON dumps (dry run)');
76
- let diffs = await compareAllDatabases(checker.connection, argv.output, false, command, argv); // Dry run
77
- let totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
78
- console.log('Total differences count:', totalDiffs);
79
- fs.writeFileSync(path.join(argv.output, 'diffs.json'), JSON.stringify(diffs, null, 4));
80
- } else if (command === 'update') {
81
- console.log('Updating the database structure based on JSON dumps');
82
- let totalDiffs = Infinity; // Initialize with a large number
83
- let previousDiffs = totalDiffs; // Store previous differences
84
- let runCount = 0; // Counter for the number of runs
85
- let maxRuns = 6;
86
- while (runCount < maxRuns && totalDiffs > 0 && (totalDiffs < previousDiffs || previousDiffs === Infinity)) {
87
- console.log('Running update command. Run count:', runCount);
88
- let diffs = await compareAllDatabases(checker.connection, argv.output, true, command, argv); // Apply updates
89
- previousDiffs = totalDiffs; // Update previous differences
90
- totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
50
+ // Only run this if this is the main module
51
+ if (import.meta.url === `file://${process.argv[1]}`) {
52
+ (async () => {
53
+ const { command, config, argv } = cli();
54
+ console.log("MySQLsyncr started via command line")
55
+ if (command === 'ui') {
56
+ const PORT = argv.uiPort
57
+ startApiServer(PORT, argv); // Start the Socket.IO server
58
+ console.log('Server started on port', PORT);
59
+ console.log('Open http://localhost:' + PORT + ' in your browser to use the UI');
60
+ if(argv.openUI && argv.openUI === true || argv.openUI === 'true'){
61
+ await open(`http://localhost:${PORT}`);
62
+ }
63
+ } else {
64
+ // Handle other commands (dump, compare, update) as before
65
+ const checker = new DBStructureChecker(config, argv.database);
66
+ checker.connection.on('connect', async () => {
67
+ console.log('Connected to the database');
68
+ checker.targetDatabase = argv.database;
69
+ checker.command = command;
70
+ checker.options = argv;
71
+ try {
72
+ if (command === 'dump') {
73
+ console.log('Dumping the database structure to files');
74
+ await dumpAllDatabases(checker.connection, command, argv);
75
+ } else if (command === 'compare') {
76
+ console.log('Comparing the database structure with JSON dumps (dry run)');
77
+ let diffs = await compareAllDatabases(checker.connection, argv.output, false, command, argv); // Dry run
78
+ let totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
91
79
  console.log('Total differences count:', totalDiffs);
92
- runCount++;
93
- if(totalDiffs > 0 && runCount < maxRuns){
94
- console.log('Total differences count is still greater than 0. Waiting 1 second before next run. Run count:', runCount);
95
- await new Promise(resolve => setTimeout(resolve, 1000));
80
+ fs.writeFileSync(path.join(argv.output, 'diffs.json'), JSON.stringify(diffs, null, 4));
81
+ } else if (command === 'update') {
82
+ console.log('Updating the database structure based on JSON dumps');
83
+ let totalDiffs = Infinity; // Initialize with a large number
84
+ let previousDiffs = totalDiffs; // Store previous differences
85
+ let runCount = 0; // Counter for the number of runs
86
+ let maxRuns = 6;
87
+ while (runCount < maxRuns && totalDiffs > 0 && (totalDiffs < previousDiffs || previousDiffs === Infinity)) {
88
+ console.log('Running update command. Run count:', runCount);
89
+ let diffs = await compareAllDatabases(checker.connection, argv.output, true, command, argv); // Apply updates
90
+ previousDiffs = totalDiffs; // Update previous differences
91
+ totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
92
+ console.log('Total differences count:', totalDiffs);
93
+ runCount++;
94
+ if(totalDiffs > 0 && runCount < maxRuns){
95
+ console.log('Total differences count is still greater than 0. Waiting 1 second before next run. Run count:', runCount);
96
+ await new Promise(resolve => setTimeout(resolve, 1000));
97
+ }
96
98
  }
97
99
  }
100
+ } catch (err) {
101
+ console.error(err.message);
102
+ } finally {
103
+ await checker.closeConnection();
98
104
  }
99
- } catch (err) {
100
- console.error(err.message);
101
- } finally {
102
- await checker.closeConnection();
103
- }
104
- });
105
- }
106
- })();
105
+ });
106
+ }
107
+ })();
108
+ }
@@ -10,26 +10,47 @@ import { dirname, join } from 'path';
10
10
  import { readdir, mkdir } from 'fs/promises';
11
11
  import { resolve } from 'path';
12
12
 
13
- export const startApiServer = (port) => {
13
+ const setConfig = (config, argv) => {
14
+ if(!config.host && argv.host){
15
+ config.host = argv.host;
16
+ }
17
+ if(!config.port && argv.port){
18
+ config.port = argv.port;
19
+ }
20
+ if(!config.user && argv.user){
21
+ config.user = argv.user;
22
+ }
23
+ if(!config.password && argv.password){
24
+ config.password = argv.password;
25
+ }
26
+ if(!config.output && argv.output !=='../db-dump'){
27
+ config.output = argv.output;
28
+ }
29
+ return config;
30
+ }
31
+
32
+ export const startApiServer = (port, argv) => {
14
33
  const app = express();
15
34
  app.use(cors({ origin: '*' }));
16
35
  const server = http.createServer(app);
17
- app.use(express.json({ limit: '50mb' })); // Increased payload limit
18
- app.use(express.urlencoded({ limit: '50mb', extended: true })); // Also handle URL-encoded data
36
+ app.use(express.json({ limit: '50mb' }));
37
+ app.use(express.urlencoded({ limit: '50mb', extended: true }));
19
38
 
20
39
  const __filename = fileURLToPath(import.meta.url);
21
40
  const __dirname = dirname(__filename);
22
41
  app.use(express.static(join(__dirname, '../ui/dist/spa')));
23
42
 
24
43
  app.post('/api/getDatabasesFromExistingDumps', async (req, res) => {
25
- const { config } = req.body;
44
+ let { config } = req.body;
45
+ config = setConfig(config, argv);
26
46
  const databases = await getDatabasesFromExistingDumps(config);
27
47
  res.json(databases);
28
48
  });
29
49
 
30
50
  // API endpoint to dump the database
31
51
  app.post('/api/dump', async (req, res) => {
32
- const { config, database } = req.body;
52
+ let { config, database } = req.body;
53
+ config = setConfig(config, argv);
33
54
  const checker = new DBStructureChecker(config, database);
34
55
 
35
56
  checker.connection.on('error', (err) => {
@@ -56,8 +77,9 @@ export const startApiServer = (port) => {
56
77
 
57
78
  // API endpoint to compare the database
58
79
  app.post('/api/compare', async (req, res) => {
59
- const { config } = req.body;
60
- const checker = new DBStructureChecker(config, config.database);
80
+ let { config, database } = req.body;
81
+ config = setConfig(config, argv);
82
+ const checker = new DBStructureChecker(config, database);
61
83
 
62
84
  checker.connection.on('error', (err) => {
63
85
  console.error('Database connection error in route compare:', err);
@@ -87,7 +109,8 @@ export const startApiServer = (port) => {
87
109
 
88
110
  // API endpoint to apply differences
89
111
  app.post('/api/apply', async (req, res) => {
90
- const { config, database, diffs } = req.body;
112
+ let { config, database, diffs } = req.body;
113
+ config = setConfig(config, argv);
91
114
  console.log('Applying differences:', diffs);
92
115
  const checker = new DBStructureChecker(config, database);
93
116
 
package/view.png CHANGED
Binary file