@wxn0brp/db 0.0.8 → 0.1.1

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.
Files changed (75) hide show
  1. package/README.md +2 -2
  2. package/dist/cjs/CollectionManager.d.ts +1 -1
  3. package/dist/cjs/CollectionManager.js +2 -2
  4. package/dist/cjs/action.d.ts +4 -2
  5. package/dist/cjs/action.js +12 -13
  6. package/dist/cjs/client/database.d.ts +4 -4
  7. package/dist/cjs/client/database.js +29 -55
  8. package/dist/cjs/client/graph.d.ts +2 -2
  9. package/dist/cjs/client/graph.js +22 -21
  10. package/dist/cjs/client/remote.d.ts +1 -2
  11. package/dist/cjs/database.d.ts +2 -1
  12. package/dist/cjs/database.js +10 -7
  13. package/dist/cjs/file/customFileCpu.d.ts +19 -0
  14. package/dist/cjs/file/customFileCpu.js +79 -0
  15. package/dist/cjs/file/find.d.ts +2 -2
  16. package/dist/cjs/file/find.js +11 -11
  17. package/dist/cjs/file/index.d.ts +3 -3
  18. package/dist/cjs/file/index.js +16 -20
  19. package/dist/cjs/file/remove.js +7 -7
  20. package/dist/cjs/file/update.js +10 -10
  21. package/dist/cjs/format.d.ts +1 -7
  22. package/dist/cjs/format.js +0 -6
  23. package/dist/cjs/gen.d.ts +6 -3
  24. package/dist/cjs/gen.js +111 -52
  25. package/dist/cjs/graph.js +2 -2
  26. package/dist/cjs/index.d.ts +3 -2
  27. package/dist/cjs/index.js +15 -13
  28. package/dist/cjs/relation.d.ts +17 -47
  29. package/dist/cjs/relation.js +31 -34
  30. package/dist/cjs/types/arg.d.ts +4 -4
  31. package/dist/cjs/types/fileCpu.d.ts +51 -0
  32. package/dist/cjs/types/fileCpu.js +2 -0
  33. package/dist/cjs/types/searchOpts.d.ts +1 -1
  34. package/dist/cjs/utils/hasFields.d.ts +4 -5
  35. package/dist/cjs/utils/hasFields.js +3 -4
  36. package/dist/cjs/utils/hasFieldsAdvanced.d.ts +1 -1
  37. package/dist/cjs/utils/hasFieldsAdvanced.js +3 -3
  38. package/dist/cjs/utils/updateFindObject.d.ts +8 -8
  39. package/dist/cjs/utils/updateFindObject.js +6 -7
  40. package/dist/cjs/utils/updateObject.d.ts +1 -1
  41. package/dist/esm/CollectionManager.d.ts +1 -1
  42. package/dist/esm/CollectionManager.js +2 -2
  43. package/dist/esm/action.d.ts +4 -2
  44. package/dist/esm/action.js +11 -12
  45. package/dist/esm/client/database.d.ts +4 -4
  46. package/dist/esm/client/database.js +26 -52
  47. package/dist/esm/client/graph.d.ts +2 -2
  48. package/dist/esm/client/graph.js +22 -21
  49. package/dist/esm/client/remote.d.ts +1 -2
  50. package/dist/esm/database.d.ts +2 -1
  51. package/dist/esm/database.js +5 -2
  52. package/dist/esm/file/customFileCpu.d.ts +19 -0
  53. package/dist/esm/file/customFileCpu.js +74 -0
  54. package/dist/esm/file/find.d.ts +2 -2
  55. package/dist/esm/file/index.d.ts +3 -3
  56. package/dist/esm/file/index.js +16 -3
  57. package/dist/esm/format.d.ts +1 -7
  58. package/dist/esm/format.js +0 -6
  59. package/dist/esm/gen.d.ts +6 -3
  60. package/dist/esm/gen.js +111 -52
  61. package/dist/esm/index.d.ts +3 -2
  62. package/dist/esm/index.js +2 -1
  63. package/dist/esm/relation.d.ts +17 -47
  64. package/dist/esm/relation.js +31 -34
  65. package/dist/esm/types/arg.d.ts +4 -4
  66. package/dist/esm/types/fileCpu.d.ts +51 -0
  67. package/dist/esm/types/fileCpu.js +1 -0
  68. package/dist/esm/types/searchOpts.d.ts +1 -1
  69. package/dist/esm/utils/hasFields.d.ts +4 -5
  70. package/dist/esm/utils/hasFields.js +3 -4
  71. package/dist/esm/utils/hasFieldsAdvanced.d.ts +1 -1
  72. package/dist/esm/utils/updateFindObject.d.ts +8 -8
  73. package/dist/esm/utils/updateFindObject.js +6 -7
  74. package/dist/esm/utils/updateObject.d.ts +1 -1
  75. package/package.json +7 -7
