Hello There #11

Merged
killerbossoriginal merged 1 commit from You-understimate-my-power into main 2023-02-06 10:34:53 +01:00
5 changed files with 181 additions and 62 deletions
Showing only changes of commit bc1a6f1c43 - Show all commits

1
data/file.json Normal file
View file

@ -0,0 +1 @@
[{"key":"ciao","value":"hello"},{"key":"arrivederci","value":"bye"}]

View file

@ -1,34 +1,14 @@
# Mit.db
MapDB A Map that stores data locally and loads it at startup. Written in JavaScript
### How does it work?
Map.db works just like the JavaScript built-in **Map**, with the same methods and functionalities, and in fact it uses itself a Map, but while the built-in Map only stores data in internal memory, this module **stores data locally in a file and loads it back in the Map at startup**.
The purpose of this module is to make the JavaScript built-in Map an actual **database**, and there comes the name `mit.db`: a Map that can be used as a database.
The file structure is easily accessible and the data is stored in JSON format, allowing manual editing
You also have the option to only use local storage without touching internal memory
### Differences
Although this module works in fact the same way as a Map, there are still some little differences between them, which are listed below:
> - `Mit#set()` and `Mit#delete()` return **promises**
> - When a value is reassigned to a key, it is only saved in the Map but not in the actual save file, so you always have to **set the key/value pair with the new value**.
> Example:
```js
const { MapDB } = require('mit.db');
const mapdb = new MapDB('file.db'); // this is the save file's name + extension
const MitDB = require('mit.db');
const db = new MitDB('file.db'); // this is the save file's name + extension
async function sample() {
// assuming 'somekey' exists in the Map and has a value { cool: false }
const data = mapdb.get('somekey');
const data = db.get('somekey');
// reassigning the 'cool' property a new value
data.cool = true;
await mapdb.set('somekey', data);
await db.set('somekey', data);
// now 'somekey' has a new value { cool: true }
}
```
@ -52,11 +32,66 @@ const db = new MapDB('database.json') // this is the save file's name + extensio
#### set()
```js
await db.set('what', 'how')
await db.set('ciao', 'hello')
await db.set('arrivederci', 'bye')
```
#### get()
```js
var answ = db.get('what') // answ = how
var ansa = db.get('ciao') // ansa = hello
```
#### has()
```js
var asnb = db.has('arrivederci') // ansb = true
```
#### entries()
```js
var ansc = db.entries() // ansc = [ 'ciao', 'hello' ], [ 'arrivederci', 'bye' ] ]
```
#### keys()
```js
var ansd = db.keys() // ansd = [ 'ciao', 'arrivederci' ]
```
#### values()
```js
var anse = db.values() // anse = [ 'hello', 'bye' ]
```
#### forEach()
```js
db.forEach((value, key) => console.log(value, key)) // console.log = hello ciao
// console.log = bye arrivederci
```
#### delete()
```js
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
await db.delete('ciao')
// [{"key":"arrivederci","value":"bye"}]
```
#### clear()
```js
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
await db.delete('ciao')
// []
```
#### size()
```js
// [{"key":"ciao","value":"hello"}, {"key":"arrivederci","value":"bye"}]
var ansf = db.size() // size = 2
```

View file

@ -1,72 +1,143 @@
import fsp from "fs/promises";
import fs from "fs";
import { promisify } from 'util';
import * as fs from 'fs';
let writeDB: any, map: any, filename: string | undefined = 'database.db', dirname: string = './data/', db: any, file: any, data: any;
const writeDB = promisify(fs.writeFile);
writeDB = fsp.writeFile;
let map: any, db: any, filename: string, dirname: string = 'data';
export class MitDB {
class MitDB {
/**
* @constructor
* @param filename If not set, MapDB will only use internal memory
* @example 'file.db'
* @param options Options to pass to the constructor
* @param options.dirname
* @example 'data'
* @param options Options to pass in the constructor
* @param options.dirname where to put the database?
*/
constructor(fn: string | undefined, options: any) {
constructor(fn?: string, options?: { dirname: string }) {
map = new Map();
if (fn) filename = fn;
if (options.dirname) dirname = options.dirname;
if (options && options.dirname) dirname = options.dirname;
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname);
db = `./${dirname}/${filename}`
try {
file = fs.readFileSync(db);
data = JSON.parse(file.toString());
for (let i = 0; i < data.length; i++) {
map.set(data[i].key, data[i].value);
}
} catch {}
}
/**
*
* @param key
* @param value
*/
async set(key: string | number, value: any) {
try {
file = fs.readFileSync(db);
data = JSON.parse(data);
const file = fs.readFileSync(db);
const data: any[] = JSON.parse(file.toString());
const i = data.findIndex((pair: any) => pair.key == key);
let i = data.findIndex((pair: any) => pair.key == key);
!data[i] ? data.push({ key, value }) : data[i] = { key, value };
await writeDB(db, JSON.stringify(data));
return data;
} catch {
await await writeDB(db, `[${JSON.stringify({ key, value })}]`)
await writeDB(db, `[${JSON.stringify({ key, value })}]`).then(() => {
return JSON.parse(fs.readFileSync(db).toString());
});
}
return map.set(key, value)
return 'error'
}
/**
*
* @param key
*/
get(key: string | number) {
if (map) {
return map.get(key)
} else {
file = fs.readFileSync(db);
data = JSON.parse(file.toString());
const file = fs.readFileSync(db);
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;
}
/**
*
* @param key
*/
has(key: string | number) {
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(db);
const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => [pair.key, pair.value]);
}
keys() {
const file = fs.readFileSync(db);
const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => pair.key);
}
values() {
const file = fs.readFileSync(db);
const data: any[] = JSON.parse(file.toString());
return data.map((pair: any) => pair.value);
}
/**
*
* @param callbackfn
*/
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, map));
}
/**
*
* @param key
*/
async delete(key: string | number) {
try {
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(db, JSON.stringify(data));
return true;
} else if (!map) {
return false;
}
} catch {}
return 'error';
}
async clear() {
await writeDB(db, JSON.stringify([])).catch(() => {});
}
size() {
const file = fs.readFileSync(db);
const data: any[] = JSON.parse(file.toString());
return data.length;
}
}
export = MitDB;

12
test.js Normal file
View file

@ -0,0 +1,12 @@
const MitDB = require('./build/index.js')
var db = new MitDB('file.json')
async function run () {
await db.set('ciao', 'hello')
await db.set('arrivederci', 'bye')
console.log(db.size())
}
run()

File diff suppressed because one or more lines are too long