removed nedb dependency, replaced with own impl

This commit is contained in:
Jan Prochazka
2022-01-29 11:53:34 +01:00
parent c89e3adb38
commit 89b3477446
5 changed files with 22 additions and 57 deletions

View File

@@ -1,7 +1,6 @@
const path = require('path');
const { fork } = require('child_process');
const _ = require('lodash');
const nedb = require('nedb-promises');
const fs = require('fs-extra');
const { datadir, filesdir } = require('../utility/directories');
@@ -9,6 +8,7 @@ const socket = require('../utility/socket');
const { encryptConnection } = require('../utility/crypting');
const { handleProcessCommunication } = require('../utility/processComm');
const { pickSafeConnectionInfo } = require('../utility/crypting');
const JsonLinesDatabase = require('../utility/JsonLinesDatabase');
const processArgs = require('../utility/processArgs');
@@ -136,7 +136,7 @@ module.exports = {
const dir = datadir();
if (!portalConnections) {
// @ts-ignore
this.datastore = nedb.create(path.join(dir, 'connections.jsonl'));
this.datastore = new JsonLinesDatabase(path.join(dir, 'connections.jsonl'));
}
},
@@ -173,7 +173,7 @@ module.exports = {
let res;
const encrypted = encryptConnection(connection);
if (connection._id) {
res = await this.datastore.update(_.pick(connection, '_id'), encrypted);
res = await this.datastore.update(encrypted);
} else {
res = await this.datastore.insert(encrypted);
}
@@ -188,7 +188,7 @@ module.exports = {
update_meta: true,
async update({ _id, values }) {
if (portalConnections) return;
const res = await this.datastore.update({ _id }, { $set: values });
const res = await this.datastore.patch(_id, values);
socket.emitChanged('connection-list-changed');
return res;
},
@@ -196,14 +196,14 @@ module.exports = {
updateDatabase_meta: true,
async updateDatabase({ conid, database, values }) {
if (portalConnections) return;
const conn = await this.datastore.find({ _id: conid });
let databases = conn[0].databases || [];
const conn = await this.datastore.get(conid);
let databases = (conn && conn.databases) || [];
if (databases.find(x => x.name == database)) {
databases = databases.map(x => (x.name == database ? { ...x, ...values } : x));
} else {
databases = [...databases, { name: database, ...values }];
}
const res = await this.datastore.update({ _id: conid }, { $set: { databases } });
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}`);
@@ -213,7 +213,7 @@ module.exports = {
delete_meta: true,
async delete(connection) {
if (portalConnections) return;
const res = await this.datastore.remove(_.pick(connection, '_id'));
const res = await this.datastore.remove(connection._id);
socket.emitChanged('connection-list-changed');
return res;
},
@@ -221,8 +221,8 @@ module.exports = {
get_meta: true,
async get({ conid }) {
if (portalConnections) return portalConnections.find(x => x._id == conid) || null;
const res = await this.datastore.find({ _id: conid });
return res[0] || null;
const res = await this.datastore.get(conid);
return res || null;
},
newSqliteDatabase_meta: true,

View File

@@ -4,7 +4,7 @@ const uuidv1 = require('uuid/v1');
// const lineReader = require('line-reader');
// const { fetchNextLineFromReader } = require('./JsonLinesDatastore');
export default class JsonLinesDatabase {
class JsonLinesDatabase {
constructor(filename) {
this.filename = filename;
this.data = [];
@@ -37,6 +37,7 @@ export default class JsonLinesDatabase {
}
async insert(obj) {
await this._ensureLoaded();
if (obj._id && (await this.get(obj._id))) {
throw new Error(`Cannot insert duplicate ID ${obj._id} into ${this.filename}`);
}
@@ -52,10 +53,12 @@ export default class JsonLinesDatabase {
}
async get(id) {
await this._ensureLoaded();
return this.data.find(x => x._id == id);
}
async find(cond) {
await this._ensureLoaded();
if (cond) {
return this.data.filter(x => {
for (const key of Object.keys(cond)) {
@@ -69,17 +72,20 @@ export default class JsonLinesDatabase {
}
async update(obj) {
await this._ensureLoaded();
this.data = this.data.map(x => (x._id == obj._id ? obj : x));
await this._write();
}
async patch(id, values) {
await this._ensureLoaded();
this.data = this.data.map(x => (x._id == id ? { ...x, ...values } : x));
await this._write();
}
async remove(id) {
this.data = this.data.filter(x => x._id!=id);
await this._ensureLoaded();
this.data = this.data.filter(x => x._id != id);
await this._write();
}
@@ -116,3 +122,5 @@ export default class JsonLinesDatabase {
// await fw.end();
// }
}
module.exports = JsonLinesDatabase;

View File

@@ -4,7 +4,7 @@ const lock = new AsyncLock();
const stableStringify = require('json-stable-stringify');
const { evaluateCondition } = require('dbgate-sqltree');
export async function fetchNextLineFromReader(reader) {
function fetchNextLineFromReader(reader) {
return new Promise((resolve, reject) => {
if (!reader.hasNextLine()) {
resolve(null);