@@ -4,62 +4,59 @@ class Relation {
4
4
  this.databases = databases;
5
5
  }
6
6
  /**
7
- * Resolves the relation path in format 'dbName.collectionName'
8
- */
7
+ * Resolves the relation path in format 'dbName.collectionName'.
8
+ */
9
9
  _resolvePath(path) {
10
- const sanitizedPath = path.replace(/\\\./g, '\uffff');
11
- const separatorIndex = sanitizedPath.indexOf('.');
12
- if (separatorIndex === -1)
10
+ if (!path.includes(".")) {
13
11
  throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
14
- const dbName = sanitizedPath.slice(0, separatorIndex).replace(/\uffff/g, '.');
15
- const collectionName = sanitizedPath.slice(separatorIndex + 1).replace(/\uffff/g, '.');
16
- if (!this.databases[dbName])
17
- throw new Error(`Database "${dbName}" not found`);
18
- return { db: this.databases[dbName], collection: collectionName };
12
+ }
13
+ const sanitizedPath = path.replace(/\\\./g, "\uffff");
14
+ const [dbName, collectionName] = sanitizedPath.split(".", 2).map(part => part.replace(/\uffff/g, "."));
15
+ const db = this.databases[dbName];
16
+ if (!db) {
17
+ throw new Error(`Database "${dbName}" not found.`);
18
+ }
19
+ return { db, collection: collectionName };
19
20
  }
20
21
  /**
21
- * Processes relations for a single item
22
- * @param {Object} item - Item to process relations for
23
- * @param {Object} relations - Relations configuration
24
- * @returns {Promise<Object>} Processed item with resolved relations
22
+ * Processes relations for a single item.
25
23
  */
26
24
  async _processItemRelations(item, relations) {
25
+ if (!item || typeof item !== "object")
26
+ return item;
27
27
  const result = { ...item };
28
28
  for (const [field, relationConfig] of Object.entries(relations)) {
29
- const { from, localField, foreignField, as, multiple = false } = relationConfig;
30
- const { db, collection } = this._resolvePath(from);
31
- const searchQuery = { [foreignField]: item[localField] };
32
- const fn = multiple ? db.find : db.findOne;
33
- const relatedItem = await fn(collection, searchQuery);
34
- result[as || field] = relatedItem;
29
+ if (!relationConfig.from || !relationConfig.localField || !relationConfig.foreignField) {
30
+ console.warn(`Skipping invalid relation configuration for field: "${field}"`);
31
+ continue;
32
+ }
33
+ try {
34
+ const { db, collection } = this._resolvePath(relationConfig.from);
35
+ const searchQuery = { [relationConfig.foreignField]: item[relationConfig.localField] };
36
+ const fetchFn = relationConfig.multiple ? db.find.bind(db) : db.findOne.bind(db);
37
+ result[relationConfig.as || field] = await fetchFn(collection, searchQuery) || null;
38
+ }
39
+ catch (error) {
40
+ console.error(`Error processing relation for field "${field}":`, error);
41
+ }
35
42
  }
36
43
  return result;
37
44
  }
38
45
  /**
39
- * Finds items with relations
40
- * @param path - Path in format 'dbName.collectionName'
41
- * @param search - Search query or function
42
- * @param relations - Relations configuration
43
- * @param options - Search options
46
+ * Finds multiple items with relations.
44
47
  */
45
48
  async find(path, search, relations = {}, options = {}) {
46
49
  const { db, collection } = this._resolvePath(path);
47
50
  const items = await db.find(collection, search, {}, options);
48
- const results = await Promise.all(items.map(item => this._processItemRelations(item, relations)));
49
- return results;
51
+ return Promise.all(items.map(item => this._processItemRelations(item, relations)));
50
52
  }
51
53
  /**
52
- * Finds one item with relations
53
- * @param path - Path in format 'dbName.collectionName'
54
- * @param search - Search query or function
55
- * @param relations - Relations configuration
54
+ * Finds a single item with relations.
56
55
  */
