@galaxy05/map.db 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +48 -40
- package/build/index.js +203 -0
- package/img.png +0 -0
- package/img2.png +0 -0
- package/package.json +14 -4
- package/src/index.ts +200 -0
- package/tsconfig.json +19 -0
- package/types/index.d.ts +48 -0
- package/index.d.ts +0 -46
- package/index.js +0 -140
package/README.md
CHANGED
|
@@ -1,41 +1,49 @@
|
|
|
1
|
-
# Map.db
|
|
2
|
-
|
|
3
|
-
###### A Map that stores data locally and loads it at startup
|
|
4
|
-
|
|
5
|
-
### How does it work?
|
|
6
|
-
Map.db works just like the
|
|
7
|
-
|
|
8
|
-
The purpose of this module is to make the
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
# Map.db
|
|
2
|
+
|
|
3
|
+
###### A Map that stores data locally and loads it at startup. Written in TypeScript
|
|
4
|
+
|
|
5
|
+
### How does it work?
|
|
6
|
+
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**.
|
|
7
|
+
|
|
8
|
+
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.
|
|
9
|
+
|
|
10
|
+
The file structure is easily accessible and the data is stored in JSON format, allowing manual editing
|
|
11
|
+
|
|
12
|
+

|
|
13
|
+
|
|
14
|
+

