@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.
- package/README.md +2 -2
- package/dist/cjs/CollectionManager.d.ts +1 -1
- package/dist/cjs/CollectionManager.js +2 -2
- package/dist/cjs/action.d.ts +4 -2
- package/dist/cjs/action.js +12 -13
- 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.d.ts +2 -1
- package/dist/cjs/database.js +10 -7
- package/dist/cjs/file/customFileCpu.d.ts +19 -0
- package/dist/cjs/file/customFileCpu.js +79 -0
- package/dist/cjs/file/find.d.ts +2 -2
- package/dist/cjs/file/find.js +11 -11
- package/dist/cjs/file/index.d.ts +3 -3
- package/dist/cjs/file/index.js +16 -20
- 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.d.ts +3 -2
- package/dist/cjs/index.js +15 -13
- 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/fileCpu.d.ts +51 -0
- package/dist/cjs/types/fileCpu.js +2 -0
- 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 +4 -2
- package/dist/esm/action.js +11 -12
- 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/database.d.ts +2 -1
- package/dist/esm/database.js +5 -2
- package/dist/esm/file/customFileCpu.d.ts +19 -0
- package/dist/esm/file/customFileCpu.js +74 -0
- package/dist/esm/file/find.d.ts +2 -2
- package/dist/esm/file/index.d.ts +3 -3
- package/dist/esm/file/index.js +16 -3
- 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/index.d.ts +3 -2
- package/dist/esm/index.js +2 -1
- 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/fileCpu.d.ts +51 -0
- package/dist/esm/types/fileCpu.js +1 -0
- 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 +7 -7
package/dist/esm/relation.js
CHANGED
|
@@ -4,62 +4,59 @@ class Relation {
|
|
|
4
4
|
this.databases = databases;
|
|
5
5
|
}
|
|
6
6
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
* Resolves the relation path in format 'dbName.collectionName'.
|
|
8
|
+
*/
|
|
9
9
|
_resolvePath(path) {
|
|
10
|
-
|
|
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
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
|
|
49
|
-
return results;
|
|
51
|
+
return Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
|
-
* Finds
|
|
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
|
-
|
|
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;
|
package/dist/esm/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;
|
|
@@ -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 {};
|
|
@@ -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;
|
|
@@ -1,9 +1,8 @@
|
|
|
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
7
|
export default function hasFields(obj, fields) {
|
|
9
8
|
const keys = Object.keys(fields);
|
|
@@ -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;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 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.
|
|
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:
|
|
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.
|
|
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/
|
|
7
|
+
"homepage": "https://github.com/wxn0brP/ValtheraDB",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
|
-
"url": "https://github.com/wxn0brP/
|
|
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
|
|
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"
|