Skip to content

Commit

Permalink
Merge pull request #46 from dappnode/dapplion/fix-db
Browse files Browse the repository at this point in the history
Replace lowdb with simple json file db
  • Loading branch information
dapplion authored Apr 10, 2021
2 parents 864f81c + dd2c986 commit 18bccec
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 82 deletions.
3 changes: 0 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.8",
"@types/lowdb": "^1.0.9",
"@types/mocha": "^8.0.4",
"@typescript-eslint/eslint-plugin": "^4.14.2",
"@typescript-eslint/parser": "^4.14.2",
"eslint": "^7.19.0",
"mocha": "^8.2.1",
"prettier": "^2.2.1",
"ts-node": "^9.0.0",
"tsc": "^1.20150623.0",
"tslint": "^6.1.3",
"typescript": "^4.0.3"
},
Expand All @@ -31,7 +29,6 @@
"@types/morgan": "^1.9.2",
"axios": "^0.21.1",
"express": "^4.17.1",
"lowdb": "^1.0.0",
"morgan": "^1.10.0"
}
}
12 changes: 6 additions & 6 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ app.get(
const from = await sanitizeFrom(req.query.from as string);
const to = sanitizeTo(req.query.to as string);

const entries = entriesDb.get();
const entries = entriesDb.read();
if (entries.some(entry => entry.from === from)) {
throw new BadRequestError("External endpoint already exists");
}
Expand All @@ -32,7 +32,7 @@ app.get(
}

entries.push({ from, to });
entriesDb.set(entries);
entriesDb.write(entries);

await reconfigureNGINX();
})
Expand All @@ -43,16 +43,16 @@ app.get(
asyncHandler(async req => {
const from = await sanitizeFrom(req.query.from as string);

const entries = entriesDb.get();
entriesDb.set(entries.filter(e => e.from !== from));
const entries = entriesDb.read();
entriesDb.write(entries.filter(e => e.from !== from));

await reconfigureNGINX();
})
);

app.get(
"/",
asyncHandler(async () => entriesDb.get())
asyncHandler(async () => entriesDb.read())
);

