@galaxy05/map.db 1.2.0 → 1.2.1

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/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@galaxy05/map.db",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
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",
7
7
  "engines": {
8
- "node": ">=12"
8
+ "node": ">=20"
9
9
  },
10
10
  "scripts": {
11
11
  "build": "tsc"
@@ -26,9 +26,9 @@
26
26
  },
27
27
  "homepage": "https://github.com/GalaxyVinci05/map.db#readme",
28
28
  "devDependencies": {
29
- "@types/node": "^16.7.10"
29
+ "@types/node": "^24.10.1"
30
30
  },
31
31
  "dependencies": {
32
- "typescript": "^4.4.2"
32
+ "typescript": "^5.9.3"
33
33
  }
34
34
  }
package/src/index.ts CHANGED
File without changes
package/types/index.d.ts CHANGED
File without changes
package/build/index.js DELETED
@@ -1,203 +0,0 @@
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;