Compare commits
14 commits
v2023.02.0
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
c902d92711 | ||
|
90670ebb7b | ||
|
363492aef5 | ||
|
41d589ccbc | ||
|
74f4f42e50 | ||
|
492c49a086 | ||
|
c8410b0c6f | ||
|
387ab7e582 | ||
|
e74533f1f2 | ||
|
e269f2c9d3 | ||
|
693bc03318 | ||
|
00f21b7d83 | ||
|
0d1a44eb55 | ||
234ef511fb |
3 changed files with 42 additions and 41 deletions
18
package.json
18
package.json
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "mit.db",
|
"name": "mit.db",
|
||||||
"version": "2023.02.06",
|
"version": "2023.04.12",
|
||||||
"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",
|
||||||
|
@ -9,19 +9,17 @@
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/ThunderNetworkRaD/map.db.git"
|
"url": "git+https://github.com/ThunderNetworkRaD/mit.db.git"
|
||||||
},
|
},
|
||||||
"author": "Thunder Network Development | Killer Boss Original",
|
"author": "ThunderNetworkRaD | Killer Boss Original",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"bugs": {
|
"bugs": {
|
||||||
"url": "https://github.com/ThunderNetworkRaD/map.db/issues"
|
"url": "https://github.com/ThunderNetworkRaD/mit.db/issues"
|
||||||
},
|
|
||||||
"homepage": "https://github.com/ThunderNetworkRaD/map.db#readme",
|
|
||||||
"dependencies": {
|
|
||||||
"tslib": "^2.4.1",
|
|
||||||
"typescript": "^4.9.5"
|
|
||||||
},
|
},
|
||||||
|
"homepage": "https://github.com/ThunderNetworkRaD/mit.db#readme",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.19"
|
"@types/node": "^18.14.0",
|
||||||
|
"tslib": "^2.4.1",
|
||||||
|
"typescript": "^5.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,8 +25,8 @@ With **npm**:
|
||||||
#### Setup
|
#### Setup
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { MapDB } = require('mit.db')
|
const MitDB = require('mit.db')
|
||||||
const db = new MapDB('database.json') // this is the save file's name + extension
|
const db = new MitDB('database.json') // this is the save file's name + extension
|
||||||
```
|
```
|
||||||
|
|
||||||
#### set()
|
#### set()
|
||||||
|
|
61
src/index.ts
61
src/index.ts
|
@ -3,9 +3,12 @@ import * as fs from 'fs';
|
||||||
|
|
||||||
const writeDB = promisify(fs.writeFile);
|
const writeDB = promisify(fs.writeFile);
|
||||||
|
|
||||||
let map: any, db: any, filename: string, dirname: string = 'data';
|
|
||||||
|
|
||||||
class MitDB {
|
class MitDB {
|
||||||
|
readonly db;
|
||||||
|
filename: string;
|
||||||
|
options: any;
|
||||||
|
dirname: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
* @param filename If not set, MapDB will only use internal memory
|
* @param filename If not set, MapDB will only use internal memory
|
||||||
|
@ -13,16 +16,18 @@ 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(filename: string, options?: { dirname: string }) {
|
||||||
map = new Map();
|
if (options && options.dirname) {
|
||||||
|
this.dirname = options.dirname;
|
||||||
|
} else {
|
||||||
|
this.dirname = 'data';
|
||||||
|
}
|
||||||
|
|
||||||
if (fn) filename = fn;
|
this.filename = filename;
|
||||||
|
|
||||||
if (options && options.dirname) dirname = options.dirname;
|
if (!fs.existsSync(this.dirname)) fs.mkdirSync(this.dirname);
|
||||||
|
|
||||||
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
|
this.db = `./${this.dirname}/${this.filename}`;
|
||||||
|
|
||||||
db = `./${dirname}/${filename}`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -32,18 +37,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());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +61,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;
|
||||||
|
@ -67,28 +72,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);
|
||||||
|
@ -96,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(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));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -111,29 +116,27 @@ 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) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
} catch {}
|
} catch {}
|
||||||
return 'error';
|
return 'error';
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
|
Loading…
Reference in a new issue