Compare commits
No commits in common. "main" and "v2023.02.23" have entirely different histories.
main
...
v2023.02.2
2 changed files with 34 additions and 35 deletions
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "mit.db",
|
||||
"version": "2023.04.12",
|
||||
"version": "2023.02.23",
|
||||
"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": "ThunderNetworkRaD | Killer Boss Original",
|
||||
"author": "Thunder Network Development | 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": "^5.0.3"
|
||||
"typescript": "^4.9.5"
|
||||
}
|
||||
}
|
||||
|
|
63
src/index.ts
63
src/index.ts
|
@ -3,12 +3,9 @@ import * as fs from 'fs';
|
|||
|
||||
const writeDB = promisify(fs.writeFile);
|
||||
|
||||
class MitDB {
|
||||
readonly db;
|
||||
filename: string;
|
||||
options: any;
|
||||
dirname: string;
|
||||
let map: any, db: any, filename: string, dirname: string;
|
||||
|
||||
class MitDB {
|
||||
/**
|
||||
* @constructor
|
||||
* @param filename If not set, MapDB will only use internal memory
|
||||
|
@ -16,18 +13,18 @@ class MitDB {
|
|||
* @param options Options to pass in the constructor
|
||||
* @param options.dirname where to put the database?
|
||||
*/
|
||||
constructor(filename: string, options?: { dirname: string }) {
|
||||
if (options && options.dirname) {
|
||||
this.dirname = options.dirname;
|
||||
} else {
|
||||
this.dirname = 'data';
|
||||
}
|
||||
constructor(fn?: string, options?: { dirname: string }) {
|
||||
if (!dirname || dirname != 'data') dirname = 'data';
|
||||
|
||||
this.filename = filename;
|
||||
map = new Map();
|
||||
|
||||
if (!fs.existsSync(this.dirname)) fs.mkdirSync(this.dirname);
|
||||
if (fn) filename = fn;
|
||||
|
||||
this.db = `./${this.dirname}/${this.filename}`;
|
||||
if (options && options.dirname) dirname = options.dirname;
|
||||
|
||||
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
|
||||
|
||||
db = `./${dirname}/${filename}`
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,18 +34,18 @@ class MitDB {
|
|||
*/
|
||||
async set(key: string | number, value: any) {
|
||||
try {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(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(this.db, JSON.stringify(data));
|
||||
await writeDB(db, JSON.stringify(data));
|
||||
return data;
|
||||
} catch {
|
||||
await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
|
||||
return JSON.parse(fs.readFileSync(this.db).toString());
|
||||
await writeDB(db, `[${JSON.stringify({ key, value })}]`).then(() => {
|
||||
return JSON.parse(fs.readFileSync(db).toString());
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -61,7 +58,7 @@ class MitDB {
|
|||
*/
|
||||
|
||||
get(key: string | number) {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.find((pair: any) => pair.key == key)?.value || undefined;
|
||||
|
@ -72,28 +69,28 @@ class MitDB {
|
|||
* @param key
|
||||
*/
|
||||
has(key: string | number) {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.find((pair: any) => pair.key == key) ? true : false;
|
||||
}
|
||||
|
||||
entries() {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.map((pair: any) => [pair.key, pair.value]);
|
||||
}
|
||||
|
||||
keys() {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.map((pair: any) => pair.key);
|
||||
}
|
||||
|
||||
values() {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.map((pair: any) => pair.value);
|
||||
|
@ -101,13 +98,13 @@ class MitDB {
|
|||
|
||||
/**
|
||||
*
|
||||
* @param callbackfilename
|
||||
* @param callbackfn
|
||||
*/
|
||||
forEach(callback: (value: any, key: any) => void) {
|
||||
const file = fs.readFileSync(this.db);
|
||||
forEach(callback: (value: any, key: any, map: Map<any, any>) => void) {
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
data.forEach((pair: any) => callback(pair.value, pair.key));
|
||||
data.forEach((pair: any) => callback(pair.value, pair.key, map));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -116,27 +113,29 @@ class MitDB {
|
|||
*/
|
||||
async delete(key: string | number) {
|
||||
try {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(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(this.db, JSON.stringify(data));
|
||||
await writeDB(db, JSON.stringify(data));
|
||||
|
||||
return true;
|
||||
} else if (!map) {
|
||||
return false;
|
||||
}
|
||||
} catch {}
|
||||
return 'error';
|
||||
}
|
||||
|
||||
async clear() {
|
||||
await writeDB(this.db, JSON.stringify([])).catch(() => {});
|
||||
await writeDB(db, JSON.stringify([])).catch(() => {});
|
||||
}
|
||||
|
||||
size() {
|
||||
const file = fs.readFileSync(this.db);
|
||||
const file = fs.readFileSync(db);
|
||||
const data: any[] = JSON.parse(file.toString());
|
||||
|
||||
return data.length;
|
||||
|
|
Loading…
Reference in a new issue