@wxn0brp/db-storage-dir 0.1.1 → 0.1.3
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/action.d.ts +4 -4
- package/dist/action.js +18 -18
- package/dist/file/find.d.ts +2 -2
- package/dist/file/find.js +17 -11
- package/dist/file/remove.js +7 -2
- package/dist/file/update.js +7 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.js +6 -0
- package/dist/version.js +1 -1
- package/package.json +8 -5
package/dist/action.d.ts
CHANGED
|
@@ -7,12 +7,12 @@ import { VQuery } from "@wxn0brp/db-core/types/query";
|
|
|
7
7
|
* A class representing database actions on files.
|
|
8
8
|
* @class
|
|
9
9
|
*/
|
|
10
|
-
declare class
|
|
10
|
+
export declare class FileActions extends dbActionBase {
|
|
11
|
+
fileCpu: FileCpu;
|
|
11
12
|
folder: string;
|
|
12
13
|
options: DbOpts;
|
|
13
|
-
fileCpu: FileCpu;
|
|
14
14
|
/**
|
|
15
|
-
* Creates a new instance of
|
|
15
|
+
* Creates a new instance of FileActions.
|
|
16
16
|
* @constructor
|
|
17
17
|
* @param folder - The folder where database files are stored.
|
|
18
18
|
* @param options - The options object.
|
|
@@ -66,4 +66,4 @@ declare class dbActionC extends dbActionBase {
|
|
|
66
66
|
collection: any;
|
|
67
67
|
}): Promise<boolean>;
|
|
68
68
|
}
|
|
69
|
-
export default
|
|
69
|
+
export default FileActions;
|
package/dist/action.js
CHANGED
|
@@ -7,24 +7,24 @@ import { resolve, sep } from "path";
|
|
|
7
7
|
* A class representing database actions on files.
|
|
8
8
|
* @class
|
|
9
9
|
*/
|
|
10
|
-
class
|
|
10
|
+
export class FileActions extends dbActionBase {
|
|
11
|
+
fileCpu;
|
|
11
12
|
folder;
|
|
12
13
|
options;
|
|
13
|
-
fileCpu;
|
|
14
14
|
/**
|
|
15
|
-
* Creates a new instance of
|
|
15
|
+
* Creates a new instance of FileActions.
|
|
16
16
|
* @constructor
|
|
17
17
|
* @param folder - The folder where database files are stored.
|
|
18
18
|
* @param options - The options object.
|
|
19
19
|
*/
|
|
20
20
|
constructor(folder, options, fileCpu) {
|
|
21
21
|
super();
|
|
22
|
+
this.fileCpu = fileCpu;
|
|
22
23
|
this.folder = folder;
|
|
23
24
|
this.options = {
|
|
24
25
|
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
25
26
|
...options,
|
|
26
27
|
};
|
|
27
|
-
this.fileCpu = fileCpu;
|
|
28
28
|
if (!existsSync(folder))
|
|
29
29
|
mkdirSync(folder, { recursive: true });
|
|
30
30
|
}
|
|
@@ -53,8 +53,8 @@ class dbActionC extends dbActionBase {
|
|
|
53
53
|
async ensureCollection({ collection }) {
|
|
54
54
|
if (await this.issetCollection(collection))
|
|
55
55
|
return;
|
|
56
|
-
const
|
|
57
|
-
await promises.mkdir(
|
|
56
|
+
const c_path = this._getCollectionPath(collection);
|
|
57
|
+
await promises.mkdir(c_path, { recursive: true });
|
|
58
58
|
return true;
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
@@ -75,8 +75,8 @@ class dbActionC extends dbActionBase {
|
|
|
75
75
|
*/
|
|
76
76
|
async add({ collection, data, id_gen = true }) {
|
|
77
77
|
await this.ensureCollection(arguments[0]);
|
|
78
|
-
const
|
|
79
|
-
const file =
|
|
78
|
+
const c_path = this._getCollectionPath(collection);
|
|
79
|
+
const file = c_path + await getLastFile(c_path, this.options.maxFileSize);
|
|
80
80
|
if (id_gen)
|
|
81
81
|
data._id = data._id || gen();
|
|
82
82
|
await this.fileCpu.add(file, data);
|
|
@@ -87,11 +87,11 @@ class dbActionC extends dbActionBase {
|
|
|
87
87
|
*/
|
|
88
88
|
async find(query) {
|
|
89
89
|
await this.ensureCollection(query);
|
|
90
|
-
const
|
|
91
|
-
let files = await getSortedFiles(
|
|
90
|
+
const c_path = this._getCollectionPath(query.collection);
|
|
91
|
+
let files = await getSortedFiles(c_path);
|
|
92
92
|
if (files.length == 0)
|
|
93
93
|
return [];
|
|
94
|
-
files = files.map(file =>
|
|
94
|
+
files = files.map(file => c_path + file);
|
|
95
95
|
const data = await findUtil(query, this.fileCpu, files);
|
|
96
96
|
return data || [];
|
|
97
97
|
}
|
|
@@ -100,10 +100,10 @@ class dbActionC extends dbActionBase {
|
|
|
100
100
|
*/
|
|
101
101
|
async findOne({ collection, search, context = {}, findOpts = {} }) {
|
|
102
102
|
await this.ensureCollection(arguments[0]);
|
|
103
|
-
const
|
|
104
|
-
const files = await getSortedFiles(
|
|
103
|
+
const c_path = this._getCollectionPath(collection);
|
|
104
|
+
const files = await getSortedFiles(c_path);
|
|
105
105
|
for (let f of files) {
|
|
106
|
-
let data = await this.fileCpu.findOne(
|
|
106
|
+
let data = await this.fileCpu.findOne(c_path + f, search, context, findOpts);
|
|
107
107
|
if (data)
|
|
108
108
|
return data;
|
|
109
109
|
}
|
|
@@ -179,15 +179,15 @@ async function getSortedFiles(folder) {
|
|
|
179
179
|
return numA - numB;
|
|
180
180
|
});
|
|
181
181
|
}
|
|
182
|
-
async function operationUpdater(
|
|
183
|
-
const files = await getSortedFiles(
|
|
182
|
+
async function operationUpdater(c_path, worker, one, ...args) {
|
|
183
|
+
const files = await getSortedFiles(c_path);
|
|
184
184
|
let update = false;
|
|
185
185
|
for (const file of files) {
|
|
186
|
-
const updated = await worker(
|
|
186
|
+
const updated = await worker(c_path + file, one, ...args);
|
|
187
187
|
update = update || updated;
|
|
188
188
|
if (one && updated)
|
|
189
189
|
break;
|
|
190
190
|
}
|
|
191
191
|
return update;
|
|
192
192
|
}
|
|
193
|
-
export default
|
|
193
|
+
export default FileActions;
|
package/dist/file/find.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { VContext } from "@wxn0brp/db-core/types/types";
|
|
|
4
4
|
/**
|
|
5
5
|
* Asynchronously finds entries in a file based on search criteria.
|
|
6
6
|
*/
|
|
7
|
-
export declare function find(file: string,
|
|
7
|
+
export declare function find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
|
|
8
8
|
/**
|
|
9
9
|
* Asynchronously finds one entry in a file based on search criteria.
|
|
10
10
|
*/
|
|
11
|
-
export declare function findOne(file: string,
|
|
11
|
+
export declare function findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
|
package/dist/file/find.js
CHANGED
|
@@ -7,15 +7,15 @@ import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
|
|
|
7
7
|
/**
|
|
8
8
|
* Processes a line of text from a file and checks if it matches the search criteria.
|
|
9
9
|
*/
|
|
10
|
-
async function findProcesLine(
|
|
10
|
+
async function findProcesLine(search, line, context = {}, findOpts = {}) {
|
|
11
11
|
const ob = parseData(line);
|
|
12
12
|
let res = false;
|
|
13
|
-
if (typeof
|
|
14
|
-
if (
|
|
13
|
+
if (typeof search === "function") {
|
|
14
|
+
if (search(ob, context))
|
|
15
15
|
res = true;
|
|
16
16
|
}
|
|
17
|
-
else if (typeof
|
|
18
|
-
if (hasFieldsAdvanced(ob,
|
|
17
|
+
else if (typeof search === "object" && !Array.isArray(search)) {
|
|
18
|
+
if (hasFieldsAdvanced(ob, search))
|
|
19
19
|
res = true;
|
|
20
20
|
}
|
|
21
21
|
if (res)
|
|
@@ -25,7 +25,7 @@ async function findProcesLine(arg, line, context = {}, findOpts = {}) {
|
|
|
25
25
|
/**
|
|
26
26
|
* Asynchronously finds entries in a file based on search criteria.
|
|
27
27
|
*/
|
|
28
|
-
export async function find(file,
|
|
28
|
+
export async function find(file, search, context = {}, findOpts = {}) {
|
|
29
29
|
file = pathRepair(file);
|
|
30
30
|
return await new Promise(async (resolve) => {
|
|
31
31
|
if (!existsSync(file)) {
|
|
@@ -36,9 +36,12 @@ export async function find(file, arg, context = {}, findOpts = {}) {
|
|
|
36
36
|
const rl = createRL(file);
|
|
37
37
|
const resF = [];
|
|
38
38
|
for await (const line of rl) {
|
|
39
|
-
if (
|
|
39
|
+
if (!line)
|
|
40
40
|
continue;
|
|
41
|
-
const
|
|
41
|
+
const trimmed = line.trim();
|
|
42
|
+
if (!trimmed)
|
|
43
|
+
continue;
|
|
44
|
+
const res = await findProcesLine(search, trimmed, context, findOpts);
|
|
42
45
|
if (res)
|
|
43
46
|
resF.push(res);
|
|
44
47
|
}
|
|
@@ -50,7 +53,7 @@ export async function find(file, arg, context = {}, findOpts = {}) {
|
|
|
50
53
|
/**
|
|
51
54
|
* Asynchronously finds one entry in a file based on search criteria.
|
|
52
55
|
*/
|
|
53
|
-
export async function findOne(file,
|
|
56
|
+
export async function findOne(file, search, context = {}, findOpts = {}) {
|
|
54
57
|
file = pathRepair(file);
|
|
55
58
|
return await new Promise(async (resolve) => {
|
|
56
59
|
if (!existsSync(file)) {
|
|
@@ -60,9 +63,12 @@ export async function findOne(file, arg, context = {}, findOpts = {}) {
|
|
|
60
63
|
}
|
|
61
64
|
const rl = createRL(file);
|
|
62
65
|
for await (const line of rl) {
|
|
63
|
-
if (
|
|
66
|
+
if (!line)
|
|
67
|
+
continue;
|
|
68
|
+
const trimmed = line.trim();
|
|
69
|
+
if (!trimmed)
|
|
64
70
|
continue;
|
|
65
|
-
const res = await findProcesLine(
|
|
71
|
+
const res = await findProcesLine(search, trimmed, context, findOpts);
|
|
66
72
|
if (res) {
|
|
67
73
|
resolve(res);
|
|
68
74
|
rl.close();
|
package/dist/file/remove.js
CHANGED
|
@@ -17,11 +17,16 @@ async function removeWorker(file, one, search, context = {}) {
|
|
|
17
17
|
const rl = createRL(file + ".tmp");
|
|
18
18
|
let removed = false;
|
|
19
19
|
for await (let line of rl) {
|
|
20
|
+
if (!line)
|
|
21
|
+
continue;
|
|
22
|
+
const trimmed = line.trim();
|
|
20
23
|
if (one && removed) {
|
|
21
|
-
appendFileSync(file,
|
|
24
|
+
appendFileSync(file, trimmed + "\n");
|
|
22
25
|
continue;
|
|
23
26
|
}
|
|
24
|
-
|
|
27
|
+
if (!trimmed)
|
|
28
|
+
continue;
|
|
29
|
+
const data = parseData(trimmed);
|
|
25
30
|
if (typeof search === "function") {
|
|
26
31
|
if (search(data, context)) {
|
|
27
32
|
removed = true;
|
package/dist/file/update.js
CHANGED
|
@@ -18,11 +18,16 @@ async function updateWorker(file, one, search, updater, context = {}) {
|
|
|
18
18
|
const rl = createRL(file + ".tmp");
|
|
19
19
|
let updated = false;
|
|
20
20
|
for await (let line of rl) {
|
|
21
|
+
if (!line)
|
|
22
|
+
continue;
|
|
23
|
+
const trimmed = line.trim();
|
|
21
24
|
if (one && updated) {
|
|
22
|
-
await promises.appendFile(file,
|
|
25
|
+
await promises.appendFile(file, trimmed + "\n");
|
|
23
26
|
continue;
|
|
24
27
|
}
|
|
25
|
-
|
|
28
|
+
if (!trimmed)
|
|
29
|
+
continue;
|
|
30
|
+
const data = parseData(trimmed);
|
|
26
31
|
let ob = false;
|
|
27
32
|
if (typeof search === "function") {
|
|
28
33
|
ob = search(data, context) || false;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.1.
|
|
1
|
+
export const version = "0.1.3";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db-storage-dir",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"author": "wxn0brP",
|
|
@@ -13,13 +13,16 @@
|
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/wxn0brP/ValtheraDB",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@wxn0brp/db-core": ">=0.2.0",
|
|
17
16
|
"json5": "^2.2.3"
|
|
18
17
|
},
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"@wxn0brp/db-core": ">=0.2.3"
|
|
20
|
+
},
|
|
19
21
|
"devDependencies": {
|
|
20
|
-
"@types/
|
|
21
|
-
"
|
|
22
|
-
"
|
|
22
|
+
"@types/bun": "*",
|
|
23
|
+
"@wxn0brp/db-core": "^0.2.3",
|
|
24
|
+
"tsc-alias": "*",
|
|
25
|
+
"typescript": "*"
|
|
23
26
|
},
|
|
24
27
|
"files": [
|
|
25
28
|
"dist"
|