Compare commits
No commits in common. "main" and "v2023.02.05-1" have entirely different histories.
main
...
v2023.02.0
9 changed files with 142 additions and 218 deletions
11
.github/dependabot.yml
vendored
11
.github/dependabot.yml
vendored
|
@ -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"
|
|
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: node1
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v2
|
uses: actions/checkout@v2
|
||||||
|
@ -14,10 +14,6 @@ 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:
|
||||||
|
|
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,2 @@
|
||||||
node_modules
|
node_modules
|
||||||
package-lock.json
|
package-lock.json
|
||||||
build
|
|
13
SECURITY.md
13
SECURITY.md
|
@ -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.
|
|
18
package.json
18
package.json
|
@ -1,25 +1,25 @@
|
||||||
{
|
{
|
||||||
"name": "mit.db",
|
"name": "mit.db",
|
||||||
"version": "2023.04.12",
|
"version": "2023.02.05-1",
|
||||||
"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": {
|
||||||
"build": "tsc"
|
"install": "tsc"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"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",
|
"license": "ISC",
|
||||||
"bugs": {
|
"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",
|
"homepage": "https://github.com/ThunderNetworkRaD/map.db#readme",
|
||||||
"devDependencies": {
|
"dependencies": {
|
||||||
"@types/node": "^18.14.0",
|
"@types/node": "^18.11.18",
|
||||||
"tslib": "^2.4.1",
|
"tslib": "^2.4.1",
|
||||||
"typescript": "^5.0.3"
|
"typescript": "^4.9.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
91
readme.md
91
readme.md
|
@ -1,14 +1,34 @@
|
||||||
# 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 MitDB = require('mit.db');
|
const { MapDB } = require('mit.db');
|
||||||
const db = new MitDB('file.db'); // this is the save file's name + extension
|
const mapdb = new MapDB('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 = db.get('somekey');
|
const data = mapdb.get('somekey');
|
||||||
// reassigning the 'cool' property a new value
|
// reassigning the 'cool' property a new value
|
||||||
data.cool = true;
|
data.cool = true;
|
||||||
await db.set('somekey', data);
|
await mapdb.set('somekey', data);
|
||||||
// now 'somekey' has a new value { cool: true }
|
// now 'somekey' has a new value { cool: true }
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -25,73 +45,18 @@ With **npm**:
|
||||||
#### Setup
|
#### Setup
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const MitDB = require('mit.db')
|
const { MapDB } = require('mit.db')
|
||||||
const db = new MitDB('database.json') // this is the save file's name + extension
|
const db = new MapDB('database.json') // this is the save file's name + extension
|
||||||
```
|
```
|
||||||
|
|
||||||
#### set()
|
#### set()
|
||||||
|
|
||||||
```js
|
```js
|
||||||
await db.set('ciao', 'hello')
|
await db.set('what', 'how')
|
||||||
await db.set('arrivederci', 'bye')
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### get()
|
#### get()
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var ansa = db.get('ciao') // ansa = hello
|
var answ = db.get('what') // answ = how
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 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
|
|
||||||
```
|
|
62
readmeold.txt
Normal file
62
readmeold.txt
Normal 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
|
||||||
|
```
|
154
src/index.ts
154
src/index.ts
|
@ -1,146 +1,72 @@
|
||||||
import { promisify } from 'util';
|
import fsp from "fs/promises";
|
||||||
import * as fs from 'fs';
|
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 {
|
writeDB = fsp.writeFile;
|
||||||
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 in the constructor
|
* @param options Options to pass to the constructor
|
||||||
* @param options.dirname where to put the database?
|
* @param options.dirname
|
||||||
|
* @example 'data'
|
||||||
*/
|
*/
|
||||||
constructor(filename: string, options?: { dirname: string }) {
|
constructor(fn: string | undefined, options: any) {
|
||||||
if (options && options.dirname) {
|
map = new Map();
|
||||||
this.dirname = options.dirname;
|
|
||||||
} else {
|
|
||||||
this.dirname = 'data';
|
|
||||||
}
|
|
||||||
|
|
||||||
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 key
|
* @param value
|
||||||
* @param value
|
|
||||||
*/
|
*/
|
||||||
async set(key: string | number, value: any) {
|
async set(key: string | number, value: any) {
|
||||||
try {
|
try {
|
||||||
const file = fs.readFileSync(this.db);
|
file = fs.readFileSync(db);
|
||||||
const data: any[] = JSON.parse(file.toString());
|
data = JSON.parse(data);
|
||||||
|
|
||||||
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(this.db, JSON.stringify(data));
|
await writeDB(db, JSON.stringify(data));
|
||||||
return data;
|
|
||||||
} catch {
|
} catch {
|
||||||
await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
|
await await writeDB(db, `[${JSON.stringify({ key, value })}]`)
|
||||||
return JSON.parse(fs.readFileSync(this.db).toString());
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'error'
|
return map.set(key, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param key
|
* @param key
|
||||||
*/
|
*/
|
||||||
|
|
||||||
get(key: string | number) {
|
get(key: string | number) {
|
||||||
const file = fs.readFileSync(this.db);
|
if (map) {
|
||||||
const data: any[] = JSON.parse(file.toString());
|
return map.get(key)
|
||||||
|
} 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