@wxn0brp/db 0.0.8 → 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 (55) 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 +4 -4
  24. package/dist/cjs/types/searchOpts.d.ts +1 -1
  25. package/dist/cjs/utils/hasFields.d.ts +4 -5
  26. package/dist/cjs/utils/hasFields.js +3 -4
  27. package/dist/cjs/utils/hasFieldsAdvanced.d.ts +1 -1
  28. package/dist/cjs/utils/hasFieldsAdvanced.js +3 -3
  29. package/dist/cjs/utils/updateFindObject.d.ts +8 -8
  30. package/dist/cjs/utils/updateFindObject.js +6 -7
  31. package/dist/cjs/utils/updateObject.d.ts +1 -1
  32. package/dist/esm/CollectionManager.d.ts +1 -1
  33. package/dist/esm/CollectionManager.js +2 -2
  34. package/dist/esm/action.d.ts +1 -1
  35. package/dist/esm/action.js +1 -1
  36. package/dist/esm/client/database.d.ts +4 -4
  37. package/dist/esm/client/database.js +26 -52
  38. package/dist/esm/client/graph.d.ts +2 -2
  39. package/dist/esm/client/graph.js +22 -21
  40. package/dist/esm/client/remote.d.ts +1 -2
  41. package/dist/esm/format.d.ts +1 -7
  42. package/dist/esm/format.js +0 -6
  43. package/dist/esm/gen.d.ts +6 -3
  44. package/dist/esm/gen.js +111 -52
  45. package/dist/esm/relation.d.ts +17 -47
  46. package/dist/esm/relation.js +31 -34
  47. package/dist/esm/types/arg.d.ts +4 -4
  48. package/dist/esm/types/searchOpts.d.ts +1 -1
  49. package/dist/esm/utils/hasFields.d.ts +4 -5
  50. package/dist/esm/utils/hasFields.js +3 -4
  51. package/dist/esm/utils/hasFieldsAdvanced.d.ts +1 -1
  52. package/dist/esm/utils/updateFindObject.d.ts +8 -8
  53. package/dist/esm/utils/updateFindObject.js +6 -7
  54. package/dist/esm/utils/updateObject.d.ts +1 -1
  55. package/package.json +5 -5
@@ -2,17 +2,11 @@
2
2
  * Parses given string into a JSON object. If the string does not start with
