dbgate-api 4.8.6 → 5.0.0-alpha.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/env/portal/.env CHANGED
@@ -38,7 +38,7 @@ SSH_LOGIN_mysqlssh=root
38
38
  SSH_PASSWORD_mysqlssh=xxx
39
39
 
40
40
  LABEL_sqlite=sqlite
41
- FILE_sqlite=/home/jena/dbgate-data/files/sqlite/feeds.sqlite
41
+ FILE_sqlite=/home/jena/.dbgate/files/sqlite/feeds.sqlite
42
42
  ENGINE_sqlite=sqlite@dbgate-plugin-sqlite
43
43
 
44
44
  # docker run -p 3000:3000 -e CONNECTIONS=mongo -e URL_mongo=mongodb://localhost:27017 -e ENGINE_mongo=mongo@dbgate-plugin-mongo -e LABEL_mongo=mongo dbgate/dbgate:beta
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dbgate-api",
3
3
  "main": "src/index.js",
4
- "version": "4.8.6",
4
+ "version": "5.0.0-alpha.1",
5
5
  "homepage": "https://dbgate.org/",
6
6
  "repository": {
7
7
  "type": "git",
@@ -25,9 +25,9 @@
25
25
  "compare-versions": "^3.6.0",
26
26
  "cors": "^2.8.5",
27
27
  "cross-env": "^6.0.3",
28
- "dbgate-query-splitter": "^4.8.3",
29
- "dbgate-sqltree": "^4.8.6",
30
- "dbgate-tools": "^4.8.6",
28
+ "dbgate-query-splitter": "^4.9.0",
29
+ "dbgate-sqltree": "^5.0.0-alpha.1",
30
+ "dbgate-tools": "^5.0.0-alpha.1",
31
31
  "diff": "^5.0.0",
32
32
  "diff2html": "^3.4.13",
33
33
  "eslint": "^6.8.0",
@@ -63,7 +63,7 @@
63
63
  "devDependencies": {
64
64
  "@types/fs-extra": "^9.0.11",
65
65
  "@types/lodash": "^4.14.149",
66
- "dbgate-types": "^4.8.6",
66
+ "dbgate-types": "^5.0.0-alpha.1",
67
67
  "env-cmd": "^10.1.0",
68
68
  "node-loader": "^1.0.2",
69
69
  "nodemon": "^2.0.2",
@@ -37,6 +37,7 @@ module.exports = {
37
37
  // hideAppEditor: !!process.env.HIDE_APP_EDITOR,
38
38
  allowShellConnection: platformInfo.allowShellConnection,
39
39
  allowShellScripting: platformInfo.allowShellConnection,
40
+ isDocker: platformInfo.isDocker,
40
41
  permissions,
41
42
  login,
42
43
  ...currentVersion,
@@ -62,6 +62,15 @@ module.exports = {
62
62
  return true;
63
63
  },
64
64
 
65
+ refresh_meta: true,
66
+ async refresh({ folders }, req) {
67
+ for (const folder of folders) {
68
+ socket.emitChanged(`files-changed-${folder}`);
69
+ socket.emitChanged(`all-files-changed`);
70
+ }
71
+ return true;
72
+ },
73
+
65
74
  copy_meta: true,
66
75
  async copy({ folder, file, newFile }, req) {
67
76
  if (!hasPermission(`files/${folder}/write`, req)) return false;
@@ -90,6 +99,12 @@ module.exports = {
90
99
  }
91
100
  },
92
101
 
102
+ loadFrom_meta: true,
103
+ async loadFrom({ filePath, format }, req) {
104
+ const text = await fs.readFile(filePath, { encoding: 'utf-8' });
105
+ return deserialize(format, text);
106
+ },
107
+
93
108
  save_meta: true,
94
109
  async save({ folder, file, data, format }, req) {
95
110
  if (folder.startsWith('archive:')) {
@@ -177,4 +192,24 @@ module.exports = {
177
192
  await fs.writeFile(filePath, getDiagramExport(html, css, themeType, themeClassName));
178
193
  return true;
179
194
  },
195
+
196
+ getFileRealPath_meta: true,
197
+ async getFileRealPath({ folder, file }, req) {
198
+ if (folder.startsWith('archive:')) {
199
+ if (!hasPermission(`archive/write`, req)) return false;
200
+ const dir = resolveArchiveFolder(folder.substring('archive:'.length));
201
+ return path.join(dir, file);
202
+ } else if (folder.startsWith('app:')) {
203
+ if (!hasPermission(`apps/write`, req)) return false;
204
+ const app = folder.substring('app:'.length);
205
+ return path.join(appdir(), app, file);
206
+ } else {
207
+ if (!hasPermission(`files/${folder}/write`, req)) return false;
208
+ const dir = path.join(filesdir(), folder);
209
+ if (!(await fs.exists(dir))) {
210
+ await fs.mkdir(dir);
211
+ }
212
+ return path.join(dir, file);
213
+ }
214
+ },
180
215
  };
@@ -162,6 +162,7 @@ module.exports = {
162
162
  authTypes_meta: true,
163
163
  async authTypes({ engine }) {
164
164
  const packageName = extractPackageName(engine);
165
+ if (!packageName) return null;
165
166
  const content = requirePlugin(packageName);
166
167
  const driver = content.drivers.find(x => x.engine == engine);
167
168
  if (!driver || !driver.getAuthTypes) return null;
@@ -1,5 +1,5 @@
1
1
 
2
2
  module.exports = {
3
- version: '4.8.6',
4
- buildTime: '2022-04-14T15:36:41.653Z'
3
+ version: '5.0.0-alpha.1',
4
+ buildTime: '2022-05-19T14:17:58.279Z'
5
5
  };
package/src/main.js CHANGED
@@ -155,8 +155,8 @@ function useAllControllers(app, electron) {
155
155
  useController(app, electron, '/apps', apps);
156
156
  }
157
157
 
158
- function initializeElectronSender(electronSender) {
158
+ function setElectronSender(electronSender) {
159
159
  socket.setElectronSender(electronSender);
160
160
  }
161
161
 
162
- module.exports = { start, useAllControllers, initializeElectronSender, configController: config };
162
+ module.exports = { start, useAllControllers, setElectronSender, configController: config };
@@ -0,0 +1,38 @@
1
+ const requireEngineDriver = require('../utility/requireEngineDriver');
2
+ const connectUtility = require('../utility/connectUtility');
3
+
4
+ function doDump(dumper) {
5
+ return new Promise((resolve, reject) => {
6
+ dumper.once('end', () => {
7
+ resolve(true);
8
+ });
9
+ dumper.once('error', err => {
10
+ reject(err);
11
+ });
12
+ dumper.run();
13
+ });
14
+ }
15
+
16
+ async function dumpDatabase({
17
+ connection = undefined,
18
+ systemConnection = undefined,
19
+ driver = undefined,
20
+ outputFile,
21
+ databaseName,
22
+ schemaName,
23
+ }) {
24
+ console.log(`Dumping database`);
25
+
26
+ if (!driver) driver = requireEngineDriver(connection);
27
+ const pool = systemConnection || (await connectUtility(driver, connection, 'read', { forceRowsAsObjects: true }));
28
+ console.log(`Connected.`);
29
+
30
+ const dumper = await driver.createBackupDumper(pool, {
31
+ outputFile,
32
+ databaseName,
33
+ schemaName,
34
+ });
35
+ await doDump(dumper);
36
+ }
37
+
38
+ module.exports = dumpDatabase;
@@ -0,0 +1,57 @@
1
+ const fs = require('fs');
2
+ const requireEngineDriver = require('../utility/requireEngineDriver');
3
+ const connectUtility = require('../utility/connectUtility');
4
+ const { splitQueryStream } = require('dbgate-query-splitter/lib/splitQueryStream');
5
+ const download = require('./download');
6
+ const stream = require('stream');
7
+
8
+ class ImportStream extends stream.Transform {
9
+ constructor(pool, driver) {
10
+ super({ objectMode: true });
11
+ this.pool = pool;
12
+ this.driver = driver;
13
+ }
14
+ async _transform(chunk, encoding, cb) {
15
+ try {
16
+ await this.driver.script(this.pool, chunk);
17
+ } catch (err) {
18
+ this.emit('error', err.message);
19
+ }
20
+ cb();
21
+ }
22
+ _flush(cb) {
23
+ this.push('finish');
24
+ cb();
25
+ this.emit('end');
26
+ }
27
+ }
28
+
29
+ function awaitStreamEnd(stream) {
30
+ return new Promise((resolve, reject) => {
31
+ stream.once('end', () => {
32
+ resolve(true);
33
+ });
34
+ stream.once('error', err => {
35
+ reject(err);
36
+ });
37
+ });
38
+ }
39
+
40
+ async function importDatabase({ connection = undefined, systemConnection = undefined, driver = undefined, inputFile }) {
41
+ console.log(`Importing database`);
42
+
43
+ if (!driver) driver = requireEngineDriver(connection);
44
+ const pool = systemConnection || (await connectUtility(driver, connection, 'write'));
45
+ console.log(`Connected.`);
46
+
47
+ const downloadedFile = await download(inputFile);
48
+
49
+ const fileStream = fs.createReadStream(downloadedFile, 'utf-8');
50
+ const splittedStream = splitQueryStream(fileStream, driver.getQuerySplitterOptions());
51
+ const importStream = new ImportStream(pool, driver);
52
+ // @ts-ignore
53
+ splittedStream.pipe(importStream);
54
+ await awaitStreamEnd(importStream);
55
+ }
56
+
57
+ module.exports = importDatabase;
@@ -21,6 +21,8 @@ const executeQuery = require('./executeQuery');
21
21
  const loadFile = require('./loadFile');
22
22
  const deployDb = require('./deployDb');
23
23
  const initializeApiEnvironment = require('./initializeApiEnvironment');
24
+ const dumpDatabase = require('./dumpDatabase');
25
+ const importDatabase = require('./importDatabase');
24
26
 
25
27
  const dbgateApi = {
26
28
  queryReader,
@@ -45,6 +47,8 @@ const dbgateApi = {
45
47
  loadFile,
46
48
  deployDb,
47
49
  initializeApiEnvironment,
50
+ dumpDatabase,
51
+ importDatabase,
48
52
  };
49
53
 
50
54
  requirePlugin.initializeDbgateApi(dbgateApi);
@@ -39,7 +39,7 @@ async function loadConnection(driver, storedConnection, connectionMode) {
39
39
  return storedConnection;
40
40
  }
41
41
 
42
- async function connectUtility(driver, storedConnection, connectionMode) {
42
+ async function connectUtility(driver, storedConnection, connectionMode, additionalOptions = null) {
43
43
  const connectionLoaded = await loadConnection(driver, storedConnection, connectionMode);
44
44
 
45
45
  const connection = {
@@ -93,7 +93,7 @@ async function connectUtility(driver, storedConnection, connectionMode) {
93
93
  }
94
94
  }
95
95
 
96
- const conn = await driver.connect(connection);
96
+ const conn = await driver.connect({ ...connection, ...additionalOptions });
97
97
  return conn;
98
98
  }
99
99
 
@@ -4,6 +4,7 @@ const fs = require('fs');
4
4
  const cleanDirectory = require('./cleanDirectory');
5
5
  const platformInfo = require('./platformInfo');
6
6
  const processArgs = require('./processArgs');
7
+ const consoleObjectWriter = require('../shell/consoleObjectWriter');
7
8
 
8
9
  const createDirectories = {};
9
10
  const ensureDirectory = (dir, clean) => {
@@ -27,7 +28,7 @@ function datadirCore() {
27
28
  if (processArgs.workspaceDir) {
28
29
  return processArgs.workspaceDir;
29
30
  }
30
- return path.join(os.homedir(), 'dbgate-data');
31
+ return path.join(os.homedir(), '.dbgate');
31
32
  }
32
33
 
33
34
  function datadir() {
@@ -112,6 +113,27 @@ function clearArchiveLinksCache() {
112
113
  archiveLinksCache = {};
113
114
  }
114
115
 
116
+ function migrateDataDir() {
117
+ if (process.env.WORKSPACE_DIR) {
118
+ return;
119
+ }
120
+ if (processArgs.workspaceDir) {
121
+ return;
122
+ }
123
+
124
+ try {
125
+ const oldDir = path.join(os.homedir(), 'dbgate-data');
126
+ const newDir = path.join(os.homedir(), '.dbgate');
127
+ if (fs.existsSync(oldDir) && !fs.existsSync(newDir)) {
128
+ fs.renameSync(oldDir, newDir);
129
+ }
130
+ } catch (e) {
131
+ console.log('Error migrating data dir:', e.message);
132
+ }
133
+ }
134
+
135
+ migrateDataDir();
136
+
115
137
  module.exports = {
116
138
  datadir,
117
139
  jsldir,
@@ -1,6 +1,6 @@
1
1
  let sseResponse = null;
2
2
  let electronSender = null;
3
- let init = '';
3
+ let init = [];
4
4
 
5
5
  module.exports = {
6
6
  setSseResponse(value) {
@@ -12,15 +12,25 @@ module.exports = {
12
12
  },
13
13
  emit(message, data) {
14
14
  if (electronSender) {
15
+ if (init.length > 0) {
16
+ for (const item of init) {
17
+ electronSender.send(item.message, item.data == null ? null : item.data);
18
+ }
19
+ init = [];
20
+ }
15
21
  electronSender.send(message, data == null ? null : data);
16
22
  } else if (sseResponse) {
17
- if (init) {
18
- sseResponse.write(init);
19
- init = '';
23
+ if (init.length > 0) {
24
+ for (const item of init) {
25
+ sseResponse.write(
26
+ `event: ${item.message}\ndata: ${JSON.stringify(item.data == null ? null : item.data)}\n\n`
27
+ );
28
+ }
29
+ init = [];
20
30
  }
21
31
  sseResponse.write(`event: ${message}\ndata: ${JSON.stringify(data == null ? null : data)}\n\n`);
22
32
  } else {
23
- init += sseResponse;
33
+ init.push([{ message, data }]);
24
34
  }
25
35
  },
26
36
  emitChanged(key) {