Compare commits

..

21 commits

Author SHA1 Message Date
Killer Boss Original
c902d92711
Fix 2023-04-12 18:42:15 +02:00
Killer Boss Original
90670ebb7b
. 2023-04-12 18:37:16 +02:00
Killer Boss Original
363492aef5
test 2023-04-12 18:29:31 +02:00
Killer Boss Original
41d589ccbc
Merge pull request #13 from ThunderNetworkRaD/dependabot/npm_and_yarn/typescript-5.0.3
Bump typescript from 4.9.5 to 5.0.3
2023-04-03 12:36:28 +02:00
dependabot[bot]
74f4f42e50
Bump typescript from 4.9.5 to 5.0.3
Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.9.5 to 5.0.3.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/commits)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-04-03 08:05:46 +00:00
Killer Boss Original
492c49a086
Update package.json 2023-02-23 12:26:39 +01:00
Killer Boss Original
c8410b0c6f
2023.02.23.4 2023-02-23 12:17:13 +01:00
Killer Boss Original
387ab7e582
2023.02.23.3 2023-02-23 12:15:27 +01:00
Killer Boss Original
e74533f1f2
ttf 2023-02-23 12:06:29 +01:00
Killer Boss Original
e269f2c9d3
ttf 2023-02-23 11:58:44 +01:00
Killer Boss Original
693bc03318 update package version 2023-02-23 10:50:14 +00:00
Killer Boss Original
00f21b7d83 Fix dirname error 2023-02-23 10:50:05 +00:00
Killer Boss Original
0d1a44eb55 fix readme 2023-02-23 10:46:26 +00:00
234ef511fb Updated Repo Url 2023-02-06 20:49:49 +01:00
Killer Boss Original
e0f80728f1
Update package.json 2023-02-06 10:38:39 +01:00
Killer Boss Original
4cc7c199c5
rfr 2023-02-06 10:37:32 +01:00
Killer Boss Original
bd56815a45
Merge pull request #11 from ThunderNetworkRaD/You-understimate-my-power
Hello There
2023-02-06 10:34:52 +01:00
bc1a6f1c43 Hello There 2023-02-06 10:15:19 +01:00
Killer Boss Original
6680c57b56
Create dependabot.yml 2023-02-05 22:15:23 +01:00
Killer Boss Original
6b52d65288
Create SECURITY.md 2023-02-05 22:14:20 +01:00
678be1577f fix npm publish 2023-02-05 13:19:24 +01:00
8 changed files with 210 additions and 142 deletions

11
.github/dependabot.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "npm" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"

View file

@ -14,7 +14,7 @@ jobs:
node-version: '16.x' node-version: '16.x'
registry-url: 'https://registry.npmjs.org' registry-url: 'https://registry.npmjs.org'
scope: '@thundernetworkrad' scope: '@thundernetworkrad'
- name: Install Dependency - name: Install Dependencies
run: npm i run: npm i
- name: Build - name: Build
run: npm run build run: npm run build

13
SECURITY.md Normal file
View file

@ -0,0 +1,13 @@
# Security Policy
## Supported Versions
| Version | Supported |
| ---------- | ------------------ |
| 2023.02.06 | ✅ |
| 2023.02.05 | ✅ |
| < 2023.02 | |
## Reporting a Vulnerability
For report a vulnerability pls go [Here](https://github.com/ThunderNetworkRaD/mit.db/issues), open a new issue.

View file

@ -1,6 +1,6 @@
{ {
"name": "mit.db", "name": "mit.db",
"version": "2023.02.05-2", "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"
} }
} }

View file

@ -1,34 +1,14 @@
# Mit.db # 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 ```js
const { MapDB } = require('mit.db'); const MitDB = require('mit.db');
const mapdb = new MapDB('file.db'); // this is the save file's name + extension const db = new MitDB('file.db'); // this is the save file's name + extension
async function sample() { async function sample() {
// assuming 'somekey' exists in the Map and has a value { cool: false } // 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 // reassigning the 'cool' property a new value
data.cool = true; data.cool = true;
await mapdb.set('somekey', data); await db.set('somekey', data);
// now 'somekey' has a new value { cool: true } // now 'somekey' has a new value { cool: true }
} }
``` ```
@ -45,18 +25,73 @@ 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()
```js ```js
await db.set('what', 'how') await db.set('ciao', 'hello')
await db.set('arrivederci', 'bye')
``` ```
#### get() #### get()
```js ```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,62 +0,0 @@
# Map.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 `map.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:
> - `MapDB#set()` 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('quickmap.db');
const mapdb = new MapDB('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');
// reassigning the 'cool' property a new value
data.cool = true;
await mapdb.set('somekey', data);
// now 'somekey' has a new value { cool: true }
}
```
### Docs
#### Installation
With **npm**:
`npm i quickmap.db`
#### Setup
```js
const { MapDB } = require('quickmap.db')
const db = new MapDB('database.json') // this is the save file's name + extension
```
#### set()
```js
await db.set('what', 'how')
```
#### get()
```js
var answ = db.get('what') // answ = how
```

View file

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

File diff suppressed because one or more lines are too long