@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 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
- ### Differences
11
- Although this module works in fact the same way as a Map, there are still some little differences between them, which are listed below:
12
- > - `MapDB#set()` and `MapDB#delete()` return **promises**
13
- > - `Map#size` in map.db is a **method** (`MapDB#size()`)
14
- > - There is an additional method, `MapDB#deleteFile()`, which deletes the save file and clears the Map (returns a promise)
15
- > - 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**.
16
-
17
- > Example:
18
-
19
- ```js
20
- const MapDB = require('@galaxy05/map.db');
21
- const mapdb = new MapDB('file.db'); // this is the save file's name + extension
22
-
23
- async function sample() {
24
- // assuming 'somekey' exists in the Map and has a value { cool: false }
25
- const data = mapdb.get('somekey');
26
- // reassigning the 'cool' property a new value
27
- data.cool = true;
28
-
29
- await mapdb.set('somekey', data);
30
- // now 'somekey' has a new value { cool: true }
31
- }
32
- ```
33
-
34
- ### Installation
35
- With **npm**:
36
-
37
- `npm i @galaxy05/map.db`
38
-
39
- With **yarn**:
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
+ ![image](img.png)
13
+
14
+ ![image](img2.png)
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
- const util_1 = require("util");
3
- const fs = require("fs");
4
- const writeDB = (0, util_1.promisify)(fs.writeFile);
5
- const deleteDB = (0, util_1.promisify)(fs.unlink);
6
- class MapDB {
7
- map;
8
- filename;
9
- db;
10
- /**
11
- * @constructor
12
- * @param filename If not set, MapDB will work just like the built-in Map that only stores data in internal memory
13
- * @example 'file.db'
14
- */
15
- constructor(filename) {
16
- this.map = new Map();
17
- this.filename = filename;
18
- if (!fs.existsSync('./data/'))
19
- fs.mkdirSync('./data');
20
- this.db = this.filename ? `./data/${this.filename}` : null;
21
- try {
22
- const file = fs.readFileSync(this.db);
23
- const data = JSON.parse(file.toString());
24
- for (let i = 0; i < data.length; i++) {
25
- this.map.set(data[i].key, data[i].value);
26
- }
27
- }
28
- catch { }
29
- }
30
- /**
31
- *
32
- * @param key
33
- * @param value
34
- */
35
- async set(key, value) {
36
- if (typeof key !== 'string' && typeof key !== 'number') {
37
- throw new TypeError('key must be of type string or number');
38
- }
39
- try {
40
- const file = fs.readFileSync(this.db);
41
- const data = JSON.parse(file.toString());
42
- const i = data.findIndex(pair => pair.key == key);
43
- !data[i] ? data.push({ key, value }) : data[i] = { key, value };
44
- await writeDB(this.db, JSON.stringify(data));
45
- }
46
- catch {
47
- await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).catch(() => { });
48
- }
49
- return this.map.set(key, value);
50
- }
51
- /**
52
- *
53
- * @param key
54
- */
55
- get(key) {
56
- return this.map.get(key);
57
- }
58
- /**
59
- *
60
- * @param key
61
- */
62
- has(key) {
63
- return this.map.has(key);
64
- }
65
- entries() {
66
- return this.map.entries();
67
- }
68
- keys() {
69
- return this.map.keys();
70
- }
71
- values() {
72
- return this.map.values();
73
- }
74
- /**
75
- *
76
- * @param callbackfn
77
- */
78
- forEach(callbackfn) {
79
- this.map.forEach(callbackfn);
80
- }
81
- /**
82
- *
83
- * @param key
84
- */
85
- async delete(key) {
86
- try {
87
- const file = fs.readFileSync(this.db);
88
- const data = JSON.parse(file.toString());
89
- const i = data.findIndex(pair => pair.key == key);
90
- if (data[i]) {
91
- data.splice(i, 1);
92
- await writeDB(this.db, JSON.stringify(data));
93
- }
94
- }
95
- catch { }
96
- return this.map.delete(key);
97
- }
98
- async clear() {
99
- await writeDB(this.db, JSON.stringify([])).catch(() => { });
100
- this.map.clear();
101
- }
102
- size() {
103
- return this.map.size;
104
- }
105
- /**
106
- * Deletes the db file and clears the Map
107
- */
108
- async deleteFile() {
109
- await deleteDB(this.db).catch(() => { });
110
- this.map.clear();
111
- return undefined;
112
- }
113
- }
114
- module.exports = MapDB;
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.1.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
- "test": "echo \"Error: no test specified\" && exit 1"
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 work just like the built-in Map that only stores data in internal memory
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
- this.map = new Map();
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
- try {
26
- const file = fs.readFileSync(this.db);
27
- const data: any[] = JSON.parse(file.toString());
28
-
29
- for (let i = 0; i < data.length; i++) {
30
- this.map.set(data[i].key, data[i].value);
31
- }
32
- } catch {}
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
- try {
46
- const file = fs.readFileSync(this.db);
47
- const data: any[] = JSON.parse(file.toString());
48
-
49
- const i = data.findIndex(pair => pair.key == key);
50
-
51
- !data[i] ? data.push({ key, value }) : data[i] = { key, value };
52
-
53
- await writeDB(this.db, JSON.stringify(data));
54
- } catch {
55
- await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).catch(() => {});
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
- return this.map.get(key);
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
- return this.map.has(key);
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
- return this.map.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
+ }
80
113
  }
