@galaxy05/map.db 1.2.1 → 1.3.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
@@ -13,10 +13,10 @@ The file structure is easily accessible and the data is stored in JSON format, a
13
13
 
14
14
  ![image](img2.png)
15
15
 
16
- You also have the option to only use local storage without touching internal memory
16
+ You also have the option to only use local storage, disabling internal memory
17
17
 
18
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:
19
+ Although this module works in fact the same way as a Map, there are a few differences between them, which are listed below:
20
20
  > - `MapDB#set()` and `MapDB#delete()` return **promises**
21
21
  > - `Map#size` in map.db is a **method** (`MapDB#size()`)
22
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**.
@@ -46,4 +46,4 @@ With **npm**:
46
46
 
47
47
  With **yarn**:
48
48
 
49
- `yarn add @galaxy05/map.db`
49
+ `yarn add @galaxy05/map.db`
package/build/index.js ADDED
@@ -0,0 +1,216 @@
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 = promisify(fs.unlink);
39
+ class MapDB {
40
+ map;
41
+ filename;
42
+ db;
43
+ options;
44
+ path;
45
+ /**
46
+ * @constructor
47
+ * @param filename If not set, MapDB will only use internal memory
48
+ * @example 'file.db'
49
+ * @param options Options to pass in the constructor
50
+ * @param options.localOnly Disable internal memory
51
+ * @param options.path Optional existing path to save the MapDB data directory
52
+ */
53
+ constructor(filename, options) {
54
+ if (!filename && options?.localOnly)
55
+ throw new Error('Cannot use local storage without a filename');
56
+ this.map = !options?.localOnly ? new Map() : null;
57
+ this.filename = filename;
58
+ this.path = options.path ?? '.';
59
+ if (!fs.existsSync(`${this.path}/data/`))
60
+ fs.mkdirSync(`${this.path}/data`);
61
+ this.db = this.filename ? `${this.path}/data/${this.filename}` : null;
62
+ if (this.map && this.db) {
63
+ try {
64
+ const file = fs.readFileSync(this.db);
65
+ const data = JSON.parse(file.toString());
66
+ for (let i = 0; i < data.length; i++) {
67
+ this.map.set(data[i].key, data[i].value);
68
+ }
69
+ }
70
+ catch { }
71
+ }
72
+ }
73
+ /**
74
+ *
75
+ * @param key
76
+ * @param value
77
+ */
78
+ async set(key, value) {
79
+ if (typeof key !== 'string' && typeof key !== 'number') {
80
+ throw new TypeError('key must be of type string or number');
81
+ }
82
+ if (this.db) {
83
+ try {
84
+ const file = fs.readFileSync(this.db);
85
+ const data = JSON.parse(file.toString());
86
+ const i = data.findIndex(pair => pair.key == key);
87
+ !data[i] ? data.push({ key, value }) : data[i] = { key, value };
88
+ await writeDB(this.db, JSON.stringify(data));
89
+ if (!this.map)
90
+ return data;
91
+ }
92
+ catch {
93
+ await writeDB(this.db, `[${JSON.stringify({ key, value })}]`).then(() => {
94
+ if (!this.map)
95
+ return JSON.parse(fs.readFileSync(this.db).toString());
96
+ });
97
+ }
98
+ }
99
+ return this.map.set(key, value);
100
+ }
101
+ /**
102
+ *
103
+ * @param key
104
+ */
105
+ get(key) {
106
+ if (this.map) {
107
+ return this.map.get(key);
108
+ }
109
+ else {
110
+ const file = fs.readFileSync(this.db);
111
+ const data = JSON.parse(file.toString());
112
+ return data.find(pair => pair.key == key)?.value || undefined;
113
+ }
114
+ }
115
+ /**
116
+ *
117
+ * @param key
118
+ */
119
+ has(key) {
120
+ if (this.map) {
121
+ return this.map.has(key);
122
+ }
123
+ else {
124
+ const file = fs.readFileSync(this.db);
125
+ const data = JSON.parse(file.toString());
126
+ return data.find(pair => pair.key == key) ? true : false;
127
+ }
128
+ }
129
+ entries() {
130
+ if (this.map) {
131
+ return this.map.entries();
132
+ }
133
+ else {
134
+ const file = fs.readFileSync(this.db);
135
+ const data = JSON.parse(file.toString());
136
+ return data.map(pair => [pair.key, pair.value]);
137
+ }
138
+ }
139
+ keys() {
140
+ if (this.map) {
141
+ return this.map.keys();
142
+ }
143
+ else {
144
+ const file = fs.readFileSync(this.db);
145
+ const data = JSON.parse(file.toString());
146
+ return data.map(pair => pair.key);
147
+ }
148
+ }
149
+ values() {
150
+ if (this.map) {
151
+ return this.map.values();
152
+ }
153
+ else {
154
+ const file = fs.readFileSync(this.db);
155
+ const data = JSON.parse(file.toString());
156
+ return data.map(pair => pair.value);
157
+ }
158
+ }
159
+ /**
160
+ *
161
+ * @param callbackfn
162
+ */
163
+ forEach(callback) {
164
+ if (this.map) {
165
+ this.map.forEach(callback);
166
+ }
167
+ else {
168
+ const file = fs.readFileSync(this.db);
169
+ const data = JSON.parse(file.toString());
170
+ data.forEach(pair => callback(pair.value, pair.key, this.map));
171
+ }
172
+ }
173
+ /**
174
+ *
175
+ * @param key
176
+ */
177
+ async delete(key) {
178
+ if (this.db) {
179
+ try {
180
+ const file = fs.readFileSync(this.db);
181
+ const data = JSON.parse(file.toString());
182
+ const i = data.findIndex(pair => pair.key == key);
183
+ if (data[i]) {
184
+ data.splice(i, 1);
185
+ await writeDB(this.db, JSON.stringify(data));
186
+ if (!this.map)
187
+ return true;
188
+ }
189
+ else if (!this.map) {
190
+ return false;
191
+ }
192
+ }
193
+ catch { }
194
+ }
195
+ if (this.map) {
196
+ return this.map.delete(key);
197
+ }
198
+ }
199
+ async clear() {
200
+ await writeDB(this.db, JSON.stringify([])).catch(() => { });
201
+ if (this.map) {
202
+ this.map.clear();
203
+ }
204
+ }
205
+ size() {
206
+ if (this.map) {
207
+ return this.map.size;
208
+ }
209
+ else {
210
+ const file = fs.readFileSync(this.db);
211
+ const data = JSON.parse(file.toString());
212
+ return data.length;
213
+ }
214
+ }
215
+ }
216
+ 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.3.0",
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",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "homepage": "https://github.com/GalaxyVinci05/map.db#readme",
28
28
  "devDependencies": {
29
- "@types/node": "^24.10.1"
29
+ "@types/node": "^25.2.0"
30
30
  },