3
3
  * a {, it is wrapped in one. This allows for a shorthand when
4
4
  * storing/reading data from a file.
5
- *
6
- * @param {string} data
7
- * @returns {Object}
8
5
  */
9
- export declare function parse(data: any): any;
6
+ export declare function parse(data: string): any;
10
7
  /**
11
8
  * Converts given object to a string. If the string is a valid json5, it is
12
9
  * returned as is. If it is a valid json5 wrapped in {}, the curly brackets
13
10
  * are removed. Otherwise the string is wrapped in {}.
14
- *
15
- * @param {Object} data
16
- * @return {String}
17
11
  */
18
12
  export declare function stringify(data: any): any;
@@ -10,9 +10,6 @@ const json5_1 = __importDefault(require("json5"));
10
10
  * Parses given string into a JSON object. If the string does not start with
11
11
  * a {, it is wrapped in one. This allows for a shorthand when
12
12
  * storing/reading data from a file.
13
- *
14
- * @param {string} data
15
- * @returns {Object}
16
13
  */
17
14
  function parse(data) {
18
15
  if (!data.startsWith("{"))
@@ -23,9 +20,6 @@ function parse(data) {
23
20
  * Converts given object to a string. If the string is a valid json5, it is
24
21
  * returned as is. If it is a valid json5 wrapped in {}, the curly brackets
25
22
  * are removed. Otherwise the string is wrapped in {}.
26
- *
27
- * @param {Object} data
28
- * @return {String}
29
23
  */
30
24
  function stringify(data) {
31
25
  data = json5_1.default.stringify(data);
package/dist/cjs/gen.d.ts CHANGED
@@ -1,5 +1,8 @@
1
- import Id from "./types/Id";
1
+ import Id from "./types/Id.js";
2
2
  /**
3
- * Generates a unique identifier based on specified parts.
3
+ * Generates a unique random identifier based on time and parts.
4
+ *
5
+ * @param {number[]} [parts] - an array of lengths of parts of the identifier
6
+ * @returns {Id} - a new unique identifier
4
7
  */
5
- export default function genId(parts?: number | number[], fill?: number): Id;
8
+ export default function genId(parts?: number[]): Id;
package/dist/cjs/gen.js CHANGED
@@ -2,77 +2,136 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = genId;
4
4
  const usedIdsMap = new Map();
5
+ let lastId;
6
+ const recentIdsTimestamps = [];
7
+ let startIndex = 0;
8
+ let lastGeneratedMs = 0;
9
+ let lastRandomValue = 0;
5
10
  /**
6
- * Generates a unique identifier based on specified parts.
11
+ * Generates a unique random identifier based on time and parts.
12
+ *
13
+ * @param {number[]} [parts] - an array of lengths of parts of the identifier
14
+ * @returns {Id} - a new unique identifier
7
15
  */
8
- function genId(parts = null, fill = 1) {
9
- parts = changeInputToPartsArray(parts, fill);
16
+ function genId(parts = null) {
17
+ if (parts === null)
18
+ parts = [1, 1];
10
19
  const time = getTime();
11
- const id = getUniqueRandom(time, parts);
20
+ const id = getUniqueRandom({
21
+ time,
22
+ partsSchema: parts,
23
+ s: 0,
24
+ });
12
25
  return id;
13
26
  }
14
27
  /**
15
28
  * Generates a unique random identifier based on time and parts.
16
- * @param time - The current time in a base36 string format.
17
- * @param parts - An array of parts to be used for generating the identifier.
18
- * @param s - Recursion counter for handling collision (default: 0).
19
- * @returns {string} The unique random identifier.
20
29
  */
21
- function getUniqueRandom(time, partsA, s = 0) {
22
- const parts = partsA.map(l => getRandom(l));
23
- const id = [time, ...parts].join("-");
24
- if (usedIdsMap.has(id)) {
25
- s++;
26
- if (s < 25)
27
- return getUniqueRandom(time, partsA, s);
28
- partsA = addOneToPods(partsA);
29
- time = getTime();
30
- return getUniqueRandom(time, partsA);
30
+ function getUniqueRandom(opts) {
31
+ while (true) {
32
+ let minValue = 0;
33
+ if (lastId) {
34
+ const parts = lastId.split("-");
35
+ const lastTime = parts.shift();
36
+ if (lastTime === opts.time) {
37
+ const int36 = parts.join("");
38
+ minValue = parseInt(int36, 36) + 1;
39
+ }
40
+ }
41
+ const partsLengthSum = opts.partsSchema.reduce((acc, num) => acc + num, 0);
42
+ const partsData = generateBase36InRange(minValue, partsLengthSum);
43
+ const parts = splitStringByArray(opts.partsSchema, partsData);
44
+ const id = [opts.time, ...parts].join("-");
45
+ if (!usedIdsMap.has(id)) {
46
+ usedIdsMap.set(id, true);
47
+ setTimeout(() => usedIdsMap.delete(id), 1000);
48
+ lastId = id;
49
+ return id;
50
+ }
51
+ opts.s++;
52
+ if (opts.s >= partsLengthSum) {
53
+ opts.time = getTime();
54
+ opts.s = 0;
55
+ }
31
56
  }
32
- usedIdsMap.set(id, Date.now() + 2000);
33
- usedIdsMap.forEach((value, key) => {
34
- if (value < Date.now())
35
- usedIdsMap.delete(key);
36
- });
37
- return id;
38
57
  }
39
58
  /**
40
- * Generates a random string of base36 characters.
59
+ * Tracks the current load of the application by counting the number of ids that have been generated in the last second.
60
+ * @returns The number of ids that have been generated in the last second.
41
61
  */
42
- function getRandom(unix) {
43
- return (Math.floor(Math.random() * Math.pow(36, unix))).toString(36);
62
+ function trackLoad() {
63
+ const now = Date.now();
64
+ recentIdsTimestamps.push(now);
65
+ while (startIndex < recentIdsTimestamps.length && recentIdsTimestamps[startIndex] < now - 1) {
66
+ startIndex++;
67
+ }
68
+ return recentIdsTimestamps.length - startIndex;
44
69
  }
45
70
  /**
46
- * Gets the current time in a base36 string format.
71
+ * Generates a base-36 encoded string with a specified length based on a random value within a range.
72
+ *
73
+ * The function calculates a random value within the base-36 range determined by the provided
74
+ * `minValue` and `length`. It uses a bias mechanism to adjust the randomness based on the
75
+ * current load, ensuring unique generation over short periods. If the system load is low or
76
+ * the current millisecond has changed, a new random value is generated. Otherwise, the random
77
+ * value is incremented by a small amount with certain probabilities.
78
+ *
79
+ * @param minValue - The minimum value for the base-36 encoded number.
80
+ * @param length - The length of the resulting base-36 string.
81
+ * @returns A base-36 encoded string representation of the random value, padded to the specified length.
47
82
  */
48
- function getTime() {
49
- return Math.floor(new Date().getTime() / 1000).toString(36);
83
+ function generateBase36InRange(minValue, length) {
84
+ const maxValue = Math.pow(36, length) - 1;
85
+ const load = trackLoad();
86
+ const currentMs = Date.now();
87
+ if (load < 3 || currentMs !== lastGeneratedMs) {
88
+ lastRandomValue = biasedRandomInRange(minValue, maxValue);
89
+ lastGeneratedMs = currentMs;
90
+ }
91
+ else {
92
+ const rand = Math.random();
93
+ if (rand < 0.6) {
94
+ lastRandomValue += 1;
95
+ }
96
+ else if (rand < 0.9) {
97
+ lastRandomValue += 2;
98
+ }
99
+ else {
100
+ lastRandomValue = biasedRandomInRange(lastRandomValue, maxValue);
101
+ }
102
+ }
103
+ return lastRandomValue.toString(36).padStart(length, "0");
50
104
  }
51
105
  /**
52
- * Adds one to each part of the input array.
106
+ * Generates a random number between the specified range, but with a bias towards the lower
107
+ * end of the range. The bias is calculated as the cube root of the random value, which
108
+ * will make the random value more likely to be closer to the minimum value.
109
+ * @param min - The minimum value of the range.
110
+ * @param max - The maximum value of the range.
111
+ * @returns A random number between the specified range, with a bias towards the lower end.
53
112
  */
54
- function addOneToPods(array) {
55
- const sum = array.reduce((acc, current) => acc + current, 0);
56
- const num = sum + 1;
57
- const len = array.length;
58
- const result = [];
59
- const quotient = Math.floor(num / len);
60
- const remainder = num % len;
61
- for (let i = 0; i < len; i++) {
62
- if (i < remainder)
63
- result.push(quotient + 1);
64
- else
65
- result.push(quotient);
66
- }
67
- return result;
113
+ function biasedRandomInRange(min, max) {
114
+ let randomValue = min;
115
+ const bias = Math.pow(Math.random(), 3);
116
+ randomValue = Math.floor(min + (max - min) * bias);
117
+ return Math.min(randomValue, max);
68
118
  }
69
119
  /**
70
- * Converts input to an array of parts.
120
+ * Splits a string into an array of substrings, where each substring is determined by
121
+ * the values in the provided array.
122
+ *
123
+ * @param arr - An array of numbers, where each number represents the length of a substring.
124
+ * @param str - The string to split.
125
+ * @returns An array of substrings, where each element is a substring of the original string,
126
+ * determined by the corresponding element in the provided array.
71
127
  */
72
- function changeInputToPartsArray(parts = null, fill = 1) {
73
- if (Array.isArray(parts))
74
- return parts;
75
- if (typeof parts == "number")
76
- return Array(parts).fill(fill);
77
- return [1, 1];
128
+ function splitStringByArray(arr, str) {
129
+ let start = 0;
130
+ return arr.map(length => str.slice(start, start += length));
131
+ }
132
+ /**
133
+ * Gets the current time in a base36 string format.
134
+ */
135
+ function getTime() {
136
+ return new Date().getTime().toString(36);
78
137
  }
package/dist/cjs/graph.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const database_js_1 = __importDefault(require("./database.js"));
6
+ const database_1 = __importDefault(require("./database.js"));
7
7
  /**
8
8
  * A class representing a graph database.
9
9
  * @class
@@ -11,7 +11,7 @@ const database_js_1 = __importDefault(require("./database.js"));
11
11
  class Graph {
12
12
  db;
13
13
  constructor(databaseFolder) {
14
- this.db = new database_js_1.default(databaseFolder);
14
+ this.db = new database_1.default(databaseFolder);
15
15
  }
16
16
  /**
17
17
  * Adds an edge between two nodes.
package/dist/cjs/index.js CHANGED
@@ -4,15 +4,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.genId = exports.Relation = exports.GraphRemote = exports.DataBaseRemote = exports.Graph = exports.DataBase = void 0;
7
- const database_js_1 = __importDefault(require("./database.js"));
8
- exports.DataBase = database_js_1.default;
9
- const graph_js_1 = __importDefault(require("./graph.js"));
10
- exports.Graph = graph_js_1.default;
11
- const database_js_2 = __importDefault(require("./client/database.js"));
12
- exports.DataBaseRemote = database_js_2.default;
13
- const graph_js_2 = __importDefault(require("./client/graph.js"));
14
- exports.GraphRemote = graph_js_2.default;
15
- const gen_js_1 = __importDefault(require("./gen.js"));
16
- exports.genId = gen_js_1.default;
17
- const relation_js_1 = __importDefault(require("./relation.js"));
18
- exports.Relation = relation_js_1.default;
7
+ const database_1 = __importDefault(require("./database.js"));
8
+ exports.DataBase = database_1.default;
9
+ const graph_1 = __importDefault(require("./graph.js"));
10
+ exports.Graph = graph_1.default;
11
+ const database_2 = __importDefault(require("./client/database.js"));
12
+ exports.DataBaseRemote = database_2.default;
13
+ const graph_2 = __importDefault(require("./client/graph.js"));
14
+ exports.GraphRemote = graph_2.default;
15
+ const gen_1 = __importDefault(require("./gen.js"));
16
+ exports.genId = gen_1.default;
17
+ const relation_1 = __importDefault(require("./relation.js"));
18
+ exports.Relation = relation_1.default;
@@ -4,61 +4,31 @@ import { DbFindOpts } from "./types/options.js";
4
4
  export interface Databases {
5
5
  [key: string]: DataBase;
6
6
  }
7
+ interface RelationConfig {
8
+ from: string;
9
+ localField: string;
10
+ foreignField: string;
11
+ as?: string;
12
+ multiple?: boolean;
13
+ }
7
14
  declare class Relation {
8
- databases: Databases;
15
+ private databases;
9
16
  constructor(databases: Databases);
10
17
  /**
11
- * Resolves the relation path in format 'dbName.collectionName'
12
- */
13
- _resolvePath(path: string): {
14
- db: DataBase;
15
- collection: string;
16
- };
18
+ * Resolves the relation path in format 'dbName.collectionName'.
19
+ */
20
+ private _resolvePath;
17
21
  /**
18
- * Processes relations for a single item
19
- * @param {Object} item - Item to process relations for
20
- * @param {Object} relations - Relations configuration
21
- * @returns {Promise<Object>} Processed item with resolved relations
22
+ * Processes relations for a single item.
22
23
  */
23
- _processItemRelations(item: Object, relations: Object): Promise<{
24
- constructor: Function;
25
- toString(): string;
26
- toLocaleString(): string;
27
- valueOf(): Object;
28
- hasOwnProperty(v: PropertyKey): boolean;
29
- isPrototypeOf(v: Object): boolean;
30
- propertyIsEnumerable(v: PropertyKey): boolean;
31
- }>;
24
+ private _processItemRelations;
32
25
  /**
33
- * Finds items with relations
34
- * @param path - Path in format 'dbName.collectionName'
35
- * @param search - Search query or function
36
- * @param relations - Relations configuration
37
- * @param options - Search options
26
+ * Finds multiple items with relations.
38
27
  */
39
- find(path: string, search: Search, relations?: {}, options?: DbFindOpts): Promise<{
40
- constructor: Function;
41
- toString(): string;
42
- toLocaleString(): string;
43
- valueOf(): Object;
44
- hasOwnProperty(v: PropertyKey): boolean;
45
- isPrototypeOf(v: Object): boolean;
46
- propertyIsEnumerable(v: PropertyKey): boolean;
47
- }[]>;
28
+ find(path: string, search: Search, relations?: Record<string, RelationConfig>, options?: DbFindOpts): Promise<Record<string, any>[]>;
48
29
  /**
49
- * Finds one item with relations
50
- * @param path - Path in format 'dbName.collectionName'
51
- * @param search - Search query or function
52
- * @param relations - Relations configuration
30
+ * Finds a single item with relations.
53
31
  */
54
- findOne(path: string, search: Search, relations?: Object): Promise<{
55
- constructor: Function;
56
- toString(): string;
57
- toLocaleString(): string;
58
- valueOf(): Object;
59
- hasOwnProperty(v: PropertyKey): boolean;
60
- isPrototypeOf(v: Object): boolean;
61
- propertyIsEnumerable(v: PropertyKey): boolean;
62
- }>;
32
+ findOne(path: string, search: Search, relations?: Record<string, RelationConfig>): Promise<Record<string, any> | null>;
63
33
  }
64
34
  export default Relation;
@@ -6,62 +6,59 @@ class Relation {
6
6
  this.databases = databases;
7
7
  }
8
8
  /**
9
- * Resolves the relation path in format 'dbName.collectionName'
10
- */
9
+ * Resolves the relation path in format 'dbName.collectionName'.
10
+ */
11
11
  _resolvePath(path) {
12
- const sanitizedPath = path.replace(/\\\./g, '\uffff');
13
- const separatorIndex = sanitizedPath.indexOf('.');
14
- if (separatorIndex === -1)
12
+ if (!path.includes(".")) {
15
13
  throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
16
- const dbName = sanitizedPath.slice(0, separatorIndex).replace(/\uffff/g, '.');
17
- const collectionName = sanitizedPath.slice(separatorIndex + 1).replace(/\uffff/g, '.');
18
- if (!this.databases[dbName])
19
- throw new Error(`Database "${dbName}" not found`);
20
- return { db: this.databases[dbName], collection: collectionName };
14
+ }
15
+ const sanitizedPath = path.replace(/\\\./g, "\uffff");
16
+ const [dbName, collectionName] = sanitizedPath.split(".", 2).map(part => part.replace(/\uffff/g, "."));
17
+ const db = this.databases[dbName];
18
+ if (!db) {
19
+ throw new Error(`Database "${dbName}" not found.`);
20
+ }
21
+ return { db, collection: collectionName };
21
22
  }
22
23
  /**
23
- * Processes relations for a single item
24
- * @param {Object} item - Item to process relations for
25
- * @param {Object} relations - Relations configuration
26
- * @returns {Promise<Object>} Processed item with resolved relations
24
+ * Processes relations for a single item.
27
25
  */
28
26
  async _processItemRelations(item, relations) {
27
+ if (!item || typeof item !== "object")
28
+ return item;
29
29
  const result = { ...item };
30
30
  for (const [field, relationConfig] of Object.entries(relations)) {
31
- const { from, localField, foreignField, as, multiple = false } = relationConfig;
32
- const { db, collection } = this._resolvePath(from);
33
- const searchQuery = { [foreignField]: item[localField] };
34
- const fn = multiple ? db.find : db.findOne;
35
- const relatedItem = await fn(collection, searchQuery);
36
- result[as || field] = relatedItem;
31
+ if (!relationConfig.from || !relationConfig.localField || !relationConfig.foreignField) {
32
+ console.warn(`Skipping invalid relation configuration for field: "${field}"`);
33
+ continue;
34
+ }
35
+ try {
36
+ const { db, collection } = this._resolvePath(relationConfig.from);
37
+ const searchQuery = { [relationConfig.foreignField]: item[relationConfig.localField] };
38
+ const fetchFn = relationConfig.multiple ? db.find.bind(db) : db.findOne.bind(db);
39
+ result[relationConfig.as || field] = await fetchFn(collection, searchQuery) || null;
40
+ }
41
+ catch (error) {
42
+ console.error(`Error processing relation for field "${field}":`, error);
43
+ }
37
44
  }
38
45
  return result;
39
46
  }
40
47
  /**
41
- * Finds items with relations
42
- * @param path - Path in format 'dbName.collectionName'
43
- * @param search - Search query or function
44
- * @param relations - Relations configuration
45
- * @param options - Search options
48
+ * Finds multiple items with relations.
46
49
  */
47
50
  async find(path, search, relations = {}, options = {}) {
48
51
  const { db, collection } = this._resolvePath(path);
49
52
  const items = await db.find(collection, search, {}, options);
50
- const results = await Promise.all(items.map(item => this._processItemRelations(item, relations)));
51
- return results;
53
+ return Promise.all(items.map(item => this._processItemRelations(item, relations)));
52
54
  }
53
55
  /**
54
- * Finds one item with relations
55
- * @param path - Path in format 'dbName.collectionName'
56
- * @param search - Search query or function
57
- * @param relations - Relations configuration
56
+ * Finds a single item with relations.
58
57
  */
59
58
  async findOne(path, search, relations = {}) {
60
59
  const { db, collection } = this._resolvePath(path);
61
60
  const item = await db.findOne(collection, search);
62
- if (!item)
63
- return null;
64
- return await this._processItemRelations(item, relations);
61
+ return item ? this._processItemRelations(item, relations) : null;
65
62
  }
66
63
  }
67
64
  exports.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;
@@ -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;
@@ -3,10 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = hasFields;
4
4
  /**
5
5
  * Checks if an object matches the standard field comparison.
6
- * @function
7
- * @param {Object} obj - The object to check.
8
- * @param {Object} fields - Criteria to compare.
9
- * @returns {boolean} - Whether the object matches the criteria.
6
+ * @param obj - The object to check.
7
+ * @param fields - Criteria to compare.
8
+ * @returns Whether the object matches the criteria.
10
9
  */
11
10
  function hasFields(obj, fields) {
12
11
  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
  */
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.default = hasFieldsAdvanced;
7
- const hasFields_js_1 = __importDefault(require("./hasFields.js"));
7
+ const hasFields_1 = __importDefault(require("./hasFields.js"));
8
8
  /**
9
9
  * Checks if an object meets the criteria specified in the fields with operators.
10
10
  */
@@ -22,7 +22,7 @@ function hasFieldsAdvanced(obj, fields) {
22
22
  if (!checkConditions(obj, fields))
23
23
  return false;
24
24
  const fieldsSubset = removeAdvancedOperators({ ...fields });
25
- return (0, hasFields_js_1.default)(obj, fieldsSubset);
25
+ return (0, hasFields_1.default)(obj, fieldsSubset);
26
26
  }
27
27
  function removeAdvancedOperators(fields) {
28
28
  const advancedOperators = [
@@ -176,7 +176,7 @@ function checkNot(obj, fields) {
176
176
  function checkSubset(obj, fields) {
177
177
  if ("$subset" in fields) {
178
178
  const setFields = fields["$subset"];
179
- return (0, hasFields_js_1.default)(obj, setFields);
179
+ return (0, hasFields_1.default)(obj, setFields);
180
180
  }
181
181
  return true;
182
182
  }
@@ -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;
@@ -3,13 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = updateFindObject;
4
4
  /**
5
5
  * Updates an object with new values from a findOpts object.
6
- * @function
7
- * @param {Object} obj - The object to update.
8
- * @param {Object} findOpts - An object containing options to update the target object.
9
- * @param {function} [findOpts.transform] - A function to transform the object before applying the other options.
10
- * @param {string[]} [findOpts.select] - An array of fields to select from the target object.
11
- * @param {string[]} [findOpts.exclude] - An array of fields to exclude from the target object.
12
- * @returns {Object} The updated object.
6
+ * @param obj - The object to update.
7
+ * @param findOpts - An object containing options to update the target object.
8
+ * @param [findOpts.transform] - A function to transform the object before applying the other options.
9
+ * @param [findOpts.select] - An array of fields to select from the target object.
10
+ * @param [findOpts.exclude] - An array of fields to exclude from the target object.
11
+ * @returns The updated object.
13
12
  */
14
13
  function updateFindObject(obj, findOpts) {
15
14
  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;
@@ -39,6 +39,6 @@ declare class CollectionManager {
39
39
  /**
40
40
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
41
41
  */
42
- updateOneOrAdd(search: Search, arg: Search, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
42
+ updateOneOrAdd(search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
43
43
  }
44
44
  export default CollectionManager;
@@ -50,8 +50,8 @@ class CollectionManager {
50
50
  /**
51
51
  * Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
52
52
  */
53
- async updateOneOrAdd(search, arg, add_arg = {}, context = {}, id_gen = true) {
54
- return await this.db.updateOneOrAdd(this.collection, search, arg, add_arg, context, id_gen);
53
+ async updateOneOrAdd(search, updater, add_arg = {}, context = {}, id_gen = true) {
54
+ return await this.db.updateOneOrAdd(this.collection, search, updater, add_arg, context, id_gen);
55
55
  }
56
56
  }
57
57
  export default CollectionManager;
@@ -1,6 +1,6 @@
1
1
  import { Arg, Search, Updater } from "./types/arg.js";
2
2
  import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
3
- import { Context } from "./types/types";
3
+ import { Context } from "./types/types.js";
4
4
  import { SearchOptions } from "./types/searchOpts.js";
5
5
  import Data from "./types/data.js";
6
6
  /**
@@ -1,6 +1,6 @@
1
- import { existsSync, mkdirSync, readdirSync, appendFileSync, rmSync, writeFileSync, statSync } from "fs";
2
1
  import gen from "./gen.js";
3
2
  import { stringify } from "./format.js";
3
+ import { existsSync, mkdirSync, readdirSync, appendFileSync, rmSync, writeFileSync, statSync } from "fs";
4
4
  import { find as _find, findOne as _findOne, update as _update, remove as _remove } from "./file/index.js";
5
5
  /**
6
6
  * A class representing database actions on files.