This commit is contained in:
Killer Boss Original 2023-02-23 12:06:29 +01:00 committed by GitHub
parent e269f2c9d3
commit e74533f1f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 25 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "mit.db", "name": "mit.db",
"version": "2023.02.23", "version": "2023.02.23.2",
"description": "An easy and quick database", "description": "An easy and quick database",
"main": "build/index.js", "main": "build/index.js",
"types": "build/index.d.ts", "types": "build/index.d.ts",

View file

@ -3,8 +3,6 @@ import * as fs from 'fs';
const writeDB = promisify(fs.writeFile); const writeDB = promisify(fs.writeFile);
let map: any, filename: string;
class MitDB { class MitDB {
/** /**
* @constructor * @constructor
@ -13,17 +11,16 @@ class MitDB {
* @param options Options to pass in the constructor * @param options Options to pass in the constructor
* @param options.dirname where to put the database? * @param options.dirname where to put the database?
*/ */
constructor(fn?: string, options?: { dirname: string }) { constructor(fn: string, options?: { dirname: string }) {
map = new Map(); if (options && options.dirname) {
this.dirname = options.dirname;
if (fn) filename = fn; } else {
this.dirname = 'data';
if (options && options.dirname) const dirname = options.dirname; }
else const dirname = 'data';
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname); if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
const db = `./${dirname}/${filename}` this.db = `./${dirname}/${filename}`
} }
/** /**
@ -33,18 +30,18 @@ class MitDB {
*/ */
async set(key: string | number, value: any) { async set(key: string | number, value: any) {
try { try {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
const i = data.findIndex((pair: any) => pair.key == key); const i = data.findIndex((pair: any) => pair.key == key);
!data[i] ? data.push({ key, value }) : data[i] = { key, value }; !data[i] ? data.push({ key, value }) : data[i] = { key, value };
await writeDB(db, JSON.stringify(data)); await writeDB(this.db, JSON.stringify(data));
return data; return data;
} catch { } catch {
await writeDB(db, `[${JSON.stringify({ key, value })}]`).then(() => { await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
return JSON.parse(fs.readFileSync(db).toString()); return JSON.parse(fs.readFileSync(this.db).toString());
}); });
} }
@ -57,7 +54,7 @@ class MitDB {
*/ */
get(key: string | number) { get(key: string | number) {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.find((pair: any) => pair.key == key)?.value || undefined; return data.find((pair: any) => pair.key == key)?.value || undefined;
@ -68,28 +65,28 @@ class MitDB {
* @param key * @param key
*/ */
has(key: string | number) { has(key: string | number) {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.find((pair: any) => pair.key == key) ? true : false; return data.find((pair: any) => pair.key == key) ? true : false;
} }
entries() { entries() {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => [pair.key, pair.value]); return data.map((pair: any) => [pair.key, pair.value]);
} }
keys() { keys() {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => pair.key); return data.map((pair: any) => pair.key);
} }
values() { values() {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => pair.value); return data.map((pair: any) => pair.value);
@ -100,7 +97,7 @@ class MitDB {
* @param callbackfn * @param callbackfn
*/ */
forEach(callback: (value: any, key: any, map: Map<any, any>) => void) { forEach(callback: (value: any, key: any, map: Map<any, any>) => void) {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
data.forEach((pair: any) => callback(pair.value, pair.key, map)); data.forEach((pair: any) => callback(pair.value, pair.key, map));
@ -112,14 +109,14 @@ class MitDB {
*/ */
async delete(key: string | number) { async delete(key: string | number) {
try { try {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
const i = data.findIndex((pair: any) => pair.key == key); const i = data.findIndex((pair: any) => pair.key == key);
if (data[i]) { if (data[i]) {
data.splice(i, 1); data.splice(i, 1);
await writeDB(db, JSON.stringify(data)); await writeDB(this.db, JSON.stringify(data));
return true; return true;
} else if (!map) { } else if (!map) {
@ -130,11 +127,11 @@ class MitDB {
} }
async clear() { async clear() {
await writeDB(db, JSON.stringify([])).catch(() => {}); await writeDB(this.db, JSON.stringify([])).catch(() => {});
} }
size() { size() {
const file = fs.readFileSync(db); const file = fs.readFileSync(this.db);
const data: any[] = JSON.parse(file.toString()); const data: any[] = JSON.parse(file.toString());
return data.length; return data.length;