First Commit

This commit is contained in:
Fabian
2019-09-18 21:54:28 +02:00
commit 429ba7e291
27 changed files with 5815 additions and 0 deletions

55
src/storage.ts Normal file
View File

@ -0,0 +1,55 @@
import * as fs from "fs";
if (!fs.existsSync("./databases/")) {
fs.mkdirSync("./databases");
}
import LevelUp, { LevelUp as LU } from "levelup";
import LevelDown, { LevelDown as LD } from "leveldown";
import { AbstractIterator } from "abstract-leveldown";
export type LevelDB = LU<LD, AbstractIterator<any, any>>;
const databases = new Map<string, LevelDB>();
export function resNull(err) {
if (!err.notFound)
throw err;
return null;
}
function rmRecursice(path: string) {
if (fs.existsSync(path)) {
fs.readdirSync(path).forEach(function (file, index) {
var curPath = path + "/" + file;
if (fs.lstatSync(curPath).isDirectory()) { // recurse
rmRecursice(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
};
export async function deleteLevelDB(name: string) {
if (!name || name === "")
return;
let db = databases.get(name);
if (db && !db.isClosed())
await db.close()
//TODO make sure, that name doesn't make it possible to delete all databases :)
rmRecursice("./databases/" + name);
}
export default function getLevelDB(name: string): LevelDB {
let db = databases.get(name);
if (!db || db.isClosed()) {
db = LevelUp(LevelDown("./databases/" + name));
databases.set(name, db);
}
return db;
}