Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ yarn-error.log*
app/src/nativeModulesContent.js
packages/api/src/nativeModulesContent.js
packages/api/src/packagedPluginsContent.js
.VSCodeCounter
.VSCodeCounter

.idea
.dbgate
6 changes: 3 additions & 3 deletions packages/api/.env
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
DEVMODE=1
SHELL_SCRIPTING=1
DEVWEB=1
runAsPortal=1
WORKSPACE_DIR=../../.dbgate

# PERMISSIONS=~widgets/app,~widgets/plugins
# DISABLE_SHELL=1
# HIDE_APP_EDITOR=1


# DEVWEB=1
# LOGINS=admin,test

# LOGIN_PASSWORD_admin=admin
# LOGIN_PERMISSIONS_admin=*

# LOGIN_PASSWORD_test=test
# LOGIN_PERMISSIONS_test=~*, widgets/database
# WORKSPACE_DIR=/home/jena/dbgate-data-2
2 changes: 1 addition & 1 deletion packages/api/src/controllers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
const permissions = login ? login.permissions : process.env.PERMISSIONS;

return {
runAsPortal: !!connections.portalConnections,
runAsPortal: !!connections.portalConnections || !!process.env.runAsPortal,
singleDbConnection: connections.singleDbConnection,
singleConnection: connections.singleConnection,
// hideAppEditor: !!process.env.HIDE_APP_EDITOR,
Expand Down
6 changes: 1 addition & 5 deletions packages/api/src/controllers/connections.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ module.exports = {
if (!portalConnections) {
// @ts-ignore
this.datastore = new JsonLinesDatabase(path.join(dir, 'connections.jsonl'));
this.datastore.watch();
}
},

Expand Down Expand Up @@ -273,7 +274,6 @@ module.exports = {
} else {
res = await this.datastore.insert(encrypted);
}
socket.emitChanged('connection-list-changed');
socket.emitChanged('used-apps-changed');
if (this._closeAll) {
this._closeAll(connection._id);
Expand All @@ -289,15 +289,13 @@ module.exports = {
if (portalConnections) return;
testConnectionPermission(_id, req);
const res = await this.datastore.patch(_id, values);
socket.emitChanged('connection-list-changed');
return res;
},

batchChangeFolder_meta: true,
async batchChangeFolder({ folder, newFolder }, req) {
// const updated = await this.datastore.find(x => x.parent == folder);
const res = await this.datastore.updateAll(x => (x.parent == folder ? { ...x, parent: newFolder } : x));
socket.emitChanged('connection-list-changed');
return res;
},

Expand All @@ -313,7 +311,6 @@ module.exports = {
databases = [...databases, { name: database, ...values }];
}
const res = await this.datastore.patch(conid, { databases });
socket.emitChanged('connection-list-changed');
socket.emitChanged('used-apps-changed');
// socket.emitChanged(`db-apps-changed-${conid}-${database}`);
return res;
Expand All @@ -324,7 +321,6 @@ module.exports = {
if (portalConnections) return;
testConnectionPermission(connection, req);
const res = await this.datastore.remove(connection._id);
socket.emitChanged('connection-list-changed');
return res;
},

Expand Down
27 changes: 27 additions & 0 deletions packages/api/src/utility/JsonLinesDatabase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const AsyncLock = require('async-lock');
const fs = require('fs-extra');
const uuidv1 = require('uuid/v1');
const socket = require('./socket');

const lock = new AsyncLock();

Expand Down Expand Up @@ -111,6 +112,32 @@ class JsonLinesDatabase {
return removed;
}

watch() {
if (!fs.existsSync(this.filename)) {
fs.writeFileSync(this.filename, '');
}

fs.watch(this.filename, { persistent: true }, (eventType, filename) => {
console.log(`File ${filename} changed, event type is: ${eventType}`);
if (eventType === 'change') {
this._reload();
socket.emitChanged('connection-list-changed');
}
});
}

async _reload() {
try {
const text = await fs.promises.readFile(this.filename, { encoding: 'utf-8' });
this.data = text
.split('\n')
.filter(x => x.trim())
.map(x => JSON.parse(x));
} catch (err) {
console.error(`Error reloading file ${this.filename}`, err);
}
}

// async _openReader() {
// return new Promise((resolve, reject) =>
// lineReader.open(this.filename, (err, reader) => {
Expand Down