@galaxy05/map.db 1.1.0 → 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 -114
- package/img.png +0 -0
- package/img2.png +0 -0
- package/package.json +34 -34
- package/src/index.ts +113 -52
- package/tsconfig.json +18 -16
- package/types/index.d.ts +48 -47
package/README.md
CHANGED
|
@@ -1,41 +1,49 @@
|
|
|
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
|
-
|
|
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
CHANGED
|
@@ -1,114 +1,203 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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,34 +1,34 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@galaxy05/map.db",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "A Map that stores data locally and loads it at startup",
|
|
5
|
-
"types": "types/index.d.ts",
|
|
6
|
-
"main": "build/index.js",
|
|
7
|
-
"engines": {
|
|
8
|
-
"node": ">=12"
|
|
9
|
-
},
|
|
10
|
-
"scripts": {
|
|
11
|
-
"
|
|
12
|
-
},
|
|
13
|
-
"repository": {
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/GalaxyVinci05/map.db.git"
|
|
16
|
-
},
|
|
17
|
-
"keywords": [
|
|
18
|
-
"Map",
|
|
19
|
-
"DB",
|
|
20
|
-
"MapDB"
|
|
21
|
-
],
|
|
22
|
-
"author": "GalaxyVinci05",
|
|
23
|
-
"license": "ISC",
|
|
24
|
-
"bugs": {
|
|
25
|
-
"url": "https://github.com/GalaxyVinci05/map.db/issues"
|
|
26
|
-
},
|
|
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
|
-
}
|
|
34
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@galaxy05/map.db",
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "A Map that stores data locally and loads it at startup",
|
|
5
|
+
"types": "types/index.d.ts",
|
|
6
|
+
"main": "build/index.js",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=12"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/GalaxyVinci05/map.db.git"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"Map",
|
|
19
|
+
"DB",
|
|
20
|
+
"MapDB"
|
|
21
|
+
],
|
|
22
|
+
"author": "GalaxyVinci05",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/GalaxyVinci05/map.db/issues"
|
|
26
|
+
},
|
|
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
|
+
}
|
|
34
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -8,28 +8,35 @@ class MapDB {
|
|
|
8
8
|
readonly map;
|
|
9
9
|
filename: string;
|
|
10
10
|
readonly db;
|
|
11
|
+
options: any;
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* @constructor
|
|
14
|
-
* @param filename If not set, MapDB will
|
|
15
|
+
* @param filename If not set, MapDB will only use internal memory
|
|
15
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)
|
|
16
19
|
*/
|
|
17
|
-
constructor(filename?: string) {
|
|
18
|
-
|
|
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;
|
|
19
24
|
this.filename = filename;
|
|
20
25
|
|
|
21
26
|
if (!fs.existsSync('./data/')) fs.mkdirSync('./data');
|
|
22
27
|
|
|
23
28
|
this.db = this.filename ? `./data/${this.filename}` : null;
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
/**
|
|
@@ -42,17 +49,22 @@ class MapDB {
|
|
|
42
49
|
throw new TypeError('key must be of type string or number');
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
+
}
|
|
56
68
|
}
|
|
57
69
|
|
|
58
70
|
return this.map.set(key, value);
|
|
@@ -64,7 +76,14 @@ class MapDB {
|
|
|
64
76
|
*/
|
|
65
77
|
|
|
66
78
|
get(key: string | number) {
|
|
67
|
-
|
|
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
|
+
}
|
|
68
87
|
}
|
|
69
88
|
|
|
70
89
|
/**
|
|
@@ -72,27 +91,62 @@ class MapDB {
|
|
|
72
91
|
* @param key
|
|
73
92
|
*/
|
|
74
93
|
has(key: string | number) {
|
|
75
|
-
|
|
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
|
+
}
|
|
76
102
|
}
|
|
77
103
|
|
|
78
104
|
entries() {
|
|
79
|
-
|
|
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
|
+
}
|
|
80
113
|
}
|
|
81
114
|
|
|
82
115
|
keys() {
|
|
83
|
-
|
|
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
|
+
}
|
|
84
124
|
}
|
|
85
125
|
|
|
86
126
|
values() {
|
|
87
|
-
|
|
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
|
+
}
|
|
88
135
|
}
|
|
89
136
|
|
|
90
137
|
/**
|
|
91
138
|
*
|
|
92
139
|
* @param callbackfn
|
|
93
140
|
*/
|
|
94
|
-
forEach(
|
|
95
|
-
this.map
|
|
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
|
+
}
|
|
96
150
|
}
|
|
97
151
|
|
|
98
152
|
/**
|
|
@@ -100,39 +154,46 @@ class MapDB {
|
|
|
100
154
|
* @param key
|
|
101
155
|
*/
|
|
102
156
|
async delete(key: string | number) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
data
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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
|
+
}
|
|
114
174
|
|
|
115
|
-
|
|
175
|
+
if (this.map) {
|
|
176
|
+
return this.map.delete(key);
|
|
177
|
+
}
|
|
116
178
|
}
|
|
117
179
|
|
|
118
180
|
async clear() {
|
|
119
181
|
await writeDB(this.db, JSON.stringify([])).catch(() => {});
|
|
120
182
|
|
|
121
|
-
this.map
|
|
183
|
+
if (this.map) {
|
|
184
|
+
this.map.clear();
|
|
185
|
+
}
|
|
122
186
|
}
|
|
123
187
|
|
|
124
188
|
size() {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
*/
|
|
131
|
-
async deleteFile(): Promise<undefined> {
|
|
132
|
-
await deleteDB(this.db).catch(() => {});
|
|
133
|
-
this.map.clear();
|
|
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());
|
|
134
194
|
|
|
135
|
-
|
|
195
|
+
return data.length;
|
|
196
|
+
}
|
|
136
197
|
}
|
|
137
198
|
}
|
|
138
199
|
|
package/tsconfig.json
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"module": "CommonJS",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
+
]
|
|
17
19
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,47 +1,48 @@
|
|
|
1
|
-
declare class MapDB {
|
|
2
|
-
readonly map: Map<any, any>;
|
|
3
|
-
filename: string;
|
|
4
|
-
readonly db: string;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
*
|
|
19
|
-
* @param
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
*
|
|
24
|
-
* @param key
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
*
|
|
37
|
-
* @param
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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;
|