57
56
  async findOne(path, search, relations = {}) {
58
57
  const { db, collection } = this._resolvePath(path);
59
58
  const item = await db.findOne(collection, search);
60
- if (!item)
61
- return null;
62
- return await this._processItemRelations(item, relations);
59
+ return item ? this._processItemRelations(item, relations) : null;
63
60
  }
64
61
  }
65
62
  export default Relation;
@@ -1,7 +1,7 @@
1
- import Id from "./Id";
2
- import { SearchOptions } from "./searchOpts";
3
- import { Context } from "./types";
4
- import { UpdaterArg } from "./updater";
1
+ import Id from "./Id.js";
2
+ import { SearchOptions } from "./searchOpts.js";
3
+ import { Context } from "./types.js";
4
+ import { UpdaterArg } from "./updater.js";
5
5
  export interface Arg {
6
6
  _id?: Id;
7
7
  [key: string]: any;
@@ -0,0 +1,51 @@
1
+ import { Search, Updater } from "./arg.js";
2
+ import Data from "./data.js";
3
+ import { FindOpts } from "./options.js";
4
+ import { Context } from "./types.js";
5
+ interface FileCpu {
6
+ /**
7
+ * Asynchronously adds an entry to a file.
8
+ * @param file The path to the file.
9
+ * @param data The data to add.
10
+ * @returns A promise resolving to `void`.
11
+ */
12
+ add(file: string, data: Data): Promise<void>;
13
+ /**
14
+ * Asynchronously finds multiple entries in a file based on search criteria.
15
+ * @param file The path to the file.
16
+ * @param arg The search criteria.
17
+ * @param context Additional context for the search.
18
+ * @param findOpts Additional options for searching.
19
+ * @returns A promise resolving to an array of found entries, or `false` if the file does not exist.
20
+ */
21
+ find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
22
+ /**
23
+ * Asynchronously finds one entry in a file based on search criteria.
24
+ * @param file The path to the file.
25
+ * @param arg The search criteria.
26
+ * @param context Additional context for the search.
27
+ * @param findOpts Additional options for searching.
28
+ * @returns A promise resolving to the found entry or `false` if not found.
29
+ */
30
+ findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
31
+ /**
32
+ * Asynchronously removes entries from a file based on search criteria.
33
+ * @param cpath The path to the directory containing the files.
34
+ * @param arg The search criteria.
35
+ * @param context Additional context for the operation.
36
+ * @param one If `true`, removes only the first matching entry.
37
+ * @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
38
+ */
39
+ remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
40
+ /**
41
+ * Asynchronously updates entries in a file based on search criteria and an updater function or object.
42
+ * @param cpath The path to the directory containing the files.
43
+ * @param arg The search criteria.
44
+ * @param updater The updater function or object.
45
+ * @param context Additional context for the operation.
46
+ * @param one If `true`, updates only the first matching entry.
47
+ * @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
48
+ */
49
+ update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
50
+ }
51
+ export default FileCpu;
@@ -0,0 +1 @@
1
+ export {};
@@ -4,7 +4,7 @@
4
4
  * This module defines the types and structures for search operators used
5
5
  * to validate and query data objects.
6
6
  */
7
- import { Arg } from "./arg";
7
+ import { Arg } from "./arg.js";
8
8
  /** Logical Operators */
9
9
  export type LogicalOperators = {
10
10
  /**
@@ -1,8 +1,7 @@
1
1
  /**
2
2
  * Checks if an object matches the standard field comparison.
3
- * @function
4
- * @param {Object} obj - The object to check.
5
- * @param {Object} fields - Criteria to compare.
6
- * @returns {boolean} - Whether the object matches the criteria.
3
+ * @param obj - The object to check.
4
+ * @param fields - Criteria to compare.
5
+ * @returns Whether the object matches the criteria.
7
6
  */
8
- export default function hasFields(obj: any, fields: any): any;
7
+ export default function hasFields(obj: Object, fields: Object): boolean;
@@ -1,9 +1,8 @@
1
1
  /**
2
2
  * Checks if an object matches the standard field comparison.
3
- * @function
4
- * @param {Object} obj - The object to check.
5
- * @param {Object} fields - Criteria to compare.
6
- * @returns {boolean} - Whether the object matches the criteria.
3
+ * @param obj - The object to check.
4
+ * @param fields - Criteria to compare.
5
+ * @returns Whether the object matches the criteria.
7
6
  */
8
7
  export default function hasFields(obj, fields) {
9
8
  const keys = Object.keys(fields);
@@ -1,4 +1,4 @@
1
- import { Arg } from "../types/arg";
1
+ import { Arg } from "../types/arg.js";
2
2
  /**
3
3
  * Checks if an object meets the criteria specified in the fields with operators.
4
4
  */
@@ -1,11 +1,11 @@
1
+ import { FindOpts } from "../types/options.js";
1
2
  /**
2
3
  * Updates an object with new values from a findOpts object.
3
- * @function
4
- * @param {Object} obj - The object to update.
5
- * @param {Object} findOpts - An object containing options to update the target object.
6
- * @param {function} [findOpts.transform] - A function to transform the object before applying the other options.
7
- * @param {string[]} [findOpts.select] - An array of fields to select from the target object.
8
- * @param {string[]} [findOpts.exclude] - An array of fields to exclude from the target object.
9
- * @returns {Object} The updated object.
4
+ * @param obj - The object to update.
5
+ * @param findOpts - An object containing options to update the target object.
6
+ * @param [findOpts.transform] - A function to transform the object before applying the other options.
7
+ * @param [findOpts.select] - An array of fields to select from the target object.
8
+ * @param [findOpts.exclude] - An array of fields to exclude from the target object.
9
+ * @returns The updated object.
10
10
  */
11
- export default function updateFindObject(obj: any, findOpts: any): any;
11
+ export default function updateFindObject(obj: Object, findOpts: FindOpts): Object;
@@ -1,12 +1,11 @@
1
1
  /**
2
2
  * Updates an object with new values from a findOpts object.
3
- * @function
4
- * @param {Object} obj - The object to update.
5
- * @param {Object} findOpts - An object containing options to update the target object.
6
- * @param {function} [findOpts.transform] - A function to transform the object before applying the other options.
7
- * @param {string[]} [findOpts.select] - An array of fields to select from the target object.
8
- * @param {string[]} [findOpts.exclude] - An array of fields to exclude from the target object.
9
- * @returns {Object} The updated object.
3
+ * @param obj - The object to update.
4
+ * @param findOpts - An object containing options to update the target object.
5
+ * @param [findOpts.transform] - A function to transform the object before applying the other options.
6
+ * @param [findOpts.select] - An array of fields to select from the target object.
7
+ * @param [findOpts.exclude] - An array of fields to exclude from the target object.
8
+ * @returns The updated object.
10
9
  */
11
10
  export default function updateFindObject(obj, findOpts) {
12
11
  const { transform, select, exclude, } = findOpts;
@@ -4,4 +4,4 @@ import { UpdaterArg } from "../types/updater.js";
4
4
  * @param obj - The object to update.
5
5
  * @param fields - An object containing new values to update in the target object.
6
6
  */
7
- export default function updateObjectAdvanced(obj: any, fields: UpdaterArg | UpdaterArg[]): any;
7
+ export default function updateObjectAdvanced(obj: Object, fields: UpdaterArg | UpdaterArg[]): Object;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.0.8",
3
+ "version": "0.1.1",
4
4
  "main": "dist/esm/index.js",
5
5
  "types": "dist/esm/index.d.ts",
6
6
  "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
7
- "homepage": "https://github.com/wxn0brP/database",
7
+ "homepage": "https://github.com/wxn0brP/ValtheraDB",
8
8
  "repository": {
9
9
  "type": "git",
10
- "url": "https://github.com/wxn0brP/database.git"
10
+ "url": "https://github.com/wxn0brP/ValtheraDB.git"
11
11
  },
12
12
  "keywords": [
13
13
  "database",
@@ -20,18 +20,18 @@
20
20
  "license": "MIT",
21
21
  "type": "module",
22
22
  "dependencies": {
23
- "got": "^14.4.2",
24
23
  "json5": "^2.2.3",
24
+ "ky": "^1.7.4",
25
25
  "readline": "^1.3.0"
26
26
  },
27
27
  "devDependencies": {
28
- "@types/got": "^9.6.12",
29
28
  "@types/node": "^22.10.2",
29
+ "tsc-alias": "^1.8.10",
30
30
  "typescript": "^5.7.2"
31
31
  },
32
32
  "scripts": {
33
- "build:esm": "tsc",
34
- "build:cjs": "tsc --module CommonJS --outDir ./dist/cjs --declarationDir ./dist/cjs",
33
+ "build:esm": "tsc && tsc-alias -p tsconfig.json",
34
+ "build:cjs": "tsc -p tsconfig.cjs.json && tsc-alias -p tsconfig.cjs.json",
35
35
  "build": "npm run build:esm && npm run build:cjs",
36
36
  "prepare": "npm run build",
37
37
  "postversion": "git push && git push --tags"