app.get(
Expand All @@ -63,7 +63,7 @@ app.get(
app.get(
"/clear",
asyncHandler(async () => {
entriesDb.clear();
entriesDb.write([]);
await reconfigureNGINX();
})
);
Expand Down
26 changes: 5 additions & 21 deletions api/src/db.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,8 @@
import fs from "fs";
import path from "path";
import lowdb from "lowdb";
import FileSync from "lowdb/adapters/FileSync";
import { DomainMapping } from "./types";
import { config } from "./config";
import { JsonFileDb } from "./utils/fileDb";

const dbPath = config.db_filepath;
fs.mkdirSync(path.dirname(dbPath), { recursive: true });

const adapter = new FileSync<DomainMapping[]>(dbPath, { defaultValue: [] });
const db = lowdb(adapter);

export const entriesDb = {
get(): DomainMapping[] {
return db.getState();
},
set(entries: DomainMapping[]): void {
db.setState(entries).write();
},
clear(): void {
this.set([]);
}
};
export const entriesDb = new JsonFileDb<DomainMapping[]>(
config.db_filepath,
[]
);
2 changes: 1 addition & 1 deletion api/src/nginx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function reconfigureNGINX(): Promise<void> {
const dappnodeDomain = await getDAppNodeDomain();

// Write domain mapping file
writeDomainsFile(entriesDb.get(), dappnodeDomain);
writeDomainsFile(entriesDb.read(), dappnodeDomain);

// reconfig NGINX
const reconfigOutput = await shell("reconfig");
Expand Down
55 changes: 55 additions & 0 deletions api/src/utils/fileDb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from "fs";
import path from "path";

export class PlainTextFileDb {
private filepath: string;

constructor(filepath: string) {
this.filepath = filepath;
}

read(): string | undefined {
try {
return fs.readFileSync(this.filepath, "utf8").trim();
} catch (e) {
if (e.code !== "ENOENT") throw e;
}
}

write(data: string): void {
fs.mkdirSync(path.dirname(this.filepath), { recursive: true });
fs.writeFileSync(this.filepath, data);
}

del(): void {
try {
fs.unlinkSync(this.filepath);
} catch (e) {
if (e.code !== "ENOENT") throw e;
}
}
}

export class JsonFileDb<T> {
private fileDb: PlainTextFileDb;
private defaultValue: T;

constructor(filepath: string, defaultValue: T) {
this.fileDb = new PlainTextFileDb(filepath);
this.defaultValue = defaultValue;
}

read(): T {
const data = this.fileDb.read();
if (data) return JSON.parse(data);
else return this.defaultValue;
}

write(data: T): void {
this.fileDb.write(JSON.stringify(data, null, 2));
}

del(): void {
this.fileDb.del();
}
}
52 changes: 1 addition & 51 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.7.tgz#98a993516c859eb0d5c4c8f098317a9ea68db9ad"
integrity sha512-cxWFQVseBm6O9Gbw1IWb8r6OS4OhSt3hPZLkFApLjM8TEXROBuQGLAH2i2gZpcXdLBIrpXuTDhH7Vbm1iXmNGA==

"@types/lodash@*":
version "4.14.168"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.168.tgz#fe24632e79b7ade3f132891afff86caa5e5ce008"
integrity sha512-oVfRvqHV/V6D1yifJbVRU3TMp8OT6o6BG+U9MkwuJ3U8/CsDHvalRpsxBqivn71ztOFZBTfJMvETbqHiaNSj7Q==

"@types/lowdb@^1.0.9":
version "1.0.9"
resolved "https://registry.yarnpkg.com/@types/lowdb/-/lowdb-1.0.9.tgz#1f6c27df72dd1c64522cc9a4566796d2dd058d38"
integrity sha512-LBRG5EPXFOJDoJc9jACstMhtMP+u+UkPYllBeGQXXKiaHc+uzJs9+/Aynb/5KkX33DtrIiKyzNVTPQc/4RcD6A==
dependencies:
"@types/lodash" "*"

"@types/mime@^1":
version "1.3.2"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a"
Expand Down Expand Up @@ -983,11 +971,6 @@ globby@^11.0.1:
merge2 "^1.3.0"
slash "^3.0.0"

graceful-fs@^4.1.3:
version "4.2.4"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.4.tgz#2256bde14d3632958c465ebc96dc467ca07a29fb"
integrity sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==

[email protected]:
version "1.10.5"
resolved "https://registry.yarnpkg.com/growl/-/growl-1.10.5.tgz#f2735dc2283674fa67478b10181059355c369e5e"
Expand Down Expand Up @@ -1136,11 +1119,6 @@ is-plain-obj@^2.1.0:
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==

is-promise@^2.1.0:
version "2.2.2"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1"
integrity sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -1205,7 +1183,7 @@ locate-path@^6.0.0:
dependencies:
p-locate "^5.0.0"

lodash@4, lodash@^4.17.15, lodash@^4.17.20:
lodash@^4.17.15, lodash@^4.17.20:
version "4.17.20"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.20.tgz#b44a9b6297bcb698f1c51a3545a2b3b368d59c52"
integrity sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==
Expand All @@ -1217,17 +1195,6 @@ [email protected]:
dependencies:
chalk "^4.0.0"

lowdb@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/lowdb/-/lowdb-1.0.0.tgz#5243be6b22786ccce30e50c9a33eac36b20c8064"
integrity sha512-2+x8esE/Wb9SQ1F9IHaYWfsC9FIecLOPrK4g17FGEayjUWH172H6nwicRovGvSE2CPZouc2MCIqCI7h9d+GftQ==
dependencies:
graceful-fs "^4.1.3"
is-promise "^2.1.0"
lodash "4"
pify "^3.0.0"
steno "^0.4.1"

lru-cache@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
Expand Down Expand Up @@ -1497,11 +1464,6 @@ picomatch@^2.0.4, picomatch@^2.0.5, picomatch@^2.2.1:
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.2.tgz#21f333e9b6b8eaff02468f5146ea406d345f4dad"
integrity sha512-q0M/9eZHzmr0AulXyPwNfZjtwZ/RBZlbN3K3CErVrk50T2ASYI7Bye0EvekFY3IP1Nt2DHu0re+V2ZHIpMkuWg==

pify@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-3.0.0.tgz#e5a4acd2c101fdf3d9a4d07f0dbc4db49dd28176"
integrity sha1-5aSs0sEB/fPZpNB/DbxNtJ3SgXY=

prelude-ls@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.2.1.tgz#debc6489d7a6e6b0e7611888cec880337d316396"
Expand Down Expand Up @@ -1736,13 +1698,6 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=

steno@^0.4.1:
version "0.4.4"
resolved "https://registry.yarnpkg.com/steno/-/steno-0.4.4.tgz#071105bdfc286e6615c0403c27e9d7b5dcb855cb"
integrity sha1-BxEFvfwobmYVwEA8J+nXtdy4Vcs=
dependencies:
graceful-fs "^4.1.3"

"string-width@^1.0.2 || 2":
version "2.1.1"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-2.1.1.tgz#ab93f27a8dc13d28cac815c462143a6d9012ae9e"
Expand Down Expand Up @@ -1848,11 +1803,6 @@ ts-node@^9.0.0:
source-map-support "^0.5.17"
yn "3.1.1"

tsc@^1.20150623.0:
version "1.20150623.0"
resolved "https://registry.yarnpkg.com/tsc/-/tsc-1.20150623.0.tgz#4ebc3c774e169148cbc768a7342533f082c7a6e5"
integrity sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=

tslib@^1.13.0, tslib@^1.8.1:
version "1.14.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
Expand Down

0 comments on commit 18bccec

Please sign in to comment.