@galaxy05/map.db 1.2.1 → 1.2.2

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/build/index.js ADDED
@@ -0,0 +1,213 @@
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 () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ const util_1 = require("util");
36
+ const fs = __importStar(require("fs"));
37
+ const writeDB = (0, util_1.promisify)(fs.writeFile);
38
+ const deleteDB = (0, util_1.promisify)(fs.unlink);
39
+ class MapDB {
40
+ map;
41
+ filename;
42
+ db;
43
+ options;
44
+ /**
45
+ * @constructor
46
+ * @param filename If not set, MapDB will only use internal memory
47
+ * @example 'file.db'
48
+ * @param options Options to pass in the constructor
49
+ * @param options.localOnly When enabled, MapDB will only use local storage, without touching internal memory (requires a filename)
50
+ */
51
+ constructor(filename, options) {
52
+ if (!filename && options?.localOnly)
53
+ throw new Error('Cannot use local storage without a filename');
54
+ this.map = !options?.localOnly ? new Map() : null;
55
+ this.filename = filename;
56
+ if (!fs.existsSync('./data/'))
57
+ fs.mkdirSync('./data');
58
+ this.db = this.filename ? `./data/${this.filename}` : null;
59
+ if (this.map && this.db) {
60
+ try {
61
+ const file = fs.readFileSync(this.db);
62
+ const data = JSON.parse(file.toString());
63
+ for (let i = 0; i < data.length; i++) {
64
+ this.map.set(data[i].key, data[i].value);
65
+ }
66
+ }
67
+ catch { }
68
+ }
69
+ }
70
+ /**
71
+ *
72
+ * @param key
73
+ * @param value
74
+ */
75
+ async set(key, value) {
76
+ if (typeof key !== 'string' && typeof key !== 'number') {
77
+ throw new TypeError('key must be of type string or number');
78
+ }
79
+ if (this.db) {
80
+ try {
81
+ const file = fs.readFileSync(this.db);
82
+ const data = JSON.parse(file.toString());
83
+ const i = data.findIndex(pair => pair.key == key);
84
+ !data[i] ? data.push({ key, value }) : data[i] = { key, value };
85
+ await writeDB(this.db, JSON.stringify(data));
86
+ if (!this.map)
87
+ return data;
88
+ }
89
+ catch {
90
+ await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
91
+ if (!this.map)
92
+ return JSON.parse(fs.readFileSync(this.db).toString());
93
+ });
94
+ }
95
+ }
96
+ return this.map.set(key, value);
97
+ }
98
+ /**
99
+ *
100
+ * @param key
101
+ */
102
+ get(key) {
103
+ if (this.map) {
104
+ return this.map.get(key);
105
+ }
106
+ else {
107
+ const file = fs.readFileSync(this.db);
108
+ const data = JSON.parse(file.toString());
109
+ return data.find(pair => pair.key == key)?.value || undefined;
110
+ }
111
+ }
112
+ /**
113
+ *
114
+ * @param key
115
+ */
116
+ has(key) {
117
+ if (this.map) {
118
+ return this.map.has(key);
119
+ }
120
+ else {
121
+ const file = fs.readFileSync(this.db);
122
+ const data = JSON.parse(file.toString());
123
+ return data.find(pair => pair.key == key) ? true : false;
124
+ }
125
+ }
126
+ entries() {
127
+ if (this.map) {
128
+ return this.map.entries();
129
+ }
130
+ else {
131
+ const file = fs.readFileSync(this.db);
132
+ const data = JSON.parse(file.toString());
133
+ return data.map(pair => [pair.key, pair.value]);
134
+ }
135
+ }
136
+ keys() {
137
+ if (this.map) {
138
+ return this.map.keys();
139
+ }
140
+ else {
141
+ const file = fs.readFileSync(this.db);
142
+ const data = JSON.parse(file.toString());
143
+ return data.map(pair => pair.key);
144
+ }
145
+ }
146
+ values() {
147
+ if (this.map) {
148
+ return this.map.values();
149
+ }
150
+ else {
151
+ const file = fs.readFileSync(this.db);
152
+ const data = JSON.parse(file.toString());
153
+ return data.map(pair => pair.value);
154
+ }
155
+ }
156
+ /**
157
+ *
158
+ * @param callbackfn
159
+ */
160
+ forEach(callback) {
161
+ if (this.map) {
162
+ this.map.forEach(callback);
163
+ }
164
+ else {
165
+ const file = fs.readFileSync(this.db);
166
+ const data = JSON.parse(file.toString());
167
+ data.forEach(pair => callback(pair.value, pair.key, this.map));
168
+ }
169
+ }
170
+ /**
171
+ *
172
+ * @param key
173
+ */
174
+ async delete(key) {
175
+ if (this.db) {
176
+ try {
177
+ const file = fs.readFileSync(this.db);
178
+ const data = JSON.parse(file.toString());
179
+ const i = data.findIndex(pair => pair.key == key);
180
+ if (data[i]) {
181
+ data.splice(i, 1);
182
+ await writeDB(this.db, JSON.stringify(data));
183
+ if (!this.map)
184
+ return true;
185
+ }
186
+ else if (!this.map) {
187
+ return false;
188
+ }
189
+ }
190
+ catch { }
191
+ }
192
+ if (this.map) {
193
+ return this.map.delete(key);
194
+ }
195
+ }
196
+ async clear() {
197
+ await writeDB(this.db, JSON.stringify([])).catch(() => { });
198
+ if (this.map) {
199
+ this.map.clear();
200
+ }
201
+ }
202
+ size() {
203
+ if (this.map) {
204
+ return this.map.size;
205
+ }
206
+ else {
207
+ const file = fs.readFileSync(this.db);
208
+ const data = JSON.parse(file.toString());
209
+ return data.length;
210
+ }
211
+ }
212
+ }
213
+ module.exports = MapDB;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@galaxy05/map.db",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "A Map that stores data locally and loads it at startup",
5
5
  "types": "types/index.d.ts",
6
6
  "main": "build/index.js",
package/tsconfig.json CHANGED
@@ -5,7 +5,6 @@
5
5
  "target": "ESNext",
6
6
  "noImplicitAny": true,
7
7
  "declaration": true,
8
- "watch": true,
9
8
  "outDir": "build",
10
9
  "declarationDir": "types",
11
10
  "esModuleInterop": true
@@ -16,4 +15,4 @@
16
15
  "exclude": [
17
16
  "node_modules"
18
17
  ]
19
- }
18
+ }
package/types/index.d.ts CHANGED
@@ -29,9 +29,9 @@ declare class MapDB {
29
29
  * @param key
30
30
  */
31
31
  has(key: string | number): boolean;
32
- entries(): IterableIterator<[any, any]> | any[][];
33
- keys(): any[] | IterableIterator<any>;
34
- values(): any[] | IterableIterator<any>;
32
+ entries(): MapIterator<[any, any]> | any[][];
33
+ keys(): any[] | MapIterator<any>;
34
+ values(): any[] | MapIterator<any>;
35
35
  /**
36
36
  *
37
37
  * @param callbackfn