Compare commits

..

No commits in common. "main" and "v2023.02.05" have entirely different histories.

9 changed files with 145 additions and 220 deletions

View file

@ -1,11 +0,0 @@
# 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

@ -4,7 +4,7 @@ on:
types: [created]
jobs:
Publish-NPM:
runs-on: node1
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
@ -14,10 +14,6 @@ jobs:
node-version: '16.x'
registry-url: 'https://registry.npmjs.org'
scope: '@thundernetworkrad'
- name: Install Dependencies
run: npm i
- name: Build
run: npm run build
- name: Publish package on NPM 📦
run: npm publish --access public
env:

1
.gitignore vendored
View file

@ -1,3 +1,2 @@
node_modules
package-lock.json
build

View file

@ -1,13 +0,0 @@
# 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,25 +1,25 @@
{
"name": "mit.db",
"version": "2023.04.12",
"version": "2023.02.05",
"description": "An easy and quick database",
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
"build": "tsc"
"install": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ThunderNetworkRaD/mit.db.git"
"url": "git+https://github.com/ThunderNetworkRaD/map.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"
"url": "https://github.com/ThunderNetworkRaD/map.db/issues"
},
"homepage": "https://github.com/ThunderNetworkRaD/mit.db#readme",
"devDependencies": {
"@types/node": "^18.14.0",
"homepage": "https://github.com/ThunderNetworkRaD/map.db#readme",
"dependencies": {
"@types/node": "^18.11.18",
"tslib": "^2.4.1",
"typescript": "^5.0.3"
"typescript": "^4.9.5"
}
}

View file

@ -1,14 +1,35 @@
# Mit.db
# 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()` and `MapDB#delete()` return **promises**
> - `Map#size` in map.db is a **method** (`MapDB#size()`)
> - 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 MitDB = require('mit.db');
const db = new MitDB('file.db'); // this is the save file's name + extension
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 = db.get('somekey');
const data = mapdb.get('somekey');
// reassigning the 'cool' property a new value
data.cool = true;
await db.set('somekey', data);
await mapdb.set('somekey', data);
// now 'somekey' has a new value { cool: true }
}
```
@ -19,79 +40,24 @@ async function sample() {
With **npm**:
`npm i mit.db`
`npm i quickmap.db`
#### Setup
```js
const MitDB = require('mit.db')
const db = new MitDB('database.json') // this is the save file's name + extension
const { MapDB } = require('quickmap.db')
const db = new MapDB('database.json') // this is the save file's name + extension
```
#### set()
```js
await db.set('ciao', 'hello')
await db.set('arrivederci', 'bye')
await db.set('what', 'how')
```
#### get()
```js
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
var answ = db.get('what') // answ = how
```

62
readmeold.txt Normal file
View file

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

View file

@ -26,7 +26,7 @@
/* Modules */
"module": "CommonJS", /* Specify what module code is generated. */
"rootDir": "src", /* Specify the root folder within your source files. */
"rootDir": "./src/", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */