@wxn0brp/db-storage-dir 0.100.0 → 0.101.0-alpha.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 +19 -1
- package/dist/action.d.ts +3 -1
- package/dist/action.js +28 -0
- package/dist/file/find.js +1 -2
- package/dist/file/index.js +3 -4
- package/dist/file/remove.js +1 -2
- package/dist/file/update.js +2 -3
- package/dist/format.d.ts +3 -2
- package/dist/format.js +38 -8
- package/dist/types.d.ts +8 -0
- package/package.json +12 -8
package/README.md
CHANGED
|
@@ -5,5 +5,23 @@ This plugin provides directory-based operations for ValtheraDB.
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install @wxn0brp/db-storage-dir
|
|
8
|
+
npm install @wxn0brp/db-storage-dir json5
|
|
9
9
|
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createFileActions } from "@wxn0brp/db-storage-dir";
|
|
15
|
+
|
|
16
|
+
const actions = createFileActions("dir", {
|
|
17
|
+
format: "json5:x", // or json, json:x, json5
|
|
18
|
+
})
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Benchmark
|
|
22
|
+
|
|
23
|
+
Performance benchmarks are available on the [`benchmark`](https://github.com/wxn0brP/ValtheraDB-storage-dir/tree/benchmark) branch.
|
|
24
|
+
|
|
25
|
+
## License
|
|
26
|
+
|
|
27
|
+
MIT
|
package/dist/action.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { Data } from "@wxn0brp/db-core/types/data";
|
|
|
3
3
|
import { FileCpu } from "@wxn0brp/db-core/types/fileCpu";
|
|
4
4
|
import * as Query from "@wxn0brp/db-core/types/query";
|
|
5
5
|
import { FileActionsUtils } from "./action.utils.js";
|
|
6
|
-
import { DbDirOpts } from "./types.js";
|
|
6
|
+
import { DbDirOpts, Format } from "./types.js";
|
|
7
7
|
/**
|
|
8
8
|
* A class representing database actions on files.
|
|
9
9
|
* @class
|
|
@@ -14,6 +14,7 @@ export declare class FileActions extends ActionsBase {
|
|
|
14
14
|
folder: string;
|
|
15
15
|
options: DbDirOpts;
|
|
16
16
|
_inited: boolean;
|
|
17
|
+
format: Format;
|
|
17
18
|
/**
|
|
18
19
|
* Creates a new instance of FileActions.
|
|
19
20
|
* @constructor
|
|
@@ -25,6 +26,7 @@ export declare class FileActions extends ActionsBase {
|
|
|
25
26
|
constructor(folder: string, options: DbDirOpts, fileCpu: FileCpu, utils?: FileActionsUtils);
|
|
26
27
|
init(): Promise<void>;
|
|
27
28
|
_getCollectionPath(collection: string): string;
|
|
29
|
+
_ensureQueryFormat(query: Query.VQuery): void;
|
|
28
30
|
/**
|
|
29
31
|
* Get a list of available databases in the specified folder.
|
|
30
32
|
*/
|
package/dist/action.js
CHANGED
|
@@ -5,6 +5,7 @@ import { promises } from "fs";
|
|
|
5
5
|
import { resolve, sep } from "path";
|
|
6
6
|
import { FileActionsUtils } from "./action.utils.js";
|
|
7
7
|
import { exists } from "./utils.js";
|
|
8
|
+
import { extendJson, format } from "./format.js";
|
|
8
9
|
/**
|
|
9
10
|
* A class representing database actions on files.
|
|
10
11
|
* @class
|
|
@@ -15,6 +16,7 @@ export class FileActions extends ActionsBase {
|
|
|
15
16
|
folder;
|
|
16
17
|
options;
|
|
17
18
|
_inited = false;
|
|
19
|
+
format;
|
|
18
20
|
/**
|
|
19
21
|
* Creates a new instance of FileActions.
|
|
20
22
|
* @constructor
|
|
@@ -30,16 +32,35 @@ export class FileActions extends ActionsBase {
|
|
|
30
32
|
this.folder = folder;
|
|
31
33
|
this.options = {
|
|
32
34
|
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
35
|
+
format: "json",
|
|
33
36
|
...options,
|
|
34
37
|
};
|
|
38
|
+
if (typeof this.options.format === "string") {
|
|
39
|
+
const [name, x] = this.options.format.split(":");
|
|
40
|
+
if (format[name]) {
|
|
41
|
+
if (x)
|
|
42
|
+
this.format = extendJson(format[name]);
|
|
43
|
+
else
|
|
44
|
+
this.format = { ...format[name] };
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
throw new Error(`Unknown format: ${this.options.format}`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
35
50
|
}
|
|
36
51
|
async init() {
|
|
37
52
|
if (!await exists(this.folder))
|
|
38
53
|
await promises.mkdir(this.folder, { recursive: true });
|
|
54
|
+
await this.format?.init?.();
|
|
39
55
|
}
|
|
40
56
|
_getCollectionPath(collection) {
|
|
41
57
|
return this.folder + "/" + collection + "/";
|
|
42
58
|
}
|
|
59
|
+
_ensureQueryFormat(query) {
|
|
60
|
+
query.control ||= {};
|
|
61
|
+
query.control.dir ||= {};
|
|
62
|
+
query.control.dir.format ||= this.format;
|
|
63
|
+
}
|
|
43
64
|
/**
|
|
44
65
|
* Get a list of available databases in the specified folder.
|
|
45
66
|
*/
|
|
@@ -84,6 +105,7 @@ export class FileActions extends ActionsBase {
|
|
|
84
105
|
*/
|
|
85
106
|
async add(query) {
|
|
86
107
|
const { collection, data } = query;
|
|
108
|
+
this._ensureQueryFormat(query);
|
|
87
109
|
await this.ensureCollection(collection);
|
|
88
110
|
const c_path = this._getCollectionPath(collection);
|
|
89
111
|
const file = c_path + await this.utils.getLastFile(c_path, this.options.maxFileSize, query);
|
|
@@ -96,6 +118,7 @@ export class FileActions extends ActionsBase {
|
|
|
96
118
|
*/
|
|
97
119
|
async find(query) {
|
|
98
120
|
await this.ensureCollection(query.collection);
|
|
121
|
+
this._ensureQueryFormat(query);
|
|
99
122
|
const c_path = this._getCollectionPath(query.collection);
|
|
100
123
|
let files = await this.utils.getSortedFiles(c_path, query);
|
|
101
124
|
if (files.length == 0)
|
|
@@ -109,6 +132,7 @@ export class FileActions extends ActionsBase {
|
|
|
109
132
|
*/
|
|
110
133
|
async findOne(query) {
|
|
111
134
|
const { collection } = query;
|
|
135
|
+
this._ensureQueryFormat(query);
|
|
112
136
|
await this.ensureCollection(collection);
|
|
113
137
|
const c_path = this._getCollectionPath(collection);
|
|
114
138
|
const files = await this.utils.getSortedFiles(c_path, query);
|
|
@@ -124,6 +148,7 @@ export class FileActions extends ActionsBase {
|
|
|
124
148
|
*/
|
|
125
149
|
async update(query) {
|
|
126
150
|
const { collection } = query;
|
|
151
|
+
this._ensureQueryFormat(query);
|
|
127
152
|
await this.ensureCollection(collection);
|
|
128
153
|
return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, query);
|
|
129
154
|
}
|
|
@@ -132,6 +157,7 @@ export class FileActions extends ActionsBase {
|
|
|
132
157
|
*/
|
|
133
158
|
async updateOne(query) {
|
|
134
159
|
const { collection } = query;
|
|
160
|
+
this._ensureQueryFormat(query);
|
|
135
161
|
await this.ensureCollection(collection);
|
|
136
162
|
const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, query);
|
|
137
163
|
return res[0];
|
|
@@ -141,6 +167,7 @@ export class FileActions extends ActionsBase {
|
|
|
141
167
|
*/
|
|
142
168
|
async remove(query) {
|
|
143
169
|
const { collection } = query;
|
|
170
|
+
this._ensureQueryFormat(query);
|
|
144
171
|
await this.ensureCollection(query.collection);
|
|
145
172
|
return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, query);
|
|
146
173
|
}
|
|
@@ -149,6 +176,7 @@ export class FileActions extends ActionsBase {
|
|
|
149
176
|
*/
|
|
150
177
|
async removeOne(query) {
|
|
151
178
|
const { collection } = query;
|
|
179
|
+
this._ensureQueryFormat(query);
|
|
152
180
|
await this.ensureCollection(query.collection);
|
|
153
181
|
const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, query);
|
|
154
182
|
return res[0];
|
package/dist/file/find.js
CHANGED
|
@@ -2,13 +2,12 @@ import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
|
|
|
2
2
|
import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
3
3
|
import { updateFindObject } from "@wxn0brp/db-core/utils/updateFindObject";
|
|
4
4
|
import { existsSync, promises } from "fs";
|
|
5
|
-
import { parseData } from "../format.js";
|
|
6
5
|
import { createRL } from "./utils.js";
|
|
7
6
|
/**
|
|
8
7
|
* Processes a line of text from a file and checks if it matches the search criteria.
|
|
9
8
|
*/
|
|
10
9
|
async function findProcesLine(config, line) {
|
|
11
|
-
const ob =
|
|
10
|
+
const ob = config.control.dir.format.parse(line);
|
|
12
11
|
let res = false;
|
|
13
12
|
const { search, context, findOpts = {} } = config;
|
|
14
13
|
if (typeof search === "function") {
|
package/dist/file/index.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { stringifyData } from "../format.js";
|
|
1
|
+
import { appendFile } from "fs/promises";
|
|
3
2
|
import { find, findOne } from "./find.js";
|
|
4
3
|
import { remove } from "./remove.js";
|
|
5
4
|
import { update } from "./update.js";
|
|
6
5
|
export const vFileCpu = {
|
|
7
6
|
add: async (file, config) => {
|
|
8
|
-
const dataString =
|
|
9
|
-
|
|
7
|
+
const dataString = config.control.dir.format.stringify(config.data);
|
|
8
|
+
await appendFile(file, dataString + "\n");
|
|
10
9
|
},
|
|
11
10
|
find,
|
|
12
11
|
findOne,
|
package/dist/file/remove.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
|
|
2
2
|
import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
3
3
|
import { appendFileSync, existsSync, promises } from "fs";
|
|
4
|
-
import { parseData } from "../format.js";
|
|
5
4
|
import { createRL } from "./utils.js";
|
|
6
5
|
/**
|
|
7
6
|
* Removes entries from a file based on search criteria.
|
|
@@ -27,7 +26,7 @@ export async function remove(file, config, one) {
|
|
|
27
26
|
}
|
|
28
27
|
if (!trimmed)
|
|
29
28
|
continue;
|
|
30
|
-
const data =
|
|
29
|
+
const data = config.control.dir.format.parse(trimmed);
|
|
31
30
|
if (typeof search === "function") {
|
|
32
31
|
if (search(data, context)) {
|
|
33
32
|
removed.push(data);
|
package/dist/file/update.js
CHANGED
|
@@ -2,7 +2,6 @@ import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
|
|
|
2
2
|
import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
|
|
3
3
|
import { updateObjectAdvanced } from "@wxn0brp/db-core/utils/updateObject";
|
|
4
4
|
import { existsSync, promises } from "fs";
|
|
5
|
-
import { parseData, stringifyData } from "../format.js";
|
|
6
5
|
import { createRL } from "./utils.js";
|
|
7
6
|
/**
|
|
8
7
|
* Updates a file based on search criteria and an updater function or object.
|
|
@@ -28,7 +27,7 @@ export async function update(file, config, one) {
|
|
|
28
27
|
}
|
|
29
28
|
if (!trimmed)
|
|
30
29
|
continue;
|
|
31
|
-
const data =
|
|
30
|
+
const data = config.control.dir.format.parse(trimmed);
|
|
32
31
|
let ob = false;
|
|
33
32
|
if (typeof search === "function") {
|
|
34
33
|
ob = search(data, context) || false;
|
|
@@ -46,7 +45,7 @@ export async function update(file, config, one) {
|
|
|
46
45
|
else if (typeof updater === "object" && !Array.isArray(updater)) {
|
|
47
46
|
updateObj = updateObjectAdvanced(data, updater);
|
|
48
47
|
}
|
|
49
|
-
line =
|
|
48
|
+
line = config.control.dir.format.stringify(updateObj);
|
|
50
49
|
updated.push(updateObj);
|
|
51
50
|
}
|
|
52
51
|
await promises.appendFile(file, line + "\n");
|
package/dist/format.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export declare
|
|
1
|
+
import { Format } from "./types.js";
|
|
2
|
+
export declare const format: Record<string, Format>;
|
|
3
|
+
export declare function extendJson(format: Format): Format;
|
package/dist/format.js
CHANGED
|
@@ -1,9 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
let json5;
|
|
2
|
+
export const format = {
|
|
3
|
+
json: {
|
|
4
|
+
parse(data) {
|
|
5
|
+
return JSON.parse(data);
|
|
6
|
+
},
|
|
7
|
+
stringify(data) {
|
|
8
|
+
return JSON.stringify(data);
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
json5: {
|
|
12
|
+
parse(data) {
|
|
13
|
+
return json5.parse(data);
|
|
14
|
+
},
|
|
15
|
+
stringify(data) {
|
|
16
|
+
return json5.stringify(data);
|
|
17
|
+
},
|
|
18
|
+
async init() {
|
|
19
|
+
if (!json5)
|
|
20
|
+
json5 = await import("json5");
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
export function extendJson(format) {
|
|
25
|
+
if (format._extended)
|
|
26
|
+
return;
|
|
27
|
+
return {
|
|
28
|
+
...format,
|
|
29
|
+
_extended: true,
|
|
30
|
+
parse(data) {
|
|
31
|
+
if (data[0] !== "{")
|
|
32
|
+
return format.parse(`{${data}}`);
|
|
33
|
+
return format.parse(data);
|
|
34
|
+
},
|
|
35
|
+
stringify(data) {
|
|
36
|
+
return format.stringify(data).slice(1, -1);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
9
39
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -4,9 +4,17 @@ declare module "@wxn0brp/db-core/types/query" {
|
|
|
4
4
|
dir?: {
|
|
5
5
|
lastFileNum?: number;
|
|
6
6
|
sortedFiles?: string[];
|
|
7
|
+
format?: Format;
|
|
7
8
|
};
|
|
8
9
|
}
|
|
9
10
|
}
|
|
10
11
|
export type DbDirOpts = Omit<DbOpts, "dbAction"> & {
|
|
11
12
|
maxFileSize?: number;
|
|
13
|
+
format?: string | Format;
|
|
12
14
|
};
|
|
15
|
+
export interface Format {
|
|
16
|
+
stringify: (data: any) => string;
|
|
17
|
+
parse: (data: string) => any;
|
|
18
|
+
init?: () => Promise<void>;
|
|
19
|
+
_extended?: boolean;
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-storage-dir",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.101.0-alpha.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -17,17 +17,21 @@
|
|
|
17
17
|
"valtheradb",
|
|
18
18
|
"vdb-adapter"
|
|
19
19
|
],
|
|
20
|
-
"dependencies": {
|
|
21
|
-
"json5": "^2.2.3"
|
|
22
|
-
},
|
|
23
20
|
"peerDependencies": {
|
|
24
|
-
"@wxn0brp/db-core": "
|
|
21
|
+
"@wxn0brp/db-core": "^0.10.2",
|
|
22
|
+
"json5": ""
|
|
23
|
+
},
|
|
24
|
+
"peerDependenciesMeta": {
|
|
25
|
+
"json5": {
|
|
26
|
+
"optional": true
|
|
27
|
+
}
|
|
25
28
|
},
|
|
26
29
|
"devDependencies": {
|
|
27
30
|
"@types/bun": "*",
|
|
28
|
-
"@wxn0brp/db-core": "
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
+
"@wxn0brp/db-core": "^0.10.2",
|
|
32
|
+
"json5": "^2.2.3",
|
|
33
|
+
"tsc-alias": "^1",
|
|
34
|
+
"typescript": "^6"
|
|
31
35
|
},
|
|
32
36
|
"files": [
|
|
33
37
|
"dist"
|