diff --git a/package.json b/package.json index d05749d..3544200 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mit.db", - "version": "2023.02.23", + "version": "2023.02.23.2", "description": "An easy and quick database", "main": "build/index.js", "types": "build/index.d.ts", diff --git a/src/index.ts b/src/index.ts index 8045b5a..a504a86 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,8 +3,6 @@ import * as fs from 'fs'; const writeDB = promisify(fs.writeFile); -let map: any, filename: string; - class MitDB { /** * @constructor @@ -13,17 +11,16 @@ class MitDB { * @param options Options to pass in the constructor * @param options.dirname where to put the database? */ - constructor(fn?: string, options?: { dirname: string }) { - map = new Map(); - - if (fn) filename = fn; - - if (options && options.dirname) const dirname = options.dirname; - else const dirname = 'data'; + constructor(fn: string, options?: { dirname: string }) { + if (options && options.dirname) { + this.dirname = options.dirname; + } else { + this.dirname = 'data'; + } 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) { try { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); const i = data.findIndex((pair: any) => pair.key == key); !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; } catch { - await writeDB(db, `[${JSON.stringify({ key, value })}]`).then(() => { - return JSON.parse(fs.readFileSync(db).toString()); + await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => { + return JSON.parse(fs.readFileSync(this.db).toString()); }); } @@ -57,7 +54,7 @@ class MitDB { */ get(key: string | number) { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.find((pair: any) => pair.key == key)?.value || undefined; @@ -68,28 +65,28 @@ class MitDB { * @param key */ has(key: string | number) { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.find((pair: any) => pair.key == key) ? true : false; } entries() { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.map((pair: any) => [pair.key, pair.value]); } keys() { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.map((pair: any) => pair.key); } values() { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.map((pair: any) => pair.value); @@ -100,7 +97,7 @@ class MitDB { * @param callbackfn */ forEach(callback: (value: any, key: any, map: Map) => void) { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); data.forEach((pair: any) => callback(pair.value, pair.key, map)); @@ -112,14 +109,14 @@ class MitDB { */ async delete(key: string | number) { try { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); const i = data.findIndex((pair: any) => pair.key == key); if (data[i]) { data.splice(i, 1); - await writeDB(db, JSON.stringify(data)); + await writeDB(this.db, JSON.stringify(data)); return true; } else if (!map) { @@ -130,11 +127,11 @@ class MitDB { } async clear() { - await writeDB(db, JSON.stringify([])).catch(() => {}); + await writeDB(this.db, JSON.stringify([])).catch(() => {}); } size() { - const file = fs.readFileSync(db); + const file = fs.readFileSync(this.db); const data: any[] = JSON.parse(file.toString()); return data.length;