Compare commits

...

24 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
356cd177ec 2023.02.05-2 2023-02-05 13:17:25 +01:00
306b94feb5 config 2023-02-05 13:06:21 +01:00
e8a9d741dd 2023.02.05-1 2023-02-05 12:20:17 +01:00
9 changed files with 220 additions and 145 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

@ -4,7 +4,7 @@ on:
types: [created]
jobs:
Publish-NPM:
runs-on: ubuntu-latest
runs-on: node1
steps:
- name: Checkout
uses: actions/checkout@v2
@ -14,6 +14,10 @@ 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,2 +1,3 @@
node_modules
package-lock.json
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,25 +1,25 @@
{
"name": "mit.db",
"version": "2023.02.05",
"version": "2023.04.12",
"description": "An easy and quick database",
"main": "build/index.js",
"types": "build/index.d.ts",
"scripts": {
"install": "tsc"
"build": "tsc"
},
"repository": {
"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",
"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": {
"@types/node": "^18.11.18",
"homepage": "https://github.com/ThunderNetworkRaD/mit.db#readme",
"devDependencies": {
"@types/node": "^18.14.0",
"tslib": "^2.4.1",
"typescript": "^4.9.5"
"typescript": "^5.0.3"
}
}

View file

@ -1,35 +1,14 @@
# 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:
# Mit.db
```js
const { MapDB } = require('quickmap.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 }
}
```
@ -40,24 +19,79 @@ async function sample() {
With **npm**:
`npm i quickmap.db`
`npm i mit.db`
#### Setup
```js
const { MapDB } = require('quickmap.db')
const db = new MapDB('database.json') // this is the save file's name + extension
const MitDB = require('mit.db')
const db = new MitDB('database.json') // this is the save file's name + extension
```
#### 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,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 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;
class MitDB {
readonly db;
filename: string;
options: any;
dirname: string;
export class MapDB {
/**
* @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) {
map = new Map();
if (fn) filename = fn;
if (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);
constructor(filename: string, options?: { dirname: string }) {
if (options && options.dirname) {
this.dirname = options.dirname;
} else {
this.dirname = 'data';
}
} catch {}
this.filename = filename;
if (!fs.existsSync(this.dirname)) fs.mkdirSync(this.dirname);
this.db = `./${this.dirname}/${this.filename}`;
}
/**
*
* @param key
* @param value
*/
async set(key: string | number, value: any) {
try {
file = fs.readFileSync(db);
data = JSON.parse(data);
const file = fs.readFileSync(this.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));
await writeDB(this.db, JSON.stringify(data));
return data;
} 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
*/
get(key: string | number) {
if (map) {
return map.get(key)
} else {
file = fs.readFileSync(db);
data = JSON.parse(file.toString());
const file = fs.readFileSync(this.db);
const data: any[] = 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;
}
}
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. */