@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.
- package/dist/cjs/CollectionManager.d.ts +1 -1
- package/dist/cjs/CollectionManager.js +2 -2
- package/dist/cjs/action.d.ts +1 -1
- package/dist/cjs/action.js +11 -11
- package/dist/cjs/client/database.d.ts +4 -4
- package/dist/cjs/client/database.js +29 -55
- package/dist/cjs/client/graph.d.ts +2 -2
- package/dist/cjs/client/graph.js +22 -21
- package/dist/cjs/client/remote.d.ts +1 -2
- package/dist/cjs/database.js +6 -6
- package/dist/cjs/file/find.js +11 -11
- package/dist/cjs/file/index.js +4 -4
- package/dist/cjs/file/remove.js +7 -7
- package/dist/cjs/file/update.js +10 -10
- package/dist/cjs/format.d.ts +1 -7
- package/dist/cjs/format.js +0 -6
- package/dist/cjs/gen.d.ts +6 -3
- package/dist/cjs/gen.js +111 -52
- package/dist/cjs/graph.js +2 -2
- package/dist/cjs/index.js +12 -12
- package/dist/cjs/relation.d.ts +17 -47
- package/dist/cjs/relation.js +31 -34
- package/dist/cjs/types/arg.d.ts +4 -4
- package/dist/cjs/types/searchOpts.d.ts +1 -1
- package/dist/cjs/utils/hasFields.d.ts +4 -5
- package/dist/cjs/utils/hasFields.js +3 -4
- package/dist/cjs/utils/hasFieldsAdvanced.d.ts +1 -1
- package/dist/cjs/utils/hasFieldsAdvanced.js +3 -3
- package/dist/cjs/utils/updateFindObject.d.ts +8 -8
- package/dist/cjs/utils/updateFindObject.js +6 -7
- package/dist/cjs/utils/updateObject.d.ts +1 -1
- package/dist/esm/CollectionManager.d.ts +1 -1
- package/dist/esm/CollectionManager.js +2 -2
- package/dist/esm/action.d.ts +1 -1
- package/dist/esm/action.js +1 -1
- package/dist/esm/client/database.d.ts +4 -4
- package/dist/esm/client/database.js +26 -52
- package/dist/esm/client/graph.d.ts +2 -2
- package/dist/esm/client/graph.js +22 -21
- package/dist/esm/client/remote.d.ts +1 -2
- package/dist/esm/format.d.ts +1 -7
- package/dist/esm/format.js +0 -6
- package/dist/esm/gen.d.ts +6 -3
- package/dist/esm/gen.js +111 -52
- package/dist/esm/relation.d.ts +17 -47
- package/dist/esm/relation.js +31 -34
- package/dist/esm/types/arg.d.ts +4 -4
- package/dist/esm/types/searchOpts.d.ts +1 -1
- package/dist/esm/utils/hasFields.d.ts +4 -5
- package/dist/esm/utils/hasFields.js +3 -4
- package/dist/esm/utils/hasFieldsAdvanced.d.ts +1 -1
- package/dist/esm/utils/updateFindObject.d.ts +8 -8
- package/dist/esm/utils/updateFindObject.js +6 -7
- package/dist/esm/utils/updateObject.d.ts +1 -1
- package/package.json +5 -5
package/dist/cjs/format.d.ts
CHANGED
|
@@ -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:
|
|
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;
|
package/dist/cjs/format.js
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
|
|
9
|
-
|
|
16
|
+
function genId(parts = null) {
|
|
17
|
+
if (parts === null)
|
|
18
|
+
parts = [1, 1];
|
|
10
19
|
const time = getTime();
|
|
11
|
-
const id = getUniqueRandom(
|
|
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(
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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
|
-
*
|
|
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
|
|
43
|
-
|
|
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
|
-
*
|
|
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
|
|
49
|
-
|
|
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
|
-
*
|
|
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
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
*
|
|
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
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
|
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
|
|
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
|
|
8
|
-
exports.DataBase =
|
|
9
|
-
const
|
|
10
|
-
exports.Graph =
|
|
11
|
-
const
|
|
12
|
-
exports.DataBaseRemote =
|
|
13
|
-
const
|
|
14
|
-
exports.GraphRemote =
|
|
15
|
-
const
|
|
16
|
-
exports.genId =
|
|
17
|
-
const
|
|
18
|
-
exports.Relation =
|
|
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;
|
package/dist/cjs/relation.d.ts
CHANGED
|
@@ -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
|
|
15
|
+
private databases;
|
|
9
16
|
constructor(databases: Databases);
|
|
10
17
|
/**
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
_resolvePath
|
|
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
|
|
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?:
|
|
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
|
|
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?:
|
|
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;
|
package/dist/cjs/relation.js
CHANGED
|
@@ -6,62 +6,59 @@ class Relation {
|
|
|
6
6
|
this.databases = databases;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
* Resolves the relation path in format 'dbName.collectionName'.
|
|
10
|
+
*/
|
|
11
11
|
_resolvePath(path) {
|
|
12
|
-
|
|
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
|
-
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
51
|
-
return results;
|
|
53
|
+
return Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
52
54
|
}
|
|
53
55
|
/**
|
|
54
|
-
* Finds
|
|
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
|
-
|
|
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;
|
package/dist/cjs/types/arg.d.ts
CHANGED
|
@@ -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;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Checks if an object matches the standard field comparison.
|
|
3
|
-
* @
|
|
4
|
-
* @param
|
|
5
|
-
* @
|
|
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:
|
|
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
|
-
* @
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
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);
|
|
@@ -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
|
|
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,
|
|
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,
|
|
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
|
-
* @
|
|
4
|
-
* @param
|
|
5
|
-
* @param
|
|
6
|
-
* @param
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
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:
|
|
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
|
-
* @
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
10
|
-
* @param
|
|
11
|
-
* @
|
|
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:
|
|
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,
|
|
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,
|
|
54
|
-
return await this.db.updateOneOrAdd(this.collection, search,
|
|
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;
|
package/dist/esm/action.d.ts
CHANGED
|
@@ -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
|
/**
|
package/dist/esm/action.js
CHANGED
|
@@ -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.
|