@wxn0brp/db 0.0.7 → 0.1.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.
Files changed (57) hide show
  1. package/dist/cjs/CollectionManager.d.ts +1 -1
  2. package/dist/cjs/CollectionManager.js +2 -2
  3. package/dist/cjs/action.d.ts +1 -1
  4. package/dist/cjs/action.js +11 -11
  5. package/dist/cjs/client/database.d.ts +4 -4
  6. package/dist/cjs/client/database.js +29 -55
  7. package/dist/cjs/client/graph.d.ts +2 -2
  8. package/dist/cjs/client/graph.js +22 -21
  9. package/dist/cjs/client/remote.d.ts +1 -2
  10. package/dist/cjs/database.js +6 -6
  11. package/dist/cjs/file/find.js +11 -11
  12. package/dist/cjs/file/index.js +4 -4
  13. package/dist/cjs/file/remove.js +7 -7
  14. package/dist/cjs/file/update.js +10 -10
  15. package/dist/cjs/format.d.ts +1 -7
  16. package/dist/cjs/format.js +0 -6
  17. package/dist/cjs/gen.d.ts +6 -3
  18. package/dist/cjs/gen.js +111 -52
  19. package/dist/cjs/graph.js +2 -2
  20. package/dist/cjs/index.js +12 -12
  21. package/dist/cjs/relation.d.ts +17 -47
  22. package/dist/cjs/relation.js +31 -34
  23. package/dist/cjs/types/arg.d.ts +7 -7
  24. package/dist/cjs/types/searchOpts.d.ts +5 -4
  25. package/dist/cjs/types/types.d.ts +1 -0
  26. package/dist/cjs/utils/hasFields.d.ts +4 -5
  27. package/dist/cjs/utils/hasFields.js +3 -4
  28. package/dist/cjs/utils/hasFieldsAdvanced.d.ts +1 -1
  29. package/dist/cjs/utils/hasFieldsAdvanced.js +3 -3
  30. package/dist/cjs/utils/updateFindObject.d.ts +8 -8
  31. package/dist/cjs/utils/updateFindObject.js +6 -7
  32. package/dist/cjs/utils/updateObject.d.ts +1 -1
  33. package/dist/esm/CollectionManager.d.ts +1 -1
  34. package/dist/esm/CollectionManager.js +2 -2
  35. package/dist/esm/action.d.ts +1 -1
  36. package/dist/esm/action.js +1 -1
  37. package/dist/esm/client/database.d.ts +4 -4
  38. package/dist/esm/client/database.js +26 -52
  39. package/dist/esm/client/graph.d.ts +2 -2
  40. package/dist/esm/client/graph.js +22 -21
  41. package/dist/esm/client/remote.d.ts +1 -2
  42. package/dist/esm/format.d.ts +1 -7
  43. package/dist/esm/format.js +0 -6
  44. package/dist/esm/gen.d.ts +6 -3
  45. package/dist/esm/gen.js +111 -52
  46. package/dist/esm/relation.d.ts +17 -47
  47. package/dist/esm/relation.js +31 -34
  48. package/dist/esm/types/arg.d.ts +7 -7
  49. package/dist/esm/types/searchOpts.d.ts +5 -4
  50. package/dist/esm/types/types.d.ts +1 -0
  51. package/dist/esm/utils/hasFields.d.ts +4 -5
  52. package/dist/esm/utils/hasFields.js +3 -4
  53. package/dist/esm/utils/hasFieldsAdvanced.d.ts +1 -1
  54. package/dist/esm/utils/updateFindObject.d.ts +8 -8
  55. package/dist/esm/utils/updateFindObject.js +6 -7
  56. package/dist/esm/utils/updateObject.d.ts +1 -1
  57. package/package.json +5 -5
@@ -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,12 +1,12 @@
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;
8
8
  }
9
- export type SearchFunc<T> = (data: T, context: Context) => boolean;
10
- export type UpdaterFunc<T> = (data: T, context: Context) => boolean;
11
- export type Search<T = any> = (Arg & SearchOptions) | SearchFunc<T>;
9
+ export type SearchFunc<T = any> = (data: T, context: Context) => boolean;
10
+ export type UpdaterFunc<T = any> = (data: T, context: Context) => boolean;
11
+ export type Search<T = any> = SearchOptions | SearchFunc<T>;
12
12
  export type Updater<T = any> = UpdaterArg | UpdaterArg[] | UpdaterFunc<T>;
@@ -4,23 +4,24 @@
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.js";
7
8
  /** Logical Operators */
8
9
  export type LogicalOperators = {
9
10
  /**
10
11
  * Recursively applies multiple conditions, all of which must evaluate to true.
11
12
  * Can include other operators such as $gt, $exists, or nested $and/$or conditions.
12
13
  */
13
- $and?: Array<PredefinedSearchOperators>;
14
+ $and?: Array<SearchOptions>;
14
15
  /**
15
16
  * Recursively applies multiple conditions, at least one of which must evaluate to true.
16
17
  * Can include other operators such as $lt, $type, or nested $and/$or conditions.
17
18
  */
18
- $or?: Array<PredefinedSearchOperators>;
19
+ $or?: Array<SearchOptions>;
19
20
  /**
20
21
  * Negates a single condition.
21
22
  * Can include any other operator as its value.
22
23
  */
23
- $not?: PredefinedSearchOperators;
24
+ $not?: SearchOptions;
24
25
  };
25
26
  /** Comparison Operators */
26
27
  export type ComparisonOperators = {
@@ -58,4 +59,4 @@ export type PredefinedSearchOperators = LogicalOperators & ComparisonOperators &
58
59
  /**
59
60
  * SearchOptions can be either a function or an object with predefined operators.
60
61
  */
61
- export type SearchOptions = PredefinedSearchOperators | ((doc: Record<string, any>) => boolean);
62
+ export type SearchOptions = PredefinedSearchOperators & Arg;
@@ -1,4 +1,5 @@
1
1
  export interface Context {
2
+ [key: string]: any;
2
3
  }
3
4
  export interface SortedFiles {
4
5
  i: number;
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@wxn0brp/db",
3
- "version": "0.0.7",
3
+ "version": "0.1.0",
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.",
@@ -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"