@wxn0brp/db 0.8.0 → 0.8.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/dist/file/utils.d.ts +4 -4
- package/dist/file/utils.js +73 -8
- package/dist/helpers/relation.js +14 -0
- package/dist/types/relation.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +2 -3
package/dist/file/utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Repairs a file path by replacing double slashes
|
|
3
3
|
*/
|
|
4
4
|
export declare function pathRepair(path: string): string;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare function createRL(file: string):
|
|
5
|
+
export interface LineReader extends AsyncIterable<string> {
|
|
6
|
+
close: () => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function createRL(file: string): LineReader;
|
package/dist/file/utils.js
CHANGED
|
@@ -1,19 +1,84 @@
|
|
|
1
1
|
import { createReadStream } from "fs";
|
|
2
|
-
import { createInterface } from "readline";
|
|
3
2
|
/**
|
|
4
3
|
* Repairs a file path by replacing double slashes
|
|
5
4
|
*/
|
|
6
5
|
export function pathRepair(path) {
|
|
7
6
|
return path.replaceAll("//", "/");
|
|
8
7
|
}
|
|
9
|
-
/**
|
|
10
|
-
* Creates a Readline interface for reading large files with a specified high water mark.
|
|
11
|
-
*/
|
|
12
8
|
export function createRL(file) {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
const stream = createReadStream(file, { highWaterMark: 64 * 1024 });
|
|
10
|
+
let buffer = "";
|
|
11
|
+
let done = false;
|
|
12
|
+
let error = null;
|
|
13
|
+
const lines = [];
|
|
14
|
+
let waiting = null;
|
|
15
|
+
stream.on("data", (chunk) => {
|
|
16
|
+
buffer += chunk.toString("utf8");
|
|
17
|
+
let index;
|
|
18
|
+
while ((index = buffer.search(/\r?\n/)) >= 0) {
|
|
19
|
+
const line = buffer.slice(0, index);
|
|
20
|
+
lines.push(line);
|
|
21
|
+
buffer = buffer.slice(index + (buffer[index] === "\r" && buffer[index + 1] === "\n" ? 2 : 1));
|
|
22
|
+
}
|
|
23
|
+
feed();
|
|
24
|
+
});
|
|
25
|
+
stream.on("end", () => {
|
|
26
|
+
if (buffer.length > 0) {
|
|
27
|
+
lines.push(buffer);
|
|
28
|
+
buffer = "";
|
|
29
|
+
}
|
|
30
|
+
done = true;
|
|
31
|
+
feed();
|
|
32
|
+
});
|
|
33
|
+
stream.on("error", (err) => {
|
|
34
|
+
error = err;
|
|
35
|
+
done = true;
|
|
36
|
+
feed();
|
|
17
37
|
});
|
|
38
|
+
const feed = () => {
|
|
39
|
+
if (waiting) {
|
|
40
|
+
if (error) {
|
|
41
|
+
waiting(Promise.reject(error));
|
|
42
|
+
}
|
|
43
|
+
else if (lines.length > 0) {
|
|
44
|
+
waiting({ value: lines.shift(), done: false });
|
|
45
|
+
}
|
|
46
|
+
else if (done) {
|
|
47
|
+
waiting({ value: undefined, done: true });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
waiting = null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const iterator = {
|
|
56
|
+
next() {
|
|
57
|
+
if (error)
|
|
58
|
+
return Promise.reject(error);
|
|
59
|
+
if (lines.length > 0)
|
|
60
|
+
return Promise.resolve({ value: lines.shift(), done: false });
|
|
61
|
+
if (done)
|
|
62
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
63
|
+
return new Promise(res => {
|
|
64
|
+
waiting = res;
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
return() {
|
|
68
|
+
rl.close();
|
|
69
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const rl = {
|
|
73
|
+
[Symbol.asyncIterator]() {
|
|
74
|
+
return iterator;
|
|
75
|
+
},
|
|
76
|
+
close() {
|
|
77
|
+
if (!done) {
|
|
78
|
+
done = true;
|
|
79
|
+
stream.destroy();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
18
83
|
return rl;
|
|
19
84
|
}
|
package/dist/helpers/relation.js
CHANGED
|
@@ -57,6 +57,20 @@ async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
57
57
|
item[as] = result;
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
|
+
else if (type === "11") {
|
|
61
|
+
const cache = new Map();
|
|
62
|
+
for (const item of targets) {
|
|
63
|
+
const id = item[pk];
|
|
64
|
+
if (!cache.has(id)) {
|
|
65
|
+
cache.set(id, await db.findOne(coll, { [fk]: id }, {}, { select }));
|
|
66
|
+
}
|
|
67
|
+
const result = cache.get(id) || null;
|
|
68
|
+
if (result && rel.relations) {
|
|
69
|
+
await processRelations(dbs, rel.relations, result);
|
|
70
|
+
}
|
|
71
|
+
item[as] = result;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
60
74
|
else if (type === "1n") {
|
|
61
75
|
const ids = targets.map(i => i[pk]);
|
|
62
76
|
const results = await db.find(coll, { $in: { [fk]: ids } }, {}, findOpts || {}, { select });
|
package/dist/types/relation.d.ts
CHANGED
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.8.
|
|
1
|
+
export const version = "0.8.2";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"type": "module",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"json5": "^2.2.3",
|
|
24
|
-
"ky": "^1.7.4"
|
|
25
|
-
"readline": "^1.3.0"
|
|
24
|
+
"ky": "^1.7.4"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
27
|
"@types/node": "^22.10.2",
|