@wxn0brp/db-storage-dir 0.2.4 → 0.10.0-alpha.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,7 +1,7 @@
1
1
  import { ActionsBase } from "@wxn0brp/db-core/base/actions";
2
2
  import { Data } from "@wxn0brp/db-core/types/data";
3
3
  import { FileCpu } from "@wxn0brp/db-core/types/fileCpu";
4
- import { VQuery } from "@wxn0brp/db-core/types/query";
4
+ import * as Query from "@wxn0brp/db-core/types/query";
5
5
  import { FileActionsUtils } from "./action.utils.js";
6
6
  import { DbDirOpts } from "./types.js";
7
7
  /**
@@ -32,41 +32,41 @@ export declare class FileActions extends ActionsBase {
32
32
  /**
33
33
  * Check and create the specified collection if it doesn't exist.
34
34
  */
35
- ensureCollection({ collection }: VQuery): Promise<boolean>;
35
+ ensureCollection(collection: string): Promise<boolean>;
36
36
  /**
37
37
  * Check if a collection exists.
38
38
  */
39
- issetCollection({ collection }: VQuery): Promise<boolean>;
39
+ issetCollection(collection: string): Promise<boolean>;
40
40
  /**
41
41
  * Add a new entry to the specified database.
42
42
  */
43
- add(query: VQuery): Promise<import("@wxn0brp/db-core/types/arg").Arg<Data>>;
43
+ add(query: Query.AddQuery): Promise<import("@wxn0brp/db-core/types/arg").Arg<Data>>;
44
44
  /**
45
45
  * Find entries in the specified database based on search criteria.
46
46
  */
47
- find(query: VQuery): Promise<Data[]>;
47
+ find(query: Query.FindQuery): Promise<Data[]>;
48
48
  /**
49
49
  * Find the first matching entry in the specified database based on search criteria.
50
50
  */
51
- findOne(query: VQuery): Promise<Data>;
51
+ findOne(query: Query.FindOneQuery): Promise<Data>;
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<any[]>;
55
+ update(query: Query.UpdateQuery): 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<any>;
59
+ updateOne(query: Query.UpdateQuery): Promise<any>;
60
60
  /**
61
61
  * Remove entries from the specified database based on search criteria.
62
62
  */
63
- remove(query: VQuery): Promise<any[]>;
63
+ remove(query: Query.RemoveQuery): 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<any>;
67
+ removeOne(query: Query.RemoveQuery): Promise<any>;
68
68
  /**
69
69
  * Removes a database collection from the file system.
70
70
  */
71
- removeCollection({ collection }: VQuery): Promise<boolean>;
71
+ removeCollection(collection: string): Promise<boolean>;
72
72
  }
package/dist/action.js CHANGED
@@ -4,6 +4,7 @@ import { findUtil } from "@wxn0brp/db-core/utils/action";
4
4
  import { promises } from "fs";
5
5
  import { resolve, sep } from "path";
6
6
  import { FileActionsUtils } from "./action.utils.js";
7
+ import { exists } from "./utils.js";
7
8
  /**
8
9
  * A class representing database actions on files.
9
10
  * @class
@@ -33,7 +34,7 @@ export class FileActions extends ActionsBase {
33
34
  };
34
35
  }
35
36
  async init() {
36
- if (!await promises.exists(this.folder))
37
+ if (!await exists(this.folder))
37
38
  await promises.mkdir(this.folder, { recursive: true });
38
39
  }
39
40
  _getCollectionPath(collection) {
@@ -58,7 +59,7 @@ export class FileActions extends ActionsBase {
58
59
  /**
59
60
  * Check and create the specified collection if it doesn't exist.
60
61
  */
