mysqlsyncr 1.1.8 → 1.1.12

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.
@@ -1,230 +1,230 @@
1
- import express from 'express';
2
- import http from 'http';
3
- import { dumpAllDatabases, getDatabasesFromExistingDumps } from './dump.js'; // Import necessary functions
4
- import { compareAllDatabases } from './compare.js';
5
- import { DBStructureChecker } from './index.js'; // Import your DBStructureChecker class
6
- import { applyDifferences } from './apply.js';
7
- import cors from 'cors';
8
- import { join } from 'path';
9
- import { readdir, mkdir } from 'fs/promises';
10
- import { resolve } from 'path';
11
- import { fileURLToPath } from 'url';
12
- import { dirname } from 'path';
13
-
14
- const __filename = fileURLToPath(import.meta.url);
15
- const __dirname = dirname(__filename);
16
-
17
- const setConfig = (config, argv) => {
18
- if(!config.host && argv.host){
19
- config.host = argv.host;
20
- }
21
- if(!config.port && argv.port){
22
- config.port = argv.port;
23
- }
24
- if(!config.user && argv.user){
25
- config.user = argv.user;
26
- }
27
- if(!config.password && argv.password){
28
- config.password = argv.password;
29
- }
30
- if(!config.output && argv.output !=='../db-dump'){
31
- config.output = argv.output;
32
- }
33
- return config;
34
- }
35
-
36
- // Create a function to get the package root directory that works in both ESM and CJS
37
- const getPackageRoot = () => {
38
- if (typeof __dirname !== 'undefined') {
39
- // CommonJS
40
- return join(__dirname, '..');
41
- } else {
42
- // ESM
43
- const __filename = fileURLToPath(import.meta.url);
44
- const __dirname = dirname(__filename);
45
- return join(__dirname, '..');
46
- }
47
- };
48
-
49
- // Use the function to get the root path
50
- const packageRoot = getPackageRoot();
51
-
52
- export const startApiServer = (port, argv) => {
53
- const app = express();
54
- app.use(cors({ origin: '*' }));
55
- const server = http.createServer(app);
56
- app.use(express.json({ limit: '50mb' }));
57
- app.use(express.urlencoded({ limit: '50mb', extended: true }));
58
-
59
- console.log(`Serving UI from ${join(packageRoot, 'ui/dist/spa')}`);
60
- app.use(express.static(join(packageRoot, 'ui/dist/spa')));
61
-
62
- app.post('/api/getDatabasesFromExistingDumps', async (req, res) => {
63
- let { config } = req.body;
64
- config = setConfig(config, argv);
65
- const databases = await getDatabasesFromExistingDumps(config);
66
- res.json(databases);
67
- });
68
-
69
- // API endpoint to dump the database
70
- app.post('/api/dump', async (req, res) => {
71
- let { config, database } = req.body;
72
- config = setConfig(config, argv);
73
- const checker = new DBStructureChecker(config, database);
74
-
75
- checker.connection.on('error', (err) => {
76
- console.error('Database connection error in route dump:', err);
77
- res.status(500).json({ message: 'Database connection failed', error: err.message });
78
- });
79
-
80
- checker.connection.on('connect', async () => {
81
- console.log('Connected to the database');
82
- checker.targetDatabase = database;
83
- checker.command = 'dump';
84
- checker.options = req.body;
85
- try {
86
- console.log('Dumping the database structure to files');
87
- await dumpAllDatabases(checker.connection, 'dump', config);
88
- res.json({ message: 'Dump completed successfully' });
89
- } catch (err) {
90
- res.status(500).json({ message: err.message });
91
- } finally {
92
- await checker.closeConnection();
93
- }
94
- });
95
- });
96
-
97
- // API endpoint to compare the database
98
- app.post('/api/compare', async (req, res) => {
99
- let { config, database } = req.body;
100
- config = setConfig(config, argv);
101
- const checker = new DBStructureChecker(config, database);
102
-
103
- checker.connection.on('error', (err) => {
104
- console.error('Database connection error in route compare:', err);
105
- res.status(500).json({ message: 'Database connection failed', error: err.message });
106
- });
107
-
108
- checker.connection.on('connect', async () => {
109
- console.log('Connected to the database');
110
- checker.targetDatabase = config.database;
111
- checker.command = 'compare';
112
- checker.options = req.body;
113
- try {
114
- delete req.body.output;
115
- console.log('Comparing the database structure with JSON dumps (dry run)');
116
- let diffs = await compareAllDatabases(checker.connection, config.output, false, 'compare', config);
117
- res.json(diffs);
118
- } catch (err) {
119
- console.log(err);
120
- console.log(req.body);
121
- res.status(500).json({ message: err.message });
122
- } finally {
123
- await checker.closeConnection();
124
- }
125
- })
126
-
127
- });
128
-
129
- // API endpoint to apply differences
130
- app.post('/api/apply', async (req, res) => {
131
- let { config, database, diffs } = req.body;
132
- config = setConfig(config, argv);
133
- console.log('Applying differences:', diffs);
134
- const checker = new DBStructureChecker(config, database);
135
-
136
- checker.connection.on('error', (err) => {
137
- console.error('Database connection error in route apply:', err);
138
- res.status(500).json({ message: 'Database connection failed', error: err.message });
139
- });
140
-
141
- checker.connection.on('connect', async () => {
142
- console.log('Connected to the database');
143
- checker.targetDatabase = database;
144
- checker.command = 'apply';
145
- checker.options = req.body;
146
- try {
147
- console.log('Applying the database structure based on JSON dumps');
148
- await applyDifferences(checker.connection, database, diffs);
149
- res.json({ message: 'Apply completed successfully' });
150
- } catch (err) {
151
- res.status(500).json({ message: err.message });
152
- } finally {
153
- await checker.closeConnection();
154
- }
155
- });
156
- });
157
-
158
- app.post('/api/browse-folders', async (req, res) => {
159
- try {
160
- const { currentPath = packageRoot } = req.body;
161
- console.log('Browsing folder:', currentPath);
162
-
163
- const resolvedPath = currentPath.startsWith('/') || currentPath.match(/^[A-Z]:\\/)
164
- ? resolve(currentPath)
165
- : resolve(currentPath);
166
-
167
- console.log('Resolved path:', resolvedPath);
168
-
169
- const contents = await readdir(resolvedPath, { withFileTypes: true });
170
-
171
- // Separate directories and files
172
- const items = contents.map(dirent => ({
173
- name: dirent.name,
174
- path: join(resolvedPath, dirent.name),
175
- isDirectory: dirent.isDirectory(),
176
- isParent: false
177
- }));
178
-
179
- // Sort items: directories first, then files, both alphabetically
180
- const sortedItems = items.sort((a, b) => {
181
- if (a.isDirectory === b.isDirectory) {
182
- return a.name.localeCompare(b.name);
183
- }
184
- return a.isDirectory ? -1 : 1;
185
- });
186
-
187
- // Add parent directory option if not at root
188
- if (resolvedPath !== resolve(resolvedPath, '..')) {
189
- sortedItems.unshift({
190
- name: '..',
191
- path: resolve(resolvedPath, '..'),
192
- isDirectory: true,
193
- isParent: true
194
- });
195
- }
196
-
197
- res.json({
198
- currentPath: resolvedPath,
199
- items: sortedItems
200
- });
201
- } catch (error) {
202
- res.status(500).json({ error: error.message });
203
- }
204
- });
205
-
206
- app.post('/api/create-folder', async (req, res) => {
207
- try {
208
- const { folderPath } = req.body;
209
- console.log('Creating folder:', folderPath);
210
-
211
- const resolvedPath = folderPath.startsWith('/') || folderPath.match(/^[A-Z]:\\/)
212
- ? resolve(folderPath)
213
- : resolve(packageRoot, folderPath);
214
-
215
- await mkdir(resolvedPath, { recursive: true });
216
-
217
- res.json({
218
- success: true,
219
- path: resolvedPath
220
- });
221
- } catch (error) {
222
- res.status(500).json({ error: error.message });
223
- }
224
- });
225
-
226
- // Start the server
227
- server.listen(port, () => {
228
- console.log(`Server is running on http://localhost:${port}`);
229
- });
1
+ import express from 'express';
2
+ import http from 'http';
3
+ import { dumpAllDatabases, getDatabasesFromExistingDumps } from './dump.js'; // Import necessary functions
4
+ import { compareAllDatabases } from './compare.js';
5
+ import { DBStructureChecker } from './index.js'; // Import your DBStructureChecker class
6
+ import { applyDifferences } from './apply.js';
7
+ import cors from 'cors';
8
+ import { join } from 'path';
9
+ import { readdir, mkdir } from 'fs/promises';
10
+ import { resolve } from 'path';
11
+ import { fileURLToPath } from 'url';
12
+ import { dirname } from 'path';
13
+
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = dirname(__filename);
16
+
17
+ const setConfig = (config, argv) => {
18
+ if(!config.host && argv.host){
19
+ config.host = argv.host;
20
+ }
21
+ if(!config.port && argv.port){
22
+ config.port = argv.port;
23
+ }
24
+ if(!config.user && argv.user){
25
+ config.user = argv.user;
26
+ }
27
+ if(!config.password && argv.password){
28
+ config.password = argv.password;
29
+ }
30
+ if(!config.output && argv.output !=='../db-dump'){
31
+ config.output = argv.output;
32
+ }
33
+ return config;
34
+ }
35
+
36
+ // Create a function to get the package root directory that works in both ESM and CJS
37
+ const getPackageRoot = () => {
38
+ if (typeof __dirname !== 'undefined') {
39
+ // CommonJS
40
+ return join(__dirname, '..');
41
+ } else {
42
+ // ESM
43
+ const __filename = fileURLToPath(import.meta.url);
44
+ const __dirname = dirname(__filename);
45
+ return join(__dirname, '..');
46
+ }
47
+ };
48
+
49
+ // Use the function to get the root path
50
+ const packageRoot = getPackageRoot();
51
+
52
+ export const startApiServer = (port, argv) => {
53
+ const app = express();
54
+ app.use(cors({ origin: '*' }));
55
+ const server = http.createServer(app);
56
+ app.use(express.json({ limit: '50mb' }));
57
+ app.use(express.urlencoded({ limit: '50mb', extended: true }));
58
+
59
+ console.log(`Serving UI from ${join(packageRoot, 'ui/dist/spa')}`);
60
+ app.use(express.static(join(packageRoot, 'ui/dist/spa')));
61
+
62
+ app.post('/api/getDatabasesFromExistingDumps', async (req, res) => {
63
+ let { config } = req.body;
64
+ config = setConfig(config, argv);
65
+ const databases = await getDatabasesFromExistingDumps(config);
66
+ res.json(databases);
67
+ });
68
+
69
+ // API endpoint to dump the database
70
+ app.post('/api/dump', async (req, res) => {
71
+ let { config, database } = req.body;
72
+ config = setConfig(config, argv);
73
+ const checker = new DBStructureChecker(config, database);
74
+
75
+ checker.connection.on('error', (err) => {
76
+ console.error('Database connection error in route dump:', err);
77
+ res.status(500).json({ message: 'Database connection failed', error: err.message });
78
+ });
79
+
80
+ checker.connection.on('connect', async () => {
81
+ console.log('Connected to the database');
82
+ checker.targetDatabase = database;
83
+ checker.command = 'dump';
84
+ checker.options = req.body;
85
+ try {
86
+ console.log('Dumping the database structure to files');
87
+ await dumpAllDatabases(checker.connection, 'dump', config);
88
+ res.json({ message: 'Dump completed successfully' });
89
+ } catch (err) {
90
+ res.status(500).json({ message: err.message });
91
+ } finally {
92
+ await checker.closeConnection();
93
+ }
94
+ });
95
+ });
96
+
97
+ // API endpoint to compare the database
98
+ app.post('/api/compare', async (req, res) => {
99
+ let { config, database } = req.body;
100
+ config = setConfig(config, argv);
101
+ const checker = new DBStructureChecker(config, database);
102
+
103
+ checker.connection.on('error', (err) => {
104
+ console.error('Database connection error in route compare:', err);
105
+ res.status(500).json({ message: 'Database connection failed', error: err.message });
106
+ });
107
+
108
+ checker.connection.on('connect', async () => {
109
+ console.log('Connected to the database');
110
+ checker.targetDatabase = config.database;
111
+ checker.command = 'compare';
112
+ checker.options = req.body;
113
+ try {
114
+ delete req.body.output;
115
+ console.log('Comparing the database structure with JSON dumps (dry run)');
116
+ let diffs = await compareAllDatabases(checker.connection, config.output, false, 'compare', config);
117
+ res.json(diffs);
118
+ } catch (err) {
119
+ console.log(err);
120
+ console.log(req.body);
121
+ res.status(500).json({ message: err.message });
122
+ } finally {
123
+ await checker.closeConnection();
124
+ }
125
+ })
126
+
127
+ });
128
+
129
+ // API endpoint to apply differences
130
+ app.post('/api/apply', async (req, res) => {
131
+ let { config, database, diffs } = req.body;
132
+ config = setConfig(config, argv);
133
+ console.log('Applying differences:', diffs);
134
+ const checker = new DBStructureChecker(config, database);
135
+
136
+ checker.connection.on('error', (err) => {
137
+ console.error('Database connection error in route apply:', err);
138
+ res.status(500).json({ message: 'Database connection failed', error: err.message });
139
+ });
140
+
141
+ checker.connection.on('connect', async () => {
142
+ console.log('Connected to the database');
143
+ checker.targetDatabase = database;
144
+ checker.command = 'apply';
145
+ checker.options = req.body;
146
+ try {
147
+ console.log('Applying the database structure based on JSON dumps');
148
+ await applyDifferences(checker.connection, database, diffs);
149
+ res.json({ message: 'Apply completed successfully' });
150
+ } catch (err) {
151
+ res.status(500).json({ message: err.message });
152
+ } finally {
153
+ await checker.closeConnection();
154
+ }
155
+ });
156
+ });
157
+
158
+ app.post('/api/browse-folders', async (req, res) => {
159
+ try {
160
+ const { currentPath = packageRoot } = req.body;
161
+ console.log('Browsing folder:', currentPath);
162
+
163
+ const resolvedPath = currentPath.startsWith('/') || currentPath.match(/^[A-Z]:\\/)
164
+ ? resolve(currentPath)
165
+ : resolve(currentPath);
166
+
167
+ console.log('Resolved path:', resolvedPath);
168
+
169
+ const contents = await readdir(resolvedPath, { withFileTypes: true });
170
+
171
+ // Separate directories and files
172
+ const items = contents.map(dirent => ({
173
+ name: dirent.name,
174
+ path: join(resolvedPath, dirent.name),
175
+ isDirectory: dirent.isDirectory(),
176
+ isParent: false
177
+ }));
178
+
179
+ // Sort items: directories first, then files, both alphabetically
180
+ const sortedItems = items.sort((a, b) => {
181
+ if (a.isDirectory === b.isDirectory) {
182
+ return a.name.localeCompare(b.name);
183
+ }
184
+ return a.isDirectory ? -1 : 1;
185
+ });
186
+
187
+ // Add parent directory option if not at root
188
+ if (resolvedPath !== resolve(resolvedPath, '..')) {
189
+ sortedItems.unshift({
190
+ name: '..',
191
+ path: resolve(resolvedPath, '..'),
192
+ isDirectory: true,
193
+ isParent: true
194
+ });
195
+ }
196
+
197
+ res.json({
198
+ currentPath: resolvedPath,
199
+ items: sortedItems
200
+ });
201
+ } catch (error) {
202
+ res.status(500).json({ error: error.message });
203
+ }
204
+ });
205
+
206
+ app.post('/api/create-folder', async (req, res) => {
207
+ try {
208
+ const { folderPath } = req.body;
209
+ console.log('Creating folder:', folderPath);
210
+
211
+ const resolvedPath = folderPath.startsWith('/') || folderPath.match(/^[A-Z]:\\/)
212
+ ? resolve(folderPath)
213
+ : resolve(packageRoot, folderPath);
214
+
215
+ await mkdir(resolvedPath, { recursive: true });
216
+
217
+ res.json({
218
+ success: true,
219
+ path: resolvedPath
220
+ });
221
+ } catch (error) {
222
+ res.status(500).json({ error: error.message });
223
+ }
224
+ });
225
+
226
+ // Start the server
227
+ server.listen(port, () => {
228
+ console.log(`Server is running on http://localhost:${port}`);
229
+ });
230
230
  };