mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 17:46:00 +00:00
using nedb for connections, controllers
This commit is contained in:
36
api/src/controllers/connections.js
Normal file
36
api/src/controllers/connections.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { fork } = require('child_process');
|
||||
const _ = require('lodash');
|
||||
const datadir = require('../utility/datadir');
|
||||
const nedb = require('nedb-promises');
|
||||
|
||||
module.exports = {
|
||||
datastore: null,
|
||||
async _init() {
|
||||
const dir = await datadir();
|
||||
this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
|
||||
},
|
||||
list_meta: 'get',
|
||||
async list() {
|
||||
return this.datastore.find();
|
||||
},
|
||||
test_meta: {
|
||||
method: 'post',
|
||||
raw: true,
|
||||
},
|
||||
test(req, res) {
|
||||
const subprocess = fork(`${__dirname}/../connectProcess.js`);
|
||||
subprocess.send(req.body);
|
||||
subprocess.on('message', resp => res.json(resp));
|
||||
},
|
||||
|
||||
save_meta: 'post',
|
||||
async save(connection) {
|
||||
const res = await this.datastore.insert(connection);
|
||||
return res;
|
||||
},
|
||||
};
|
||||
@@ -1,7 +1,9 @@
|
||||
const express = require('express');
|
||||
const bodyParser = require('body-parser');
|
||||
const bodyParser = require('body-parser');
|
||||
const cors = require('cors');
|
||||
const connection = require('./connection');
|
||||
const useController = require('./utility/useController');
|
||||
const connections = require('./controllers/connections');
|
||||
const app = express();
|
||||
|
||||
app.use(cors());
|
||||
@@ -11,6 +13,7 @@ app.get('/', (req, res) => {
|
||||
res.send('DbGate API');
|
||||
});
|
||||
|
||||
useController(app, '/connections', connections);
|
||||
app.use('/connection', connection);
|
||||
|
||||
app.listen(3000);
|
||||
|
||||
18
api/src/utility/datadir.js
Normal file
18
api/src/utility/datadir.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const fs = require('fs-extra');
|
||||
|
||||
let created = false;
|
||||
|
||||
module.exports = async function datadir() {
|
||||
const dir = path.join(os.homedir(), 'dbgate-data');
|
||||
if (!created) {
|
||||
const stat = await fs.stat(dir);
|
||||
if (!stat.isDirectory) {
|
||||
await fs.mkdir(dir);
|
||||
}
|
||||
created = true;
|
||||
}
|
||||
|
||||
return dir;
|
||||
};
|
||||
49
api/src/utility/useController.js
Normal file
49
api/src/utility/useController.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const _ = require('lodash');
|
||||
const express = require('express');
|
||||
|
||||
module.exports = function useController(app, route, controller) {
|
||||
const router = express.Router();
|
||||
|
||||
for (const key of _.keys(controller)) {
|
||||
const obj = controller[key];
|
||||
if (!_.isFunction(obj)) continue;
|
||||
const meta = controller[`${key}_meta`];
|
||||
if (!meta) continue;
|
||||
|
||||
let method = 'get';
|
||||
let raw = false;
|
||||
let rawParams = false;
|
||||
|
||||
if (_.isString(meta)) {
|
||||
method = meta;
|
||||
}
|
||||
if (_.isPlainObject(meta)) {
|
||||
method = meta.method;
|
||||
raw = meta.raw;
|
||||
rawParams = meta.rawParams;
|
||||
}
|
||||
|
||||
const route = `/${_.kebabCase(key)}`;
|
||||
if (raw) {
|
||||
router[method](route, controller[key]);
|
||||
} else {
|
||||
router[method](route, async (req, res) => {
|
||||
if (controller._init && !controller._init_called) {
|
||||
await controller._init();
|
||||
controller._init_called = true;
|
||||
}
|
||||
try {
|
||||
let params = [{ ...req.body }];
|
||||
if (rawParams) params = [req, res];
|
||||
const data = await controller[key](...params);
|
||||
res.json(data);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
app.use(route, router);
|
||||
};
|
||||
Reference in New Issue
Block a user