@wxn0brp/db-storage-dir 0.101.1 → 0.102.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/dist/action.d.ts CHANGED
@@ -4,10 +4,6 @@ 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
6
  import { DbDirOpts, Format } from "./types.js";
7
- /**
8
- * A class representing database actions on files.
9
- * @class
10
- */
11
7
  export declare class FileActions extends ActionsBase {
12
8
  fileCpu: FileCpu;
13
9
  utils: FileActionsUtils;
package/dist/action.js CHANGED
@@ -6,10 +6,6 @@ import { resolve, sep } from "path";
6
6
  import { FileActionsUtils } from "./action.utils.js";
7
7
  import { exists } from "./utils.js";
8
8
  import { extendJson, format } from "./format.js";
9
- /**
10
- * A class representing database actions on files.
11
- * @class
12
- */
13
9
  export class FileActions extends ActionsBase {
14
10
  fileCpu;
15
11
  utils;
@@ -1,13 +1,7 @@
1
1
  import { VQuery } from "@wxn0brp/db-core/types/query";
2
2
  import { DataInternal } from "@wxn0brp/db-core/types/data";
3
3
  export declare class FileActionsUtils {
4
- /**
5
- * Get the last file in the specified directory.
6
- */
7
4
  getLastFile(path: string, maxFileSize: number, query: VQuery): Promise<string>;
8
- /**
9
- * Get all files in a directory sorted by name.
10
- */
11
5
  getSortedFiles(folder: string, query: VQuery): Promise<string[]>;
12
6
  operationUpdater(c_path: string, worker: (file: string, config: VQuery, one: boolean) => Promise<boolean>, one: boolean, config: VQuery): Promise<DataInternal[]>;
13
7
  }
@@ -1,9 +1,6 @@
1
1
  import { mkdir, readdir, stat, writeFile } from "fs/promises";
2
2
  import { exists } from "./utils.js";
3
3
  export class FileActionsUtils {
4
- /**
5
- * Get the last file in the specified directory.
6
- */
7
4
  async getLastFile(path, maxFileSize, query) {
8
5
  if (!await exists(path))
9
6
  await mkdir(path, { recursive: true });
@@ -20,12 +17,9 @@ export class FileActionsUtils {
20
17
  await writeFile(path + "/" + num + ".db", "");
21
18
  query.control ||= {};
22
19
  query.control.dir ||= {};
23
- query.context.dir.lastFileNum = num;
20
+ query.control.dir.lastFileNum = num;
24
21
  return num + ".db";
25
22
  }
26
- /**
27
- * Get all files in a directory sorted by name.
28
- */
29
23
  async getSortedFiles(folder, query) {
30
24
  const files = await readdir(folder, { withFileTypes: true });
31
25
  const sorted = files
@@ -1,9 +1,3 @@
1
1
  import { FindOneQuery, FindQuery } from "@wxn0brp/db-core/types/query";
2
- /**
3
- * Asynchronously finds entries in a file based on search criteria.
4
- */
5
2
  export declare function find(file: string, config: FindQuery): Promise<any[]>;
6
- /**
7
- * Asynchronously finds one entry in a file based on search criteria.
8
- */
9
- export declare function findOne(file: string, config: FindOneQuery): Promise<any | false>;
3
+ export declare function findOne(file: string, config: FindOneQuery): Promise<any | null>;
package/dist/file/find.js CHANGED
@@ -1,35 +1,28 @@
1
1
  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
- import { existsSync, promises } from "fs";
4
+ import { exists } from "../utils.js";
5
5
  import { createRL } from "./utils.js";
6
- /**
7
- * Processes a line of text from a file and checks if it matches the search criteria.
8
- */
9
- async function findProcesLine(config, line) {
10
- const ob = config.control.dir.format.parse(line);
6
+ function findProcesLine(config, line) {
7
+ const obj = config.control.dir.format.parse(line);
11
8
  let res = false;
12
9
  const { search, context, findOpts = {} } = config;
13
10
  if (typeof search === "function") {
14
- if (search(ob, context))
11
+ if (search(obj, context))
15
12
  res = true;
16
13
  }
17
14
  else if (typeof search === "object" && !Array.isArray(search)) {
18
- if (hasFieldsAdvanced(ob, search))
15
+ if (hasFieldsAdvanced(obj, search))
19
16
  res = true;
20
17
  }
21
18
  if (res)
22
- return updateFindObject(ob, findOpts);
19
+ return updateFindObject(obj, findOpts);
23
20
  return null;
24
21
  }
25
- /**
26
- * Asynchronously finds entries in a file based on search criteria.
27
- */
28
22
  export async function find(file, config) {
29
23
  file = pathRepair(file);
30
24
  return await new Promise(async (resolve) => {
31
- if (!existsSync(file)) {
32
- await promises.writeFile(file, "");
25
+ if (!await exists(file)) {
33
26
  resolve([]);
34
27
  return;
35
28
  }
@@ -41,7 +34,7 @@ export async function find(file, config) {
41
34
  const trimmed = line.trim();
42
35
  if (!trimmed)
43
36
  continue;
44
- const res = await findProcesLine(config, trimmed);
37
+ const res = findProcesLine(config, trimmed);
45
38
  if (res)
46
39
  results.push(res);
47
40
  }
@@ -50,15 +43,11 @@ export async function find(file, config) {
50
43
  rl.close();
51
44
  });
52
45
  }
53
- /**
54
- * Asynchronously finds one entry in a file based on search criteria.
55
- */
56
46
  export async function findOne(file, config) {
57
47
  file = pathRepair(file);
58
48
  return await new Promise(async (resolve) => {
59
- if (!existsSync(file)) {
60
- await promises.writeFile(file, "");
61
- resolve(false);
49
+ if (!await exists(file)) {
50
+ resolve(null);
62
51
  return;
63
52
  }
64
53
  const rl = createRL(file);
@@ -68,13 +57,14 @@ export async function findOne(file, config) {
68
57
  const trimmed = line.trim();
69
58
  if (!trimmed)
70
59
  continue;
71
- const res = await findProcesLine(config, trimmed);
60
+ const res = findProcesLine(config, trimmed);
72
61
  if (res) {
73
62
  resolve(res);
74
63
  rl.close();
64
+ return;
75
65
  }
76
66
  }
77
67
  ;
78
- resolve(false);
68
+ resolve(null);
79
69
  });
80
70
  }
@@ -1,5 +1,2 @@
1
1
  import { RemoveQuery } from "@wxn0brp/db-core/types/query";
2
- /**
3
- * Removes entries from a file based on search criteria.
4
- */
5
2
  export declare function remove(file: string, config: RemoveQuery, one: boolean): Promise<any[]>;
@@ -1,19 +1,16 @@
1
1
  import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
2
2
  import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
3
- import { appendFileSync, existsSync, promises } from "fs";
3
+ import { createWriteStream, promises } from "fs";
4
4
  import { createRL } from "./utils.js";
5
- /**
6
- * Removes entries from a file based on search criteria.
7
- */
5
+ import { exists } from "../utils.js";
8
6
  export async function remove(file, config, one) {
9
7
  file = pathRepair(file);
10
- if (!existsSync(file)) {
11
- await promises.writeFile(file, "");
8
+ if (!await exists(file))
12
9
  return [];
13
- }
14
- await promises.copyFile(file, file + ".tmp");
15
- await promises.writeFile(file, "");
16
- const rl = createRL(file + ".tmp");
10
+ const tmpFile = file + ".tmp";
11
+ await promises.writeFile(tmpFile, "");
12
+ const rl = createRL(file);
13
+ const ws = createWriteStream(tmpFile, { flags: "a" });
17
14
  const { search, context } = config;
18
15
  let removed = [];
19
16
  for await (let line of rl) {
@@ -21,7 +18,8 @@ export async function remove(file, config, one) {
21
18
  continue;
22
19
  const trimmed = line.trim();
23
20
  if (one && removed.length) {
24
- appendFileSync(file, trimmed + "\n");
21
+ ws.write(trimmed);
22
+ ws.write("\n");
25
23
  continue;
26
24
  }
27
25
  if (!trimmed)
@@ -39,8 +37,13 @@ export async function remove(file, config, one) {
39
37
  continue;
40
38
  }
41
39
  }
42
- appendFileSync(file, line + "\n");
40
+ ws.write(trimmed);
41
+ ws.write("\n");
43
42
  }
44
- await promises.writeFile(file + ".tmp", "");
43
+ rl.close();
44
+ await new Promise((res, rej) => {
45
+ ws.end(err => err ? rej(err) : res(null));
46
+ });
47
+ await promises.rename(tmpFile, file);
45
48
  return removed;
46
49
  }
@@ -1,5 +1,2 @@
1
1
  import { UpdateQuery } from "@wxn0brp/db-core/types/query";
2
- /**
3
- * Updates a file based on search criteria and an updater function or object.
4
- */
5
2
  export declare function update(file: string, config: UpdateQuery, one: boolean): Promise<any[]>;
@@ -1,20 +1,17 @@
1
1
  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
- import { existsSync, promises } from "fs";
4
+ import { createWriteStream, promises } from "fs";
5
+ import { exists } from "../utils.js";
5
6
  import { createRL } from "./utils.js";
6
- /**
7
- * Updates a file based on search criteria and an updater function or object.
8
- */
9
7
  export async function update(file, config, one) {
10
8
  file = pathRepair(file);
11
- if (!existsSync(file)) {
12
- await promises.writeFile(file, "");
9
+ if (!await exists(file))
13
10
  return [];
14
- }
15
- await promises.copyFile(file, file + ".tmp");
16
- await promises.writeFile(file, "");
17
- const rl = createRL(file + ".tmp");
11
+ const tmpFile = file + ".tmp";
12
+ await promises.writeFile(tmpFile, "");
13
+ const rl = createRL(file);
14
+ const ws = createWriteStream(tmpFile, { flags: "a" });
18
15
  const { search, updater, context } = config;
19
16
  let updated = [];
20
17
  for await (let line of rl) {
@@ -22,20 +19,21 @@ export async function update(file, config, one) {
22
19
  continue;
23
20
  const trimmed = line.trim();
24
21
  if (one && updated.length) {
25
- await promises.appendFile(file, trimmed + "\n");
22
+ ws.write(trimmed);
23
+ ws.write("\n");
26
24
  continue;
27
25
  }
28
26
  if (!trimmed)
29
27
  continue;
30
28
  const data = config.control.dir.format.parse(trimmed);
31
- let ob = false;
29
+ let match = false;
32
30
  if (typeof search === "function") {
33
- ob = search(data, context) || false;
31
+ match = search(data, context) || false;
34
32
  }
35
33
  else if (typeof search === "object" && !Array.isArray(search)) {
36
- ob = hasFieldsAdvanced(data, search);
34
+ match = hasFieldsAdvanced(data, search);
37
35
  }
38
- if (ob) {
36
+ if (match) {
39
37
  let updateObj = data;
40
38
  if (typeof updater === "function") {
41
39
  const updateObjValue = updater(data, context);
@@ -48,8 +46,13 @@ export async function update(file, config, one) {
48
46
  line = config.control.dir.format.stringify(updateObj);
49
47
  updated.push(updateObj);
50
48
  }
51
- await promises.appendFile(file, line + "\n");
49
+ ws.write(line);
50
+ ws.write("\n");
52
51
  }
53
- await promises.writeFile(file + ".tmp", "");
52
+ rl.close();
53
+ await new Promise((res, rej) => {
54
+ ws.end(err => err ? rej(err) : res(null));
55
+ });
56
+ await promises.rename(tmpFile, file);
54
57
  return updated;
55
58
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-dir",
3
- "version": "0.101.1",
3
+ "version": "0.102.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",