@wxn0brp/db-storage-dir 0.1.5 → 0.1.7

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 wxn0brP
3
+ Copyright (c) 2026 wxn0brP
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/dist/action.d.ts CHANGED
@@ -3,21 +3,27 @@ import Data from "@wxn0brp/db-core/types/data";
3
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
+ import { FileActionsUtils } from "./action.utils.js";
6
7
  /**
7
8
  * A class representing database actions on files.
8
9
  * @class
9
10
  */
10
11
  export declare class FileActions extends dbActionBase {
11
12
  fileCpu: FileCpu;
13
+ utils: FileActionsUtils;
12
14
  folder: string;
13
15
  options: DbOpts;
16
+ _inited: boolean;
14
17
  /**
15
18
  * Creates a new instance of FileActions.
16
19
  * @constructor
17
20
  * @param folder - The folder where database files are stored.
18
21
  * @param options - The options object.
22
+ * @param fileCpu - The file cpu instance
23
+ * @param utils - The utils instance
19
24
  */
20
- constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
25
+ constructor(folder: string, options: DbOpts, fileCpu: FileCpu, utils?: FileActionsUtils);
26
+ init(): Promise<void>;
21
27
  _getCollectionPath(collection: string): string;
22
28
  /**
23
29
  * Get a list of available databases in the specified folder.
package/dist/action.js CHANGED
@@ -1,32 +1,40 @@
1
1
  import dbActionBase 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
- import { existsSync, mkdirSync, promises, statSync } from "fs";
4
+ import { promises } from "fs";
5
5
  import { resolve, sep } from "path";
6
+ import { FileActionsUtils } from "./action.utils.js";
6
7
  /**
7
8
  * A class representing database actions on files.
8
9
  * @class
9
10
  */
10
11
  export class FileActions extends dbActionBase {
11
12
  fileCpu;
13
+ utils;
12
14
  folder;
13
15
  options;
16
+ _inited = false;
14
17
  /**
15
18
  * Creates a new instance of FileActions.
16
19
  * @constructor
17
20
  * @param folder - The folder where database files are stored.
18
21
  * @param options - The options object.
22
+ * @param fileCpu - The file cpu instance
23
+ * @param utils - The utils instance
19
24
  */
20
- constructor(folder, options, fileCpu) {
25
+ constructor(folder, options, fileCpu, utils = new FileActionsUtils()) {
21
26
  super();
22
27
  this.fileCpu = fileCpu;
28
+ this.utils = utils;
23
29
  this.folder = folder;
24
30
  this.options = {
25
31
  maxFileSize: 2 * 1024 * 1024, //2 MB
26
32
  ...options,
27
33
  };
28
- if (!existsSync(folder))
29
- mkdirSync(folder, { recursive: true });
34
+ }
35
+ async init() {
36
+ if (!await promises.exists(this.folder))
37
+ await promises.mkdir(this.folder, { recursive: true });
30
38
  }
31
39
  _getCollectionPath(collection) {
32
40
  return this.folder + "/" + collection + "/";
@@ -76,7 +84,7 @@ export class FileActions extends dbActionBase {
76
84
  async add({ collection, data }) {
77
85
  await this.ensureCollection(arguments[0]);
78
86
  const c_path = this._getCollectionPath(collection);
79
- const file = c_path + await getLastFile(c_path, this.options.maxFileSize);
87
+ const file = c_path + await this.utils.getLastFile(c_path, this.options.maxFileSize);
80
88
  await addId(arguments[0], this);
81
89
  await this.fileCpu.add(file, data);
82
90
  return data;
@@ -87,7 +95,7 @@ export class FileActions extends dbActionBase {
87
95
  async find(query) {
88
96
  await this.ensureCollection(query);
89
97
  const c_path = this._getCollectionPath(query.collection);
90
- let files = await getSortedFiles(c_path);
98
+ let files = await this.utils.getSortedFiles(c_path);
91
99
  if (files.length == 0)
92
100
  return [];
93
101
  files = files.map(file => c_path + file);
@@ -100,7 +108,7 @@ export class FileActions extends dbActionBase {
100
108
  async findOne({ collection, search, context = {}, findOpts = {} }) {
101
109
  await this.ensureCollection(arguments[0]);
102
110
  const c_path = this._getCollectionPath(collection);
103
- const files = await getSortedFiles(c_path);
111
+ const files = await this.utils.getSortedFiles(c_path);
104
112
  for (let f of files) {
105
113
  let data = await this.fileCpu.findOne(c_path + f, search, context, findOpts);
106
114
  if (data)
@@ -113,28 +121,28 @@ export class FileActions extends dbActionBase {
113
121
  */
114
122
  async update({ collection, search, updater, context = {} }) {
115
123
  await this.ensureCollection(arguments[0]);
116
- return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, search, updater, context);
124
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, search, updater, context);
117
125
  }
118
126
  /**
119
127
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
120
128
  */
121
129
  async updateOne({ collection, search, updater, context = {} }) {
122
130
  await this.ensureCollection(arguments[0]);
123
- return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, search, updater, context);
131
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, search, updater, context);
124
132
  }
125
133
  /**
126
134
  * Remove entries from the specified database based on search criteria.
127
135
  */
128
136
  async remove({ collection, search, context = {} }) {
129
137
  await this.ensureCollection(arguments[0]);
130
- return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, search, context);
138
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, search, context);
131
139
  }
132
140
  /**
133
141
  * Remove the first matching entry from the specified database based on search criteria.
134
142
  */
135
143
  async removeOne({ collection, search, context = {} }) {
136
144
  await this.ensureCollection(arguments[0]);
137
- return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, search, context);
145
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, search, context);
138
146
  }
139
147
  /**
140
148
  * Removes a database collection from the file system.
@@ -144,49 +152,4 @@ export class FileActions extends dbActionBase {
144
152
  return true;
145
153
  }
146
154
  }
147
- /**
148
- * Get the last file in the specified directory.
149
- */
150
- async function getLastFile(path, maxFileSize = 1024 * 1024) {
151
- if (!existsSync(path))
152
- mkdirSync(path, { recursive: true });
153
- const files = await getSortedFiles(path);
154
- if (files.length == 0) {
155
- await promises.writeFile(path + "/1.db", "");
156
- return "1.db";
157
- }
158
- const last = files[files.length - 1];
159
- const info = path + "/" + last;
160
- if (statSync(info).size < maxFileSize)
161
- return last;
162
- const num = parseInt(last.replace(".db", ""), 10) + 1;
163
- await promises.writeFile(path + "/" + num + ".db", "");
164
- return num + ".db";
165
- }
166
- /**
167
- * Get all files in a directory sorted by name.
168
- */
169
- async function getSortedFiles(folder) {
170
- const files = await promises.readdir(folder, { withFileTypes: true });
171
- return files
172
- .filter(file => file.isFile() && !file.name.endsWith(".tmp"))
173
- .map(file => file.name)
174
- .filter(name => /^\d+\.db$/.test(name))
175
- .sort((a, b) => {
176
- const numA = parseInt(a, 10);
177
- const numB = parseInt(b, 10);
178
- return numA - numB;
179
- });
180
- }
181
- async function operationUpdater(c_path, worker, one, ...args) {
182
- const files = await getSortedFiles(c_path);
183
- let update = false;
184
- for (const file of files) {
185
- const updated = await worker(c_path + file, one, ...args);
186
- update = update || updated;
187
- if (one && updated)
188
- break;
189
- }
190
- return update;
191
- }
192
155
  export default FileActions;
@@ -0,0 +1,11 @@
1
+ export declare class FileActionsUtils {
2
+ /**
3
+ * Get the last file in the specified directory.
4
+ */
5
+ getLastFile(path: string, maxFileSize: number): Promise<string>;
6
+ /**
7
+ * Get all files in a directory sorted by name.
8
+ */
9
+ getSortedFiles(folder: string): Promise<string[]>;
10
+ operationUpdater(c_path: string, worker: (file: string, one: boolean, ...args: any[]) => Promise<boolean>, one: boolean, ...args: any[]): Promise<boolean>;
11
+ }
@@ -0,0 +1,48 @@
1
+ import { exists, mkdir, readdir, stat, writeFile } from "fs/promises";
2
+ export class FileActionsUtils {
3
+ /**
4
+ * Get the last file in the specified directory.
5
+ */
6
+ async getLastFile(path, maxFileSize) {
7
+ if (!await exists(path))
8
+ await mkdir(path, { recursive: true });
9
+ const files = await this.getSortedFiles(path);
10
+ if (files.length == 0) {
11
+ await writeFile(path + "/1.db", "");
12
+ return "1.db";
13
+ }
14
+ const last = files[files.length - 1];
15
+ const info = path + "/" + last;
16
+ if ((await stat(info)).size < maxFileSize)
17
+ return last;
18
+ const num = parseInt(last.replace(".db", ""), 10) + 1;
19
+ await writeFile(path + "/" + num + ".db", "");
20
+ return num + ".db";
21
+ }
22
+ /**
23
+ * Get all files in a directory sorted by name.
24
+ */
25
+ async getSortedFiles(folder) {
26
+ const files = await readdir(folder, { withFileTypes: true });
27
+ return files
28
+ .filter(file => file.isFile() && !file.name.endsWith(".tmp"))
29
+ .map(file => file.name)
30
+ .filter(name => /^\d+\.db$/.test(name))
31
+ .sort((a, b) => {
32
+ const numA = parseInt(a, 10);
33
+ const numB = parseInt(b, 10);
34
+ return numA - numB;
35
+ });
36
+ }
37
+ async operationUpdater(c_path, worker, one, ...args) {
38
+ const files = await this.getSortedFiles(c_path);
39
+ let update = false;
40
+ for (const file of files) {
41
+ const updated = await worker(c_path + file, one, ...args);
42
+ update = update || updated;
43
+ if (one && updated)
44
+ break;
45
+ }
46
+ return update;
47
+ }
48
+ }
package/dist/format.js CHANGED
@@ -5,5 +5,5 @@ export function parseData(data) {
5
5
  return json5.parse(data);
6
6
  }
7
7
  export function stringifyData(data) {
8
- return json5.stringify(data);
8
+ return json5.stringify(data).slice(1, -1);
9
9
  }
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const version = "0.1.5";
1
+ export const version = "0.1.7";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-dir",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "wxn0brP",
@@ -12,11 +12,16 @@
12
12
  "url": "https://github.com/wxn0brP/ValtheraDB-storage-dir.git"
13
13
  },
14
14
  "homepage": "https://github.com/wxn0brP/ValtheraDB",
15
+ "keywords": [
16
+ "valthera",
17
+ "valtheradb",
18
+ "vdb-adapter"
19
+ ],
15
20
  "dependencies": {
16
21
  "json5": "^2.2.3"
17
22
  },
18
23
  "peerDependencies": {
19
- "@wxn0brp/db-core": ">=0.4.0"
24
+ "@wxn0brp/db-core": "~0.4.0"
20
25
  },
21
26
  "devDependencies": {
22
27
  "@types/bun": "*",