mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-20 07:46:00 +00:00
query history - prepare
This commit is contained in:
45
packages/api/src/controllers/queryHistory.js
Normal file
45
packages/api/src/controllers/queryHistory.js
Normal file
@@ -0,0 +1,45 @@
|
||||
const fsReverse = require('fs-reverse');
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const { datadir } = require('../utility/directories');
|
||||
const _ = require('lodash');
|
||||
const { filterName } = require('dbgate-tools');
|
||||
|
||||
function readCore(reader, skip, limit, filter) {
|
||||
return new Promise(resolve => {
|
||||
const res = [];
|
||||
let readed = 0;
|
||||
reader.on('data', line => {
|
||||
const json = JSON.parse(line);
|
||||
if (filterName(filter, json.sql)) {
|
||||
if (!skip || readed >= skip) {
|
||||
res.push(json);
|
||||
}
|
||||
readed++;
|
||||
if (limit && readed > (skip || 0) + limit) {
|
||||
reader.destroy();
|
||||
resolve(res);
|
||||
}
|
||||
}
|
||||
});
|
||||
reader.on('end', () => resolve(res));
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
read_meta: 'get',
|
||||
async read({ skip, limit, filter }) {
|
||||
const fileName = path.join(datadir(), 'query-history.jsonl');
|
||||
// @ts-ignore
|
||||
if (!(await fs.exists(fileName))) return [];
|
||||
const reader = fsReverse(fileName);
|
||||
const res = await readCore(reader, skip, limit, filter);
|
||||
return res;
|
||||
},
|
||||
|
||||
write_meta: 'post',
|
||||
async write({ data }) {
|
||||
const fileName = path.join(datadir(), 'query-history.jsonl');
|
||||
await fs.appendFile(fileName, JSON.stringify(data) + '\n');
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user