61
- async ensureCollection({ collection }) {
62
+ async ensureCollection(collection) {
62
63
  if (await this.issetCollection(collection))
63
64
  return;
64
65
  const c_path = this._getCollectionPath(collection);
@@ -68,7 +69,7 @@ export class FileActions extends ActionsBase {
68
69
  /**
69
70
  * Check if a collection exists.
70
71
  */
71
- async issetCollection({ collection }) {
72
+ async issetCollection(collection) {
72
73
  const path = this._getCollectionPath(collection);
73
74
  try {
74
75
  await promises.access(path);
@@ -83,18 +84,18 @@ export class FileActions extends ActionsBase {
83
84
  */
84
85
  async add(query) {
85
86
  const { collection, data } = query;
86
- await this.ensureCollection(query);
87
+ await this.ensureCollection(collection);
87
88
  const c_path = this._getCollectionPath(collection);
88
89
  const file = c_path + await this.utils.getLastFile(c_path, this.options.maxFileSize, query);
89
90
  await addId(query, this);
90
- await this.fileCpu.add(file, data);
91
+ await this.fileCpu.add(file, query);
91
92
  return data;
92
93
  }
93
94
  /**
94
95
  * Find entries in the specified database based on search criteria.
95
96
  */
96
97
  async find(query) {
97
- await this.ensureCollection(query);
98
+ await this.ensureCollection(query.collection);
98
99
  const c_path = this._getCollectionPath(query.collection);
99
100
  let files = await this.utils.getSortedFiles(c_path, query);
100
101
  if (files.length == 0)
@@ -107,12 +108,12 @@ export class FileActions extends ActionsBase {
107
108
  * Find the first matching entry in the specified database based on search criteria.
108
109
  */
109
110
  async findOne(query) {
110
- const { collection, search, context = {}, findOpts = {} } = query;
111
- await this.ensureCollection(query);
111
+ const { collection } = query;
112
+ await this.ensureCollection(collection);
112
113
  const c_path = this._getCollectionPath(collection);
113
114
  const files = await this.utils.getSortedFiles(c_path, query);
114
115
  for (let f of files) {
115
- let data = await this.fileCpu.findOne(c_path + f, search, context, findOpts);
116
+ let data = await this.fileCpu.findOne(c_path + f, query);
116
117
  if (data)
117
118
  return data;
118
119
  }
@@ -122,40 +123,40 @@ export class FileActions extends ActionsBase {
122
123
  * Update entries in the specified database based on search criteria and an updater function or object.
123
124
  */
124
125
  async update(query) {
125
- const { collection, search, updater, context = {} } = query;
126
- await this.ensureCollection(query);
127
- return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, query, search, updater, context);
126
+ const { collection } = query;
127
+ await this.ensureCollection(collection);
128
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, query);
128
129
  }
129
130
  /**
130
131
  * Update the first matching entry in the specified database based on search criteria and an updater function or object.
131
132
  */
132
133
  async updateOne(query) {
133
- const { collection, search, updater, context = {} } = query;
134
- await this.ensureCollection(query);
135
- const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, query, search, updater, context);
134
+ const { collection } = query;
135
+ await this.ensureCollection(collection);
136
+ const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, query);
136
137
  return res[0];
137
138
  }
138
139
  /**
139
140
  * Remove entries from the specified database based on search criteria.
140
141
  */
141
142
  async remove(query) {
142
- const { collection, search, context = {} } = query;
143
- await this.ensureCollection(query);
144
- return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, query, search, context);
143
+ const { collection } = query;
144
+ await this.ensureCollection(query.collection);
145
+ return await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, query);
145
146
  }
146
147
  /**
147
148
  * Remove the first matching entry from the specified database based on search criteria.
148
149
  */
149
150
  async removeOne(query) {
150
- const { collection, search, context = {} } = query;
151
- await this.ensureCollection(query);
152
- const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, query, search, context);
151
+ const { collection } = query;
152
+ await this.ensureCollection(query.collection);
153
+ const res = await this.utils.operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, query);
153
154
  return res[0];
154
155
  }
155
156
  /**
156
157
  * Removes a database collection from the file system.
157
158
  */
158
- async removeCollection({ collection }) {
159
+ async removeCollection(collection) {
159
160
  await promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
160
161
  return true;
161
162
  }
@@ -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<any[]>;
11
+ operationUpdater(c_path: string, worker: (file: string, config: VQuery, one: boolean) => Promise<boolean>, one: boolean, config: VQuery): Promise<any[]>;
12
12
  }
@@ -1,4 +1,5 @@
1
- import { exists, mkdir, readdir, stat, writeFile } from "fs/promises";
1
+ import { mkdir, readdir, stat, writeFile } from "fs/promises";
2
+ import { exists } from "./utils.js";
2
3
  export class FileActionsUtils {
3
4
  /**
4
5
  * Get the last file in the specified directory.
@@ -41,11 +42,11 @@ export class FileActionsUtils {
41
42
  query.control.dir.sortedFiles = sorted;
42
43
  return sorted;
43
44
  }
44
- async operationUpdater(c_path, worker, one, query, ...args) {
45
- const files = await this.getSortedFiles(c_path, query);
45
+ async operationUpdater(c_path, worker, one, config) {
46
+ const files = await this.getSortedFiles(c_path, config);
46
47
  let update = [];
47
48
  for (const file of files) {
48
- const updated = await worker(c_path + file, one, ...args);
49
+ const updated = await worker(c_path + file, config, one);
49
50
  update.push(updated);
50
51
  if (one && updated)
51
52
  break;
@@ -1,11 +1,9 @@
1
- import { Search } from "@wxn0brp/db-core/types/arg";
2
- import { FindOpts } from "@wxn0brp/db-core/types/options";
3
- import { VContext } from "@wxn0brp/db-core/types/types";
1
+ import { FindOneQuery, FindQuery } from "@wxn0brp/db-core/types/query";
4
2
  /**
5
3
  * Asynchronously finds entries in a file based on search criteria.
6
4
  */
7
- export declare function find(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any[]>;
5
+ export declare function find(file: string, config: FindQuery): Promise<any[]>;
8
6
  /**
9
7
  * Asynchronously finds one entry in a file based on search criteria.
10
8
  */
11
- export declare function findOne(file: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<any | false>;
9
+ export declare function findOne(file: string, config: FindOneQuery): Promise<any | false>;
package/dist/file/find.js CHANGED
@@ -7,9 +7,10 @@ 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
  */
10
- async function findProcesLine(search, line, context = {}, findOpts = {}) {
10
+ async function findProcesLine(config, line) {
11
11
  const ob = parseData(line);
12
12
  let res = false;
13
+ const { search, context, findOpts = {} } = config;
13
14
  if (typeof search === "function") {
14
15
  if (search(ob, context))
15
16
  res = true;
@@ -25,7 +26,7 @@ async function findProcesLine(search, line, context = {}, findOpts = {}) {
25
26
  /**
26
27
  * Asynchronously finds entries in a file based on search criteria.
27
28
  */
28
- export async function find(file, search, context = {}, findOpts = {}) {
29
+ export async function find(file, config) {
29
30
  file = pathRepair(file);
30
31
  return await new Promise(async (resolve) => {
31
32
  if (!existsSync(file)) {
@@ -41,7 +42,7 @@ export async function find(file, search, context = {}, findOpts = {}) {
41
42
  const trimmed = line.trim();
42
43
  if (!trimmed)
43
44
  continue;
44
- const res = await findProcesLine(search, trimmed, context, findOpts);
45
+ const res = await findProcesLine(config, trimmed);
45
46
  if (res)
46
47
  results.push(res);
47
48
  }
@@ -53,7 +54,7 @@ export async function find(file, search, context = {}, findOpts = {}) {
53
54
  /**
54
55
  * Asynchronously finds one entry in a file based on search criteria.
55
56
  */
56
- export async function findOne(file, search, context = {}, findOpts = {}) {
57
+ export async function findOne(file, config) {
57
58
  file = pathRepair(file);
58
59
  return await new Promise(async (resolve) => {
59
60
  if (!existsSync(file)) {
@@ -68,7 +69,7 @@ export async function findOne(file, search, context = {}, findOpts = {}) {
68
69
  const trimmed = line.trim();
69
70
  if (!trimmed)
70
71
  continue;
71
- const res = await findProcesLine(search, trimmed, context, findOpts);
72
+ const res = await findProcesLine(config, trimmed);
72
73
  if (res) {
73
74
  resolve(res);
74
75
  rl.close();
@@ -4,8 +4,8 @@ import { find, findOne } from "./find.js";
4
4
  import { remove } from "./remove.js";
5
5
  import { update } from "./update.js";
6
6
  export const vFileCpu = {
7
- add: async (file, data) => {
8
- const dataString = stringifyData(data);
7
+ add: async (file, config) => {
8
+ const dataString = stringifyData(config.data);
9
9
  appendFileSync(file, dataString + "\n");
10
10
  },
11
11
  find,
@@ -1,6 +1,5 @@
1
- import { Search } from "@wxn0brp/db-core/types/arg";
2
- import { VContext } from "@wxn0brp/db-core/types/types";
1
+ import { RemoveQuery } from "@wxn0brp/db-core/types/query";
3
2
  /**
4
3
  * Removes entries from a file based on search criteria.
5
4
  */
6
- export declare function remove(file: string, one: boolean, search: Search, context?: VContext): Promise<any[]>;
5
+ export declare function remove(file: string, config: RemoveQuery, one: boolean): Promise<any[]>;
@@ -6,7 +6,7 @@ import { createRL } from "./utils.js";
6
6
  /**
7
7
  * Removes entries from a file based on search criteria.
8
8
  */
9
- export async function remove(file, one, search, context = {}) {
9
+ export async function remove(file, config, one) {
10
10
  file = pathRepair(file);
11
11
  if (!existsSync(file)) {
12
12
  await promises.writeFile(file, "");
@@ -15,6 +15,7 @@ export async function remove(file, one, search, context = {}) {
15
15
  await promises.copyFile(file, file + ".tmp");
16
16
  await promises.writeFile(file, "");
17
17
  const rl = createRL(file + ".tmp");
18
+ const { search, context } = config;
18
19
  let removed = [];
19
20
  for await (let line of rl) {
20
21
  if (!line)
@@ -1,6 +1,5 @@
1
- import { Search, Updater } from "@wxn0brp/db-core/types/arg";
2
- import { VContext } from "@wxn0brp/db-core/types/types";
1
+ import { UpdateQuery } from "@wxn0brp/db-core/types/query";
3
2
  /**
4
3
  * Updates a file based on search criteria and an updater function or object.
5
4
  */
6
- export declare function update(file: string, one: boolean, search: Search, updater: Updater, context?: VContext): Promise<any[]>;
5
+ export declare function update(file: string, config: UpdateQuery, one: boolean): Promise<any[]>;
@@ -7,7 +7,7 @@ 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
- export async function update(file, one, search, updater, context = {}) {
10
+ export async function update(file, config, one) {
11
11
  file = pathRepair(file);
12
12
  if (!existsSync(file)) {
13
13
  await promises.writeFile(file, "");
@@ -16,6 +16,7 @@ export async function update(file, one, search, updater, context = {}) {
16
16
  await promises.copyFile(file, file + ".tmp");
17
17
  await promises.writeFile(file, "");
18
18
  const rl = createRL(file + ".tmp");
19
+ const { search, updater, context } = config;
19
20
  let updated = [];
20
21
  for await (let line of rl) {
21
22
  if (!line)
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FileActions } from "./action.js";
2
2
  import { DbDirOpts } from "./types.js";
3
- export * from "./file/index.js";
4
3
  export * from "./action.js";
4
+ export * from "./file/index.js";
5
5
  export declare function createFileActions(folder: string, options?: DbDirOpts): FileActions;
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
- import { vFileCpu } from "./file/index.js";
2
1
  import { FileActions } from "./action.js";
3
- export * from "./file/index.js";
2
+ import { vFileCpu } from "./file/index.js";
4
3
  export * from "./action.js";
4
+ export * from "./file/index.js";
5
5
  export function createFileActions(folder, options = {}) {
6
6
  return new FileActions(folder, options, vFileCpu);
7
7
  }
@@ -0,0 +1 @@
1
+ export declare function exists(path: string): Promise<boolean>;
package/dist/utils.js ADDED
@@ -0,0 +1,10 @@
1
+ import { access } from "node:fs/promises";
2
+ export async function exists(path) {
3
+ try {
4
+ await access(path);
5
+ return true;
6
+ }
7
+ catch {
8
+ return false;
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db-storage-dir",
3
- "version": "0.2.4",
3
+ "version": "0.10.0-alpha.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.9.0"
24
+ "@wxn0brp/db-core": "~0.10.0-alpha.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/bun": "*",
28
- "@wxn0brp/db-core": "~0.9.0",
28
+ "@wxn0brp/db-core": "~0.10.0-alpha.1",
29
29
  "tsc-alias": "*",
30
30
  "typescript": "*"
31
31
  },
package/dist/version.js DELETED
@@ -1 +0,0 @@
1
- export const version = "0.2.4";