|
|
15
|
+
|
|
16
|
+
You also have the option to only use local storage without touching internal memory
|
|
17
|
+
|
|
18
|
+
### Differences
|
|
19
|
+
Although this module works in fact the same way as a Map, there are still some little differences between them, which are listed below:
|
|
20
|
+
> - `MapDB#set()` and `MapDB#delete()` return **promises**
|
|
21
|
+
> - `Map#size` in map.db is a **method** (`MapDB#size()`)
|
|
22
|
+
> - 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**.
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
> Example:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const MapDB = require('@galaxy05/map.db');
|
|
29
|
+
const mapdb = new MapDB('file.db'); // this is the save file's name + extension
|
|
30
|
+
|
|
31
|
+
async function sample() {
|
|
32
|
+
// assuming 'somekey' exists in the Map and has a value { cool: false }
|
|
33
|
+
const data = mapdb.get('somekey');
|
|
34
|
+
// reassigning the 'cool' property a new value
|
|
35
|
+
data.cool = true;
|
|
36
|
+
|
|
37
|
+
await mapdb.set('somekey', data);
|
|
38
|
+
// now 'somekey' has a new value { cool: true }
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Installation
|
|
43
|
+
With **npm**:
|
|
44
|
+
|
|
45
|
+
`npm i @galaxy05/map.db`
|
|
46
|
+
|
|
47
|
+
With **yarn**:
|
|
48
|
+
|
|
41
49
|
`yarn add @galaxy05/map.db`
|
package/build/index.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
const util_1 = require("util");
|
|
26
|
+
const fs = __importStar(require("fs"));
|
|
27
|
+
const writeDB = (0, util_1.promisify)(fs.writeFile);
|
|
28
|
+
const deleteDB = (0, util_1.promisify)(fs.unlink);
|
|
29
|
+
class MapDB {
|
|
30
|
+
map;
|
|
31
|
+
filename;
|
|
32
|
+
db;
|
|
33
|
+
options;
|
|
34
|
+
/**
|
|
35
|
+
* @constructor
|
|
36
|
+
* @param filename If not set, MapDB will only use internal memory
|
|
37
|
+
* @example 'file.db'
|
|
38
|
+
* @param options Options to pass in the constructor
|
|
39
|
+
* @param options.localOnly When enabled, MapDB will only use local storage, without touching internal memory (requires a filename)
|
|
40
|
+
*/
|
|
41
|
+
constructor(filename, options) {
|
|
42
|
+
if (!filename && options?.localOnly)
|
|
43
|
+
throw new Error('Cannot use local storage without a filename');
|
|
44
|
+
this.map = !options?.localOnly ? new Map() : null;
|
|
45
|
+
this.filename = filename;
|
|
46
|
+
if (!fs.existsSync('./data/'))
|
|
47
|
+
fs.mkdirSync('./data');
|
|
48
|
+
this.db = this.filename ? `./data/${this.filename}` : null;
|
|
49
|
+
if (this.map && this.db) {
|
|
50
|
+
try {
|
|
51
|
+
const file = fs.readFileSync(this.db);
|
|
52
|
+
const data = JSON.parse(file.toString());
|
|
53
|
+
for (let i = 0; i < data.length; i++) {
|
|
54
|
+
this.map.set(data[i].key, data[i].value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch { }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
* @param key
|
|
63
|
+
* @param value
|
|
64
|
+
*/
|
|
65
|
+
async set(key, value) {
|
|
66
|
+
if (typeof key !== 'string' && typeof key !== 'number') {
|
|
67
|
+
throw new TypeError('key must be of type string or number');
|
|
68
|
+
}
|
|
69
|
+
if (this.db) {
|
|
70
|
+
try {
|
|
71
|
+
const file = fs.readFileSync(this.db);
|
|
72
|
+
const data = JSON.parse(file.toString());
|
|
73
|
+
const i = data.findIndex(pair => pair.key == key);
|
|
74
|
+
!data[i] ? data.push({ key, value }) : data[i] = { key, value };
|
|
75
|
+
await writeDB(this.db, JSON.stringify(data));
|
|
76
|
+
if (!this.map)
|
|
77
|
+
return data;
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
|
|
81
|
+
if (!this.map)
|
|
82
|
+
return JSON.parse(fs.readFileSync(this.db).toString());
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return this.map.set(key, value);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
*
|
|
90
|
+
* @param key
|
|
91
|
+
*/
|
|
92
|
+
get(key) {
|
|
93
|
+
if (this.map) {
|
|
94
|
+
return this.map.get(key);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
const file = fs.readFileSync(this.db);
|
|
98
|
+
const data = JSON.parse(file.toString());
|
|
99
|
+
return data.find(pair => pair.key == key)?.value || undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
*
|
|
104
|
+
* @param key
|
|
105
|
+
*/
|
|
106
|
+
has(key) {
|
|
107
|
+
if (this.map) {
|
|
108
|
+
return this.map.has(key);
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const file = fs.readFileSync(this.db);
|
|
112
|
+
const data = JSON.parse(file.toString());
|
|
113
|
+
return data.find(pair => pair.key == key) ? true : false;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
entries() {
|
|
117
|
+
if (this.map) {
|
|
118
|
+
return this.map.entries();
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const file = fs.readFileSync(this.db);
|
|
122
|
+
const data = JSON.parse(file.toString());
|
|
123
|
+
return data.map(pair => [pair.key, pair.value]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
keys() {
|
|
127
|
+
if (this.map) {
|
|
128
|
+
return this.map.keys();
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
const file = fs.readFileSync(this.db);
|
|
132
|
+
const data = JSON.parse(file.toString());
|
|
133
|
+
return data.map(pair => pair.key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
values() {
|
|
137
|
+
if (this.map) {
|
|
138
|
+
return this.map.values();
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
const file = fs.readFileSync(this.db);
|
|
142
|
+
const data = JSON.parse(file.toString());
|
|
143
|
+
return data.map(pair => pair.value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
*
|
|
148
|
+
* @param callbackfn
|
|
149
|
+
*/
|
|
150
|
+
forEach(callback) {
|
|
151
|
+
if (this.map) {
|
|
152
|
+
this.map.forEach(callback);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const file = fs.readFileSync(this.db);
|
|
156
|
+
const data = JSON.parse(file.toString());
|
|
157
|
+
data.forEach(pair => callback(pair.value, pair.key, this.map));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
*
|
|
162
|
+
* @param key
|
|
163
|
+
*/
|
|
164
|
+
async delete(key) {
|
|
165
|
+
if (this.db) {
|
|
166
|
+
try {
|
|
167
|
+
const file = fs.readFileSync(this.db);
|
|
168
|
+
const data = JSON.parse(file.toString());
|
|
169
|
+
const i = data.findIndex(pair => pair.key == key);
|
|
170
|
+
if (data[i]) {
|
|
171
|
+
data.splice(i, 1);
|
|
172
|
+
await writeDB(this.db, JSON.stringify(data));
|
|
173
|
+
if (!this.map)
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
else if (!this.map) {
|
|
177
|
+
return false;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
catch { }
|
|
181
|
+
}
|
|
182
|
+
if (this.map) {
|
|
183
|
+
return this.map.delete(key);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async clear() {
|
|
187
|
+
await writeDB(this.db, JSON.stringify([])).catch(() => { });
|
|
188
|
+
if (this.map) {
|
|
189
|
+
this.map.clear();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
size() {
|
|
193
|
+
if (this.map) {
|
|
194
|
+
return this.map.size;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const file = fs.readFileSync(this.db);
|
|
198
|
+
const data = JSON.parse(file.toString());
|
|
199
|
+
return data.length;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
module.exports = MapDB;
|
package/img.png
ADDED
|
Binary file
|
package/img2.png
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@galaxy05/map.db",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "A Map that stores data locally and loads it at startup",
|
|
5
|
-
"
|
|
5
|
+
"types": "types/index.d.ts",
|
|
6
|
+
"main": "build/index.js",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=12"
|
|
9
|
+
},
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
11
|
+
"build": "tsc"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
@@ -20,5 +24,11 @@
|
|
|
20
24
|
"bugs": {
|
|
21
25
|
"url": "https://github.com/GalaxyVinci05/map.db/issues"
|
|
22
26
|
},
|
|
23
|
-
"homepage": "https://github.com/GalaxyVinci05/map.db#readme"
|
|
27
|
+
"homepage": "https://github.com/GalaxyVinci05/map.db#readme",
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^16.7.10"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"typescript": "^4.4.2"
|
|
33
|
+
}
|
|
24
34
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { promisify } from 'util';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
|
|
4
|
+
const writeDB = promisify(fs.writeFile);
|
|
5
|
+
const deleteDB = promisify(fs.unlink);
|
|
6
|
+
|
|
7
|
+
class MapDB {
|
|
8
|
+
readonly map;
|
|
9
|
+
filename: string;
|
|
10
|
+
readonly db;
|
|
11
|
+
options: any;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @constructor
|
|
15
|
+
* @param filename If not set, MapDB will only use internal memory
|
|
16
|
+
* @example 'file.db'
|
|
17
|
+
* @param options Options to pass in the constructor
|
|
18
|
+
* @param options.localOnly When enabled, MapDB will only use local storage, without touching internal memory (requires a filename)
|
|
19
|
+
*/
|
|
20
|
+
constructor(filename?: string, options?: { localOnly: boolean }) {
|
|
21
|
+
if (!filename && options?.localOnly) throw new Error('Cannot use local storage without a filename');
|
|
22
|
+
|
|
23
|
+
this.map = !options?.localOnly ? new Map() : null;
|
|
24
|
+
this.filename = filename;
|
|
25
|
+
|
|
26
|
+
if (!fs.existsSync('./data/')) fs.mkdirSync('./data');
|
|
27
|
+
|
|
28
|
+
this.db = this.filename ? `./data/${this.filename}` : null;
|
|
29
|
+
|
|
30
|
+
if (this.map && this.db) {
|
|
31
|
+
try {
|
|
32
|
+
const file = fs.readFileSync(this.db);
|
|
33
|
+
const data: any[] = JSON.parse(file.toString());
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < data.length; i++) {
|
|
36
|
+
this.map.set(data[i].key, data[i].value);
|
|
37
|
+
}
|
|
38
|
+
} catch {}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param key
|
|
45
|
+
* @param value
|
|
46
|
+
*/
|
|
47
|
+
async set(key: string | number, value: any) {
|
|
48
|
+
if (typeof key !== 'string' && typeof key !== 'number') {
|
|
49
|
+
throw new TypeError('key must be of type string or number');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (this.db) {
|
|
53
|
+
try {
|
|
54
|
+
const file = fs.readFileSync(this.db);
|
|
55
|
+
const data: any[] = JSON.parse(file.toString());
|
|
56
|
+
|
|
57
|
+
const i = data.findIndex(pair => pair.key == key);
|
|
58
|
+
|
|
59
|
+
!data[i] ? data.push({ key, value }) : data[i] = { key, value };
|
|
60
|
+
|
|
61
|
+
await writeDB(this.db, JSON.stringify(data));
|
|
62
|
+
if (!this.map) return data;
|
|
63
|
+
} catch {
|
|
64
|
+
await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
|
|
65
|
+
if (!this.map) return JSON.parse(fs.readFileSync(this.db).toString());
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return this.map.set(key, value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
*
|
|
75
|
+
* @param key
|
|
76
|
+
*/
|
|
77
|
+
|
|
78
|
+
get(key: string | number) {
|
|
79
|
+
if (this.map) {
|
|
80
|
+
return this.map.get(key);
|
|
81
|
+
} else {
|
|
82
|
+
const file = fs.readFileSync(this.db);
|
|
83
|
+
const data: any[] = JSON.parse(file.toString());
|
|
84
|
+
|
|
85
|
+
return data.find(pair => pair.key == key)?.value || undefined;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
*
|
|
91
|
+
* @param key
|
|
92
|
+
*/
|
|
93
|
+
has(key: string | number) {
|
|
94
|
+
if (this.map) {
|
|
95
|
+
return this.map.has(key);
|
|
96
|
+
} else {
|
|
97
|
+
const file = fs.readFileSync(this.db);
|
|
98
|
+
const data: any[] = JSON.parse(file.toString());
|
|
99
|
+
|
|
100
|
+
return data.find(pair => pair.key == key) ? true : false;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
entries() {
|
|
105
|
+
if (this.map) {
|
|
106
|
+
return this.map.entries();
|
|
107
|
+
} else {
|
|
108
|
+
const file = fs.readFileSync(this.db);
|
|
109
|
+
const data: any[] = JSON.parse(file.toString());
|
|
110
|
+
|
|
111
|
+
return data.map(pair => [pair.key, pair.value]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
keys() {
|
|
116
|
+
if (this.map) {
|
|
117
|
+
return this.map.keys();
|
|
118
|
+
} else {
|
|
119
|
+
const file = fs.readFileSync(this.db);
|
|
120
|
+
const data: any[] = JSON.parse(file.toString());
|
|
121
|
+
|
|
122
|
+
return data.map(pair => pair.key);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
values() {
|
|
127
|
+
if (this.map) {
|
|
128
|
+
return this.map.values();
|
|
129
|
+
} else {
|
|
130
|
+
const file = fs.readFileSync(this.db);
|
|
131
|
+
const data: any[] = JSON.parse(file.toString());
|
|
132
|
+
|
|
133
|
+
return data.map(pair => pair.value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
* @param callbackfn
|
|
140
|
+
*/
|
|
141
|
+
forEach(callback: (value: any, key: any, map: Map<any, any>) => void) {
|
|
142
|
+
if (this.map) {
|
|
143
|
+
this.map.forEach(callback);
|
|
144
|
+
} else {
|
|
145
|
+
const file = fs.readFileSync(this.db);
|
|
146
|
+
const data: any[] = JSON.parse(file.toString());
|
|
147
|
+
|
|
148
|
+
data.forEach(pair => callback(pair.value, pair.key, this.map));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
*
|
|
154
|
+
* @param key
|
|
155
|
+
*/
|
|
156
|
+
async delete(key: string | number) {
|
|
157
|
+
if (this.db) {
|
|
158
|
+
try {
|
|
159
|
+
const file = fs.readFileSync(this.db);
|
|
160
|
+
const data: any[] = JSON.parse(file.toString());
|
|
161
|
+
|
|
162
|
+
const i = data.findIndex(pair => pair.key == key);
|
|
163
|
+
|
|
164
|
+
if (data[i]) {
|
|
165
|
+
data.splice(i, 1);
|
|
166
|
+
await writeDB(this.db, JSON.stringify(data));
|
|
167
|
+
|
|
168
|
+
if (!this.map) return true;
|
|
169
|
+
} else if (!this.map) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
} catch {}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (this.map) {
|
|
176
|
+
return this.map.delete(key);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async clear() {
|
|
181
|
+
await writeDB(this.db, JSON.stringify([])).catch(() => {});
|
|
182
|
+
|
|
183
|
+
if (this.map) {
|
|
184
|
+
this.map.clear();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
size() {
|
|
189
|
+
if (this.map) {
|
|
190
|
+
return this.map.size;
|
|
191
|
+
} else {
|
|
192
|
+
const file = fs.readFileSync(this.db);
|
|
193
|
+
const data: any[] = JSON.parse(file.toString());
|
|
194
|
+
|
|
195
|
+
return data.length;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export = MapDB;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "CommonJS",
|
|
4
|
+
"moduleResolution": "node",
|
|
5
|
+
"target": "ESNext",
|
|
6
|
+
"noImplicitAny": true,
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"watch": true,
|
|
9
|
+
"outDir": "build",
|
|
10
|
+
"declarationDir": "types",
|
|
11
|
+
"esModuleInterop": true
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"src/**/*"
|
|
15
|
+
],
|
|
16
|
+
"exclude": [
|
|
17
|
+
"node_modules"
|
|
18
|
+
]
|
|
19
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
declare class MapDB {
|
|
2
|
+
readonly map: Map<any, any>;
|
|
3
|
+
filename: string;
|
|
4
|
+
readonly db: string;
|
|
5
|
+
options: any;
|
|
6
|
+
/**
|
|
7
|
+
* @constructor
|
|
8
|
+
* @param filename If not set, MapDB will only use internal memory
|
|
9
|
+
* @example 'file.db'
|
|
10
|
+
* @param options Options to pass in the constructor
|
|
11
|
+
* @param options.localOnly When enabled, MapDB will only use local storage, without touching internal memory (requires a filename)
|
|
12
|
+
*/
|
|
13
|
+
constructor(filename?: string, options?: {
|
|
14
|
+
localOnly: boolean;
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
*
|
|
18
|
+
* @param key
|
|
19
|
+
* @param value
|
|
20
|
+
*/
|
|
21
|
+
set(key: string | number, value: any): Promise<any[] | Map<any, any>>;
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param key
|
|
25
|
+
*/
|
|
26
|
+
get(key: string | number): any;
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param key
|
|
30
|
+
*/
|
|
31
|
+
has(key: string | number): boolean;
|
|
32
|
+
entries(): IterableIterator<[any, any]> | any[][];
|
|
33
|
+
keys(): any[] | IterableIterator<any>;
|
|
34
|
+
values(): any[] | IterableIterator<any>;
|
|
35
|
+
/**
|
|
36
|
+
*
|
|
37
|
+
* @param callbackfn
|
|
38
|
+
*/
|
|
39
|
+
forEach(callback: (value: any, key: any, map: Map<any, any>) => void): void;
|
|
40
|
+
/**
|
|
41
|
+
*
|
|
42
|
+
* @param key
|
|
43
|
+
*/
|
|
44
|
+
delete(key: string | number): Promise<boolean>;
|
|
45
|
+
clear(): Promise<void>;
|
|
46
|
+
size(): number;
|
|
47
|
+
}
|
|
48
|
+
export = MapDB;
|
package/index.d.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
export = MapDB;
|
|
2
|
-
declare class MapDB {
|
|
3
|
-
/**
|
|
4
|
-
* @constructor
|
|
5
|
-
* @param {string} [filename] If not set, MapDB will work just like the built-in Map that only stores data in internal memory
|
|
6
|
-
* @example 'file.db'
|
|
7
|
-
*/
|
|
8
|
-
constructor(filename?: string);
|
|
9
|
-
filename: string;
|
|
10
|
-
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {string | number} key
|
|
13
|
-
* @param {*} value
|
|
14
|
-
*/
|
|
15
|
-
set(key: string | number, value: any): Promise<Map<any, any>>;
|
|
16
|
-
/**
|
|
17
|
-
*
|
|
18
|
-
* @param {string | number} key
|
|
19
|
-
*/
|
|
20
|
-
get(key: string | number): any;
|
|
21
|
-
/**
|
|
22
|
-
*
|
|
23
|
-
* @param {string | number} key
|
|
24
|
-
*/
|
|
25
|
-
has(key: string | number): boolean;
|
|
26
|
-
entries(): IterableIterator<[any, any]>;
|
|
27
|
-
keys(): IterableIterator<any>;
|
|
28
|
-
values(): IterableIterator<any>;
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
* @param {function} fn
|
|
32
|
-
*/
|
|
33
|
-
forEach(fn: Function): void;
|
|
34
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
* @param {string | number} key
|
|
37
|
-
*/
|
|
38
|
-
delete(key: string | number): Promise<boolean>;
|
|
39
|
-
clear(): Promise<void>;
|
|
40
|
-
size(): number;
|
|
41
|
-
/**
|
|
42
|
-
* Deletes the db file and clears the Map
|
|
43
|
-
*/
|
|
44
|
-
deleteFile(): Promise<any>;
|
|
45
|
-
#private;
|
|
46
|
-
}
|
package/index.js
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
const { promisify } = require('util');
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
|
|
4
|
-
const writeDB = promisify(fs.writeFile);
|
|
5
|
-
const deleteDB = promisify(fs.unlink);
|
|
6
|
-
|
|
7
|
-
module.exports = class MapDB {
|
|
8
|
-
#map;
|
|
9
|
-
filename;
|
|
10
|
-
#db;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @constructor
|
|
14
|
-
* @param {string} [filename] If not set, MapDB will work just like the built-in Map that only stores data in internal memory
|
|
15
|
-
* @example 'file.db'
|
|
16
|
-
*/
|
|
17
|
-
constructor(filename) {
|
|
18
|
-
this.#map = new Map();
|
|
19
|
-
this.filename = filename;
|
|
20
|
-
|
|
21
|
-
if (!fs.existsSync('./data/')) fs.mkdirSync('./data');
|
|
22
|
-
|
|
23
|
-
this.#db = this.filename ? `./data/${this.filename}` : null;
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
const file = fs.readFileSync(this.#db);
|
|
27
|
-
const data = JSON.parse(file);
|
|
28
|
-
|
|
29
|
-
for (let i = 0; i < data.length; i++) {
|
|
30
|
-
this.#map.set(data[i].key, data[i].value);
|
|
31
|
-
}
|
|
32
|
-
} catch {}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
*
|
|
37
|
-
* @param {string | number} key
|
|
38
|
-
* @param {*} value
|
|
39
|
-
*/
|
|
40
|
-
async set(key, value) {
|
|
41
|
-
if (typeof key !== 'string' && typeof key !== 'number') {
|
|
42
|
-
throw new TypeError('key must be of type string or number');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
console.log(value);
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
const file = fs.readFileSync(this.#db);
|
|
49
|
-
const data = JSON.parse(file);
|
|
50
|
-
|
|
51
|
-
const i = data.findIndex(pair => pair.key == key);
|
|
52
|
-
|
|
53
|
-
!data[i] ? data.push({ key, value }) : data[i] = { key, value };
|
|
54
|
-
|
|
55
|
-
await writeDB(this.#db, JSON.stringify(data));
|
|
56
|
-
} catch {
|
|
57
|
-
console.log('entered catch');
|
|
58
|
-
await writeDB(this.#db, `[${JSON.stringify({ key, value })}]`).catch(() => {});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return this.#map.set(key, value);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
*
|
|
66
|
-
* @param {string | number} key
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
get(key) {
|
|
70
|
-
return this.#map.get(key);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
*
|
|
75
|
-
* @param {string | number} key
|
|
76
|
-
*/
|
|
77
|
-
has(key) {
|
|
78
|
-
return this.#map.has(key);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
entries() {
|
|
82
|
-
return this.#map.entries();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
keys() {
|
|
86
|
-
return this.#map.keys();
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
values() {
|
|
90
|
-
return this.#map.values();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
*
|
|
95
|
-
* @param {function} fn
|
|
96
|
-
*/
|
|
97
|
-
forEach(fn) {
|
|
98
|
-
this.#map.forEach(fn);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
*
|
|
103
|
-
* @param {string | number} key
|
|
104
|
-
*/
|
|
105
|
-
async delete(key) {
|
|
106
|
-
try {
|
|
107
|
-
const file = fs.readFileSync(this.#db);
|
|
108
|
-
const data = JSON.parse(file);
|
|
109
|
-
|
|
110
|
-
const i = data.findIndex(pair => pair.key == key);
|
|
111
|
-
|
|
112
|
-
if (data[i]) {
|
|
113
|
-
data.splice(i, 1);
|
|
114
|
-
await writeDB(this.#db, JSON.stringify(data));
|
|
115
|
-
}
|
|
116
|
-
} catch {}
|
|
117
|
-
|
|
118
|
-
return this.#map.delete(key);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async clear() {
|
|
122
|
-
await writeDB(this.#db, JSON.stringify([])).catch(() => {});
|
|
123
|
-
|
|
124
|
-
this.#map.clear();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
size() {
|
|
128
|
-
return this.#map.size;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Deletes the db file and clears the Map
|
|
133
|
-
*/
|
|
134
|
-
async deleteFile() {
|
|
135
|
-
await deleteDB(this.#db).catch(() => {});
|
|
136
|
-
this.#map.clear();
|
|
137
|
-
|
|
138
|
-
return undefined;
|
|
139
|
-
}
|
|
140
|
-
}
|