Compare commits
8 commits
v2023.02.2
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
c902d92711 | ||
|
90670ebb7b | ||
|
363492aef5 | ||
|
41d589ccbc | ||
|
74f4f42e50 | ||
|
492c49a086 | ||
|
c8410b0c6f | ||
|
387ab7e582 |
2 changed files with 17 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mit.db",
|
||||
"version": "2023.02.23.2",
|
||||
"version": "2023.04.12",
|
||||
"description": "An easy and quick database",
|
||||
"main": "build/index.js",
|
||||
"types": "build/index.d.ts",
|
||||
|
@ -11,7 +11,7 @@
|
|||
"type": "git",
|
||||
"url": "git+https://github.com/ThunderNetworkRaD/mit.db.git"
|
||||
},
|
||||
"author": "Thunder Network Development | Killer Boss Original",
|
||||
"author": "ThunderNetworkRaD | Killer Boss Original",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/ThunderNetworkRaD/mit.db/issues"
|
||||
|
@ -20,6 +20,6 @@
|
|||
"devDependencies": {
|
||||
"@types/node": "^18.14.0",
|
||||
"tslib": "^2.4.1",
|
||||
"typescript": "^4.9.5"
|
||||
"typescript": "^5.0.3"
|
||||
}
|
||||
}
|
||||
|
|
23
src/index.ts
23
src/index.ts
|
@ -4,6 +4,11 @@ import * as fs from 'fs';
|
|||
const writeDB = promisify(fs.writeFile);
|
||||
|
||||
class MitDB {
|
||||
readonly db;
|
||||
filename: string;
|
||||
options: any;
|
||||
dirname: string;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @param filename If not set, MapDB will only use internal memory
|
||||
|
@ -11,16 +16,18 @@ class MitDB {
|
|||
* @param options Options to pass in the constructor
|
||||
* @param options.dirname where to put the database?
|
||||
*/
|
||||
constructor(fn: string, options?: { dirname: string }) {
|
||||
constructor(filename: string, options?: { dirname: string }) {
|
||||
if (options && options.dirname) {
|
||||
this.dirname = options.dirname;
|
||||
} else {
|
||||
this.dirname = 'data';
|
||||
}
|
||||
|
||||
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
|
||||
this.filename = filename;
|
||||
|
||||
this.db = `./${dirname}/${filename}`
|
||||
if (!fs.existsSync(this.dirname)) fs.mkdirSync(this.dirname);
|
||||
|
||||
this.db = `./${this.dirname}/${this.filename}`;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,7 +41,7 @@ class MitDB {
|
|||
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(this.db, JSON.stringify(data));
|
||||
|
@ -94,13 +101,13 @@ class MitDB {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param callbackfn
|
||||
* @param callbackfilename
|
||||
*/
|
||||
forEach(callback: (value: any, key: any, map: Map<any, any>) => void) {
|
||||
forEach(callback: (value: any, key: any) => void) {
|
||||
const file = fs.readFileSync(this.db);
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,8 +126,6 @@ class MitDB {
|
|||
await writeDB(this.db, JSON.stringify(data));
|
||||
|
||||
return true;
|
||||
} else if (!map) {
|
||||
return false;
|
||||
}
|
||||
} catch {}
|
||||
return 'error';
|
||||
|
|
Loading…
Reference in a new issue