31
31
  "dependencies": {
32
32
  "typescript": "^5.9.3"
package/src/index.ts CHANGED
@@ -2,30 +2,33 @@ import { promisify } from 'util';
2
2
  import * as fs from 'fs';
3
3
 
4
4
  const writeDB = promisify(fs.writeFile);
5
- const deleteDB = promisify(fs.unlink);
5
+ // const deleteDB = promisify(fs.unlink);
6
6
 
7
7
  class MapDB {
8
8
  readonly map;
9
9
  filename: string;
10
10
  readonly db;
11
- options: any;
11
+ options: MapDBOptions;
12
+ private path: string;
12
13
 
13
14
  /**
14
15
  * @constructor
15
16
  * @param filename If not set, MapDB will only use internal memory
16
17
  * @example 'file.db'
17
18
  * @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
+ * @param options.localOnly Disable internal memory
20
+ * @param options.path Optional existing path to save the MapDB data directory
19
21
  */
20
- constructor(filename?: string, options?: { localOnly: boolean }) {
22
+ constructor(filename?: string, options?: MapDBOptions) {
21
23
  if (!filename && options?.localOnly) throw new Error('Cannot use local storage without a filename');
22
24
 
23
25
  this.map = !options?.localOnly ? new Map() : null;
24
26
  this.filename = filename;
27
+ this.path = options.path ?? '.';
25
28
 
26
- if (!fs.existsSync('./data/')) fs.mkdirSync('./data');
29
+ if (!fs.existsSync(`${this.path}/data/`)) fs.mkdirSync(`${this.path}/data`);
27
30
 
28
- this.db = this.filename ? `./data/${this.filename}` : null;
31
+ this.db = this.filename ? `${this.path}/data/${this.filename}` : null;
29
32
 
30
33
  if (this.map && this.db) {
31
34
  try {
@@ -197,4 +200,9 @@ class MapDB {
197
200
  }
198
201
  }
199
202
 
200
- export = MapDB;
203
+ interface MapDBOptions {
204
+ localOnly?: boolean;
205
+ path?: string;
206
+ }
207
+
208
+ export = MapDB;
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
@@ -2,17 +2,17 @@ declare class MapDB {
2
2
  readonly map: Map<any, any>;
3
3
  filename: string;
4
4
  readonly db: string;
5
- options: any;
5
+ options: MapDBOptions;
6
+ private path;
6
7
  /**
7
8
  * @constructor
8
9
  * @param filename If not set, MapDB will only use internal memory
9
10
  * @example 'file.db'
10
11
  * @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
+ * @param options.localOnly Disable internal memory
13
+ * @param options.path Optional existing path to save the MapDB data directory
12
14
  */
13
- constructor(filename?: string, options?: {
14
- localOnly: boolean;
15
- });
15
+ constructor(filename?: string, options?: MapDBOptions);
16
16
  /**
17
17
  *
18
18
  * @param key
@@ -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
@@ -45,4 +45,8 @@ declare class MapDB {
45
45
  clear(): Promise<void>;
46
46
  size(): number;
47
47
  }
48
+ interface MapDBOptions {
49
+ localOnly?: boolean;
50
+ path?: string;
51
+ }
48
52
  export = MapDB;