@wxn0brp/db-storage-dir 0.1.8 → 0.2.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
@@ -1,6 +1,6 @@
1
- import dbActionBase from "@wxn0brp/db-core/base/actions";
2
- import Data from "@wxn0brp/db-core/types/data";
3
- import FileCpu from "@wxn0brp/db-core/types/fileCpu";
1
+ import { ActionsBase } from "@wxn0brp/db-core/base/actions";
2
+ import { Data } from "@wxn0brp/db-core/types/data";
3
+ import { FileCpu } from "@wxn0brp/db-core/types/fileCpu";
4
4
  import { DbOpts } from "@wxn0brp/db-core/types/options";
5
5
  import { VQuery } from "@wxn0brp/db-core/types/query";
6
6
  import { FileActionsUtils } from "./action.utils.js";
@@ -8,7 +8,7 @@ import { FileActionsUtils } from "./action.utils.js";
8
8
  * A class representing database actions on files.
9
9
  * @class
10
10
  */
11
- export declare class FileActions extends dbActionBase {
11
+ export declare class FileActions extends ActionsBase {
12
12
  fileCpu: FileCpu;
13
13
  utils: FileActionsUtils;
14
14
  folder: string;
@@ -52,22 +52,21 @@ export declare class FileActions extends dbActionBase {
52
52
  /**
53
53
  * Update entries in the specified database based on search criteria and an updater function or object.
54
54
  */
55
- update(query: VQuery): Promise<boolean>;
55
+ update(query: VQuery): Promise<any[]>;
56
56
  /**
57
57
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
58
58
  */
59
- updateOne(query: VQuery): Promise<boolean>;
59
+ updateOne(query: VQuery): Promise<any[]>;
60
60
  /**
61
61
  * Remove entries from the specified database based on search criteria.
62
62
  */
63
- remove(query: VQuery): Promise<boolean>;
63
+ remove(query: VQuery): Promise<any[]>;
64
64
  /**
65
65
  * Remove the first matching entry from the specified database based on search criteria.
66
66
  */
67
- removeOne(query: VQuery): Promise<boolean>;
67
+ removeOne(query: VQuery): Promise<any[]>;
68
68
  /**
69
69
  * Removes a database collection from the file system.
70
70
  */
71
71
  removeCollection({ collection }: VQuery): Promise<boolean>;
72
72
  }
73
- export default FileActions;
package/dist/action.js CHANGED
@@ -1,4 +1,4 @@
1
- import dbActionBase from "@wxn0brp/db-core/base/actions";
1
+ import { ActionsBase } from "@wxn0brp/db-core/base/actions";
2
2
  import { addId } from "@wxn0brp/db-core/helpers/addId";
3
3
  import { findUtil } from "@wxn0brp/db-core/utils/action";
4
4
  import { promises } from "fs";
@@ -8,7 +8,7 @@ import { FileActionsUtils } from "./action.utils.js";
8
8
  * A class representing database actions on files.
9
9
  * @class
10
10
  */
11
- export class FileActions extends dbActionBase {
11
+ export class FileActions extends ActionsBase {
12
12
  fileCpu;
13
13
  utils;
14
14
  folder;
@@ -158,4 +158,3 @@ export class FileActions extends dbActionBase {
158
158
  return true;
159
159
  }
160
160
  }
161
- export default FileActions;
@@ -8,5 +8,5 @@ export declare class FileActionsUtils {
8
8
  * Get all files in a directory sorted by name.
9
9
  */
10
10
  getSortedFiles(folder: string, query: VQuery): Promise<string[]>;
11
- operationUpdater(c_path: string, worker: (file: string, one: boolean, ...args: any[]) => Promise<boolean>, one: boolean, query: VQuery, ...args: any[]): Promise<boolean>;
11
+ operationUpdater(c_path: string, worker: (file: string, one: boolean, ...args: any[]) => Promise<boolean>, one: boolean, query: VQuery, ...args: any[]): Promise<any[]>;
12
12
  }
@@ -17,8 +17,9 @@ export class FileActionsUtils {
17
17
  return last;
18
18
  const num = parseInt(last.replace(".db", ""), 10) + 1;
19
19
  await writeFile(path + "/" + num + ".db", "");
20
- query.context ||= {};
21
- query.context._dir_lastFileNum = num;
20
+ query.control ||= {};
21
+ query.control.dir ||= {};
22
+ query.context.dir.lastFileNum = num;
22
23
  return num + ".db";
23
24
  }
24
25
  /**
@@ -35,19 +36,20 @@ export class FileActionsUtils {
35
36
  const numB = parseInt(b, 10);
36
37
  return numA - numB;
37
38
  });
38
- query.context ||= {};
39
- query.context._dir_sortedFiles = sorted;
39
+ query.control ||= {};
40
+ query.control.dir ||= {};
41
+ query.control.dir.sortedFiles = sorted;
40
42
  return sorted;
41
43
  }
42
44
  async operationUpdater(c_path, worker, one, query, ...args) {
43
45
  const files = await this.getSortedFiles(c_path, query);
44
- let update = false;
46
+ let update = [];
45
47
  for (const file of files) {
46
48
  const updated = await worker(c_path + file, one, ...args);
47
- update = update || updated;
49
+ update.push(updated);
48
50
  if (one && updated)
49
51
  break;
50
52
  }
51
- return update;
53
+ return update.flat();
52
54
  }
53
55
  }
@@ -4,7 +4,7 @@ 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, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[] | false>;
7
+ export declare function find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[]>;
8
8
  /**
9
9
  * Asynchronously finds one entry in a file based on search criteria.
10
10
  */
package/dist/file/find.js CHANGED
@@ -1,9 +1,9 @@
1
- import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
2
- import updateFindObject from "@wxn0brp/db-core/utils/updateFindObject";
1
+ import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
2
+ import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
3
+ import { updateFindObject } from "@wxn0brp/db-core/utils/updateFindObject";
3
4
  import { existsSync, promises } from "fs";
4
- import { createRL } from "./utils.js";
5
5
  import { parseData } from "../format.js";
6
- import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
6
+ import { createRL } from "./utils.js";
7
7
  /**
8
8
  * Processes a line of text from a file and checks if it matches the search criteria.
9
9
  */
@@ -30,11 +30,11 @@ export async function find(file, search, context = {}, findOpts = {}) {
30
30
  return await new Promise(async (resolve) => {
31
31
  if (!existsSync(file)) {
32
32
  await promises.writeFile(file, "");
33
- resolve(false);
33
+ resolve([]);
34
34
  return;
35
35
  }
36
36
  const rl = createRL(file);
37
- const resF = [];
37
+ const results = [];
38
38
  for await (const line of rl) {
39
39
  if (!line)
40
40
  continue;
@@ -43,10 +43,10 @@ export async function find(file, search, context = {}, findOpts = {}) {
43
43
  continue;
44
44
  const res = await findProcesLine(search, trimmed, context, findOpts);
45
45
  if (res)
46
- resF.push(res);
46
+ results.push(res);
47
47
  }
48
48
  ;
49
- resolve(resF);
49
+ resolve(results);
50
50
  rl.close();
51
51
  });
52
52
  }
@@ -1,3 +1,2 @@
1
- import FileCpu from "@wxn0brp/db-core/types/fileCpu";
2
- declare const vFileCpu: FileCpu;
3
- export default vFileCpu;
1
+ import { FileCpu } from "@wxn0brp/db-core/types/fileCpu";
2
+ export declare const vFileCpu: FileCpu;
@@ -1,9 +1,9 @@
1
- import update from "./update.js";
2
- import remove from "./remove.js";
1
+ import { update } from "./update.js";
2
+ import { remove } from "./remove.js";
3
3
  import { find, findOne } from "./find.js";
4
4
  import { appendFileSync } from "fs";
5
5
  import { stringifyData } from "../format.js";
6
- const vFileCpu = {
6
+ export const vFileCpu = {
7
7
  add: async (file, data) => {
8
8
  const dataString = stringifyData(data);
9
9
  appendFileSync(file, dataString + "\n");
@@ -13,4 +13,3 @@ const vFileCpu = {
13
13
  update,
14
14
  remove,
15
15
  };
16
- export default vFileCpu;
@@ -3,5 +3,4 @@ import { VContext } from "@wxn0brp/db-core/types/types";
3
3
  /**
4
4
  * Removes entries from a file based on search criteria.
5
5
  */
6
- declare function removeWorker(file: string, one: boolean, search: Search, context?: VContext): Promise<boolean>;
7
- export default removeWorker;
6
+ export declare function remove(file: string, one: boolean, search: Search, context?: VContext): Promise<any[]>;
@@ -1,26 +1,26 @@
1
- import { existsSync, promises, appendFileSync } from "fs";
2
- import { createRL } from "./utils.js";
3
- import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
4
- import { parseData } from "../format.js";
5
1
  import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
2
+ import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
3
+ import { appendFileSync, existsSync, promises } from "fs";
4
+ import { parseData } from "../format.js";
5
+ import { createRL } from "./utils.js";
6
6
  /**
7
7
  * Removes entries from a file based on search criteria.
8
8
  */
9
- async function removeWorker(file, one, search, context = {}) {
9
+ export async function remove(file, one, search, context = {}) {
10
10
  file = pathRepair(file);
11
11
  if (!existsSync(file)) {
12
12
  await promises.writeFile(file, "");
13
- return false;
13
+ return [];
14
14
  }
15
15
  await promises.copyFile(file, file + ".tmp");
16
16
  await promises.writeFile(file, "");
17
17
  const rl = createRL(file + ".tmp");
18
- let removed = false;
18
+ let removed = [];
19
19
  for await (let line of rl) {
20
20
  if (!line)
21
21
  continue;
22
22
  const trimmed = line.trim();
23
- if (one && removed) {
23
+ if (one && removed.length) {
24
24
  appendFileSync(file, trimmed + "\n");
25
25
  continue;
26
26
  }
@@ -29,13 +29,13 @@ async function removeWorker(file, one, search, context = {}) {
29
29
  const data = parseData(trimmed);
30
30
  if (typeof search === "function") {
31
31
  if (search(data, context)) {
32
- removed = true;
32
+ removed.push(data);
33
33
  continue;
34
34
  }
35
35
  }
36
36
  else if (typeof search === "object" && !Array.isArray(search)) {
37
37
  if (hasFieldsAdvanced(data, search)) {
38
- removed = true;
38
+ removed.push(data);
39
39
  continue;
40
40
  }
41
41
  }
@@ -44,4 +44,3 @@ async function removeWorker(file, one, search, context = {}) {
44
44
  await promises.writeFile(file + ".tmp", "");
45
45
  return removed;
46
46
  }
47
- export default removeWorker;
@@ -3,5 +3,4 @@ import { VContext } from "@wxn0brp/db-core/types/types";
3
3
  /**
4
4
  * Updates a file based on search criteria and an updater function or object.
5
5
  */
6
- declare function updateWorker(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
7
- export default updateWorker;
6
+ export declare function update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<any[]>;
@@ -1,27 +1,27 @@
1
+ import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
2
+ import { hasFieldsAdvanced } from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
3
+ import { updateObjectAdvanced } from "@wxn0brp/db-core/utils/updateObject";
1
4
  import { existsSync, promises } from "fs";
2
- import { createRL } from "./utils.js";
3
- import hasFieldsAdvanced from "@wxn0brp/db-core/utils/hasFieldsAdvanced";
4
- import updateObjectAdvanced from "@wxn0brp/db-core/utils/updateObject";
5
5
  import { parseData, stringifyData } from "../format.js";
6
- import { pathRepair } from "@wxn0brp/db-core/customFileCpu";
6
+ import { createRL } from "./utils.js";
7
7
  /**
8
8
  * Updates a file based on search criteria and an updater function or object.
9
9
  */
10
- async function updateWorker(file, one, search, updater, context = {}) {
10
+ export async function update(file, one, search, updater, context = {}) {
11
11
  file = pathRepair(file);
12
12
  if (!existsSync(file)) {
13
13
  await promises.writeFile(file, "");
14
- return false;
14
+ return [];
15
15
  }
16
16
  await promises.copyFile(file, file + ".tmp");
17
17
  await promises.writeFile(file, "");
18
18
  const rl = createRL(file + ".tmp");
19
- let updated = false;
19
+ let updated = [];
20
20
  for await (let line of rl) {
21
21
  if (!line)
22
22
  continue;
23
23
  const trimmed = line.trim();
24
- if (one && updated) {
24
+ if (one && updated.length) {
25
25
  await promises.appendFile(file, trimmed + "\n");
26
26
  continue;
27
27
  }
@@ -46,11 +46,10 @@ async function updateWorker(file, one, search, updater, context = {}) {
46
46
  updateObj = updateObjectAdvanced(data, updater);
47
47
  }
48
48
  line = await stringifyData(updateObj);
49
- updated = true;
49
+ updated.push(updateObj);
50
50
  }
51
51
  await promises.appendFile(file, line + "\n");
52
52
  }
53
53
  await promises.writeFile(file + ".tmp", "");
54
54
  return updated;
55
55
  }
56
- export default updateWorker;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DbOpts } from "@wxn0brp/db-core/types/options";
2
- import FileActions from "./action.js";
2
+ import { FileActions } from "./action.js";
3
3
  export * from "./file/index.js";
4
4
  export * from "./action.js";
5
5
  export declare function createFileActions(folder: string, options?: DbOpts): FileActions;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- import FileActions from "./action.js";
2
- import vFileCpu from "./file/index.js";
1
+ import { vFileCpu } from "./file/index.js";
2
+ import { FileActions } from "./action.js";
3
3
  export * from "./file/index.js";
4
4
  export * from "./action.js";
5
5
  export function createFileActions(folder, options = {}) {
@@ -0,0 +1,9 @@
1
+ declare module "@wxn0brp/db-core/types/query" {
2
+ interface VQuery_Control {
3
+ dir: {
4
+ lastFileNum?: number;
5
+ sortedFiles?: string[];
6
+ };
7
+ }
8
+ }
9
+ export {};
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.8";
1
+ export const version = "0.2.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-dir",
3
- "version": "0.1.8",
3
+ "version": "0.2.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
@@ -21,11 +21,11 @@
21
21
  "json5": "^2.2.3"
22
22
  },
23
23
  "peerDependencies": {
24
- "@wxn0brp/db-core": "~0.4.0"
24
+ "@wxn0brp/db-core": "~0.5.0"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "*",
28
- "@wxn0brp/db-core": "^0.4.0",
28
+ "@wxn0brp/db-core": "~0.5.0",
29
29
  "tsc-alias": "*",
30
30
  "typescript": "*"
31
31
  },