@galaxy05/map.db 1.2.2 → 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 +3 -3
- package/build/index.js +8 -5
- package/package.json +2 -2
- package/src/index.ts +15 -7
- package/types/index.d.ts +9 -5
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
|

|
|
15
15
|
|
|
16
|
-
You also have the option to only use local storage
|
|
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
|
|
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
CHANGED
|
@@ -35,27 +35,30 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
const util_1 = require("util");
|
|
36
36
|
const fs = __importStar(require("fs"));
|
|
37
37
|
const writeDB = (0, util_1.promisify)(fs.writeFile);
|
|
38
|
-
const deleteDB =
|
|
38
|
+
// const deleteDB = promisify(fs.unlink);
|
|
39
39
|
class MapDB {
|
|
40
40
|
map;
|
|
41
41
|
filename;
|
|
42
42
|
db;
|
|
43
43
|
options;
|
|
44
|
+
path;
|
|
44
45
|
/**
|
|
45
46
|
* @constructor
|
|
46
47
|
* @param filename If not set, MapDB will only use internal memory
|
|
47
48
|
* @example 'file.db'
|
|
48
49
|
* @param options Options to pass in the constructor
|
|
49
|
-
* @param options.localOnly
|
|
50
|
+
* @param options.localOnly Disable internal memory
|
|
51
|
+
* @param options.path Optional existing path to save the MapDB data directory
|
|
50
52
|
*/
|
|
51
53
|
constructor(filename, options) {
|
|
52
54
|
if (!filename && options?.localOnly)
|
|
53
55
|
throw new Error('Cannot use local storage without a filename');
|
|
54
56
|
this.map = !options?.localOnly ? new Map() : null;
|
|
55
57
|
this.filename = filename;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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;
|
|
59
62
|
if (this.map && this.db) {
|
|
60
63
|
try {
|
|
61
64
|
const file = fs.readFileSync(this.db);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@galaxy05/map.db",
|
|
3
|
-
"version": "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": "^
|
|
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:
|
|
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
|
|
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?:
|
|
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(
|
|
29
|
+
if (!fs.existsSync(`${this.path}/data/`)) fs.mkdirSync(`${this.path}/data`);
|
|
27
30
|
|
|
28
|
-
this.db = this.filename ?
|
|
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
|
-
|
|
203
|
+
interface MapDBOptions {
|
|
204
|
+
localOnly?: boolean;
|
|
205
|
+
path?: string;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export = MapDB;
|
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:
|
|
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
|
|
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
|
|
@@ -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;
|