81
114
 
82
115
  keys() {
83
- return this.map.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
+ }
84
124
  }
85
125
 
86
126
  values() {
87
- return this.map.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
+ }
88
135
  }
89
136
 
90
137
  /**
91
138
  *
92
139
  * @param callbackfn
93
140
  */
94
- forEach(callbackfn: (value: any, key: any, map: Map<any, any>) => void) {
95
- this.map.forEach(callbackfn);
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
- try {
104
- const file = fs.readFileSync(this.db);
105
- const data: any[] = JSON.parse(file.toString());
106
-
107
- const i = data.findIndex(pair => pair.key == key);
108
-
109
- if (data[i]) {
110
- data.splice(i, 1);
111
- await writeDB(this.db, JSON.stringify(data));
112
- }
113
- } catch {}
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
- return this.map.delete(key);
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.clear();
183
+ if (this.map) {
184
+ this.map.clear();
185
+ }
122
186
  }
123
187
 
124
188
  size() {
125
- return this.map.size;
126
- }
127
-
128
- /**
129
- * Deletes the db file and clears the Map
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
- return undefined;
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
- "target": "ESNext",
5
- "noImplicitAny": true,
6
- "declaration": true,
7
- "watch": true,
8
- "outDir": "build",
9
- "declarationDir": "types"
10
- },
11
- "include": [
12
- "src/**/*"
13
- ],
14
- "exclude": [
15
- "node_modules"
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
- * @constructor
7
- * @param filename If not set, MapDB will work just like the built-in Map that only stores data in internal memory
8
- * @example 'file.db'
9
- */
10
- constructor(filename?: string);
11
- /**
12
- *
13
- * @param key
14
- * @param value
15
- */
16
- set(key: string | number, value: any): Promise<Map<any, any>>;
17
- /**
18
- *
19
- * @param key
20
- */
21
- get(key: string | number): any;
22
- /**
23
- *
24
- * @param key
25
- */
26
- has(key: string | number): boolean;
27
- entries(): IterableIterator<[any, any]>;
28
- keys(): IterableIterator<any>;
29
- values(): IterableIterator<any>;
30
- /**
31
- *
32
- * @param callbackfn
33
- */
34
- forEach(callbackfn: (value: any, key: any, map: Map<any, any>) => void): void;
35
- /**
36
- *
37
- * @param key
38
- */
39
- delete(key: string | number): Promise<boolean>;
40
- clear(): Promise<void>;
41
- size(): number;
42
- /**
43
- * Deletes the db file and clears the Map
44
- */
45
- deleteFile(): Promise<undefined>;
46
- }
47
- export = MapDB;
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;