Compare commits
24 commits
v2023.02.0
...
main
Author | SHA1 | Date | |
---|---|---|---|
|
c902d92711 | ||
|
90670ebb7b | ||
|
363492aef5 | ||
|
41d589ccbc | ||
|
74f4f42e50 | ||
|
492c49a086 | ||
|
c8410b0c6f | ||
|
387ab7e582 | ||
|
e74533f1f2 | ||
|
e269f2c9d3 | ||
|
693bc03318 | ||
|
00f21b7d83 | ||
|
0d1a44eb55 | ||
234ef511fb | |||
|
e0f80728f1 | ||
|
4cc7c199c5 | ||
|
bd56815a45 | ||
bc1a6f1c43 | |||
|
6680c57b56 | ||
|
6b52d65288 | ||
678be1577f | |||
356cd177ec | |||
306b94feb5 | |||
e8a9d741dd |
9 changed files with 220 additions and 145 deletions
11
.github/dependabot.yml
vendored
Normal file
11
.github/dependabot.yml
vendored
Normal 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"
|
6
.github/workflows/npm-publish.yml
vendored
6
.github/workflows/npm-publish.yml
vendored
|
@ -4,7 +4,7 @@ on:
|
||||||
types: [created]
|
types: [created]
|
||||||
jobs:
|
jobs:
|
||||||
Publish-NPM:
|
Publish-NPM:
|
||||||
runs-on: ubuntu-latest
|
runs-on: node1
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
@ -14,6 +14,10 @@ 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 Dependencies
|
||||||
|
run: npm i
|
||||||
|
- name: Build
|
||||||
|
run: npm run build
|
||||||
- name: Publish package on NPM 📦
|
- name: Publish package on NPM 📦
|
||||||
run: npm publish --access public
|
run: npm publish --access public
|
||||||
env:
|
env:
|
||||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
|
build
|
13
SECURITY.md
Normal file
13
SECURITY.md
Normal 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.
|
18
package.json
18
package.json
|
@ -1,25 +1,25 @@
|
||||||
{
|
{
|
||||||
"name": "mit.db",
|
"name": "mit.db",
|
||||||
"version": "2023.02.05",
|
"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",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"install": "tsc"
|
"build": "tsc"
|
||||||
},
|
},
|
||||||
"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",
|
"homepage": "https://github.com/ThunderNetworkRaD/mit.db#readme",
|
||||||
"dependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.11.18",
|
"@types/node": "^18.14.0",
|
||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
"typescript": "^4.9.5"
|
"typescript": "^5.0.3"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
96
readme.md
96
readme.md
|
@ -1,35 +1,14 @@
|
||||||
# Map.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 `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
|
```js
|
||||||
const { MapDB } = require('quickmap.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 }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -40,24 +19,79 @@ async function sample() {
|
||||||
|
|
||||||
With **npm**:
|
With **npm**:
|
||||||
|
|
||||||
`npm i quickmap.db`
|
`npm i mit.db`
|
||||||
|
|
||||||
|
|
||||||
#### Setup
|
#### Setup
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const { MapDB } = require('quickmap.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
|
||||||
```
|
```
|
|
@ -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
|
|
||||||
```
|
|
148
src/index.ts
148
src/index.ts
|
@ -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 MapDB {
|
|
||||||
/**
|
/**
|
||||||
* @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 key
|
||||||
* @param value
|
* @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;
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
/* Modules */
|
/* Modules */
|
||||||
"module": "CommonJS", /* Specify what module code is generated. */
|
"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. */
|
"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. */
|
// "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. */
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||||
|
|
Loading…
Reference in a new issue