@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/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;
|
|
@@ -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;
|
|
@@ -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,8 +1,9 @@
|
|
|
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
|
+
import FileCpu from "./types/fileCpu.js";
|
|
6
7
|
/**
|
|
7
8
|
* A class representing database actions on files.
|
|
8
9
|
* @class
|
|
@@ -10,13 +11,14 @@ import Data from "./types/data.js";
|
|
|
10
11
|
declare class dbActionC {
|
|
11
12
|
folder: string;
|
|
12
13
|
options: DbOpts;
|
|
14
|
+
fileCpu: FileCpu;
|
|
13
15
|
/**
|
|
14
16
|
* Creates a new instance of dbActionC.
|
|
15
17
|
* @constructor
|
|
16
18
|
* @param folder - The folder where database files are stored.
|
|
17
19
|
* @param options - The options object.
|
|
18
20
|
*/
|
|
19
|
-
constructor(folder: string, options: DbOpts);
|
|
21
|
+
constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
|
|
20
22
|
_getCollectionPath(collection: string): string;
|
|
21
23
|
/**
|
|
22
24
|
* Get a list of available databases in the specified folder.
|
package/dist/esm/action.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, readdirSync, appendFileSync, rmSync, writeFileSync, statSync } from "fs";
|
|
2
1
|
import gen from "./gen.js";
|
|
3
|
-
import {
|
|
4
|
-
import { find as _find, findOne as _findOne, update as _update, remove as _remove } from "./file/index.js";
|
|
2
|
+
import { existsSync, mkdirSync, readdirSync, rmSync, writeFileSync, statSync } from "fs";
|
|
5
3
|
/**
|
|
6
4
|
* A class representing database actions on files.
|
|
7
5
|
* @class
|
|
@@ -9,18 +7,20 @@ import { find as _find, findOne as _findOne, update as _update, remove as _remov
|
|
|
9
7
|
class dbActionC {
|
|
10
8
|
folder;
|
|
11
9
|
options;
|
|
10
|
+
fileCpu;
|
|
12
11
|
/**
|
|
13
12
|
* Creates a new instance of dbActionC.
|
|
14
13
|
* @constructor
|
|
15
14
|
* @param folder - The folder where database files are stored.
|
|
16
15
|
* @param options - The options object.
|
|
17
16
|
*/
|
|
18
|
-
constructor(folder, options) {
|
|
17
|
+
constructor(folder, options, fileCpu) {
|
|
19
18
|
this.folder = folder;
|
|
20
19
|
this.options = {
|
|
21
20
|
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
22
21
|
...options,
|
|
23
22
|
};
|
|
23
|
+
this.fileCpu = fileCpu;
|
|
24
24
|
if (!existsSync(folder))
|
|
25
25
|
mkdirSync(folder, { recursive: true });
|
|
26
26
|
}
|
|
@@ -64,8 +64,7 @@ class dbActionC {
|
|
|
64
64
|
const file = cpath + getLastFile(cpath, this.options.maxFileSize);
|
|
65
65
|
if (id_gen)
|
|
66
66
|
arg._id = arg._id || gen();
|
|
67
|
-
|
|
68
|
-
appendFileSync(file, data + "\n");
|
|
67
|
+
await this.fileCpu.add(file, arg);
|
|
69
68
|
return arg;
|
|
70
69
|
}
|
|
71
70
|
/**
|
|
@@ -82,7 +81,7 @@ class dbActionC {
|
|
|
82
81
|
let datas = [];
|
|
83
82
|
let totalEntries = 0;
|
|
84
83
|
for (let f of files) {
|
|
85
|
-
let data = await
|
|
84
|
+
let data = await this.fileCpu.find(cpath + f, arg, context, findOpts);
|
|
86
85
|
if (options.reverse)
|
|
87
86
|
data.reverse();
|
|
88
87
|
if (options.max !== -1) {
|
|
@@ -109,7 +108,7 @@ class dbActionC {
|
|
|
109
108
|
const cpath = this._getCollectionPath(collection);
|
|
110
109
|
const files = getSortedFiles(cpath).map(f => f.f);
|
|
111
110
|
for (let f of files) {
|
|
112
|
-
let data = await
|
|
111
|
+
let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
|
|
113
112
|
if (data)
|
|
114
113
|
return data;
|
|
115
114
|
}
|
|
@@ -120,28 +119,28 @@ class dbActionC {
|
|
|
120
119
|
*/
|
|
121
120
|
async update(collection, arg, updater, context = {}) {
|
|
122
121
|
this.checkCollection(collection);
|
|
123
|
-
return await
|
|
122
|
+
return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context);
|
|
124
123
|
}
|
|
125
124
|
/**
|
|
126
125
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
127
126
|
*/
|
|
128
127
|
async updateOne(collection, arg, updater, context = {}) {
|
|
129
128
|
this.checkCollection(collection);
|
|
130
|
-
return await
|
|
129
|
+
return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context, true);
|
|
131
130
|
}
|
|
132
131
|
/**
|
|
133
132
|
* Remove entries from the specified database based on search criteria.
|
|
134
133
|
*/
|
|
135
134
|
async remove(collection, arg, context = {}) {
|
|
136
135
|
this.checkCollection(collection);
|
|
137
|
-
return await
|
|
136
|
+
return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context);
|
|
138
137
|
}
|
|
139
138
|
/**
|
|
140
139
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
141
140
|
*/
|
|
142
141
|
async removeOne(collection, arg, context = {}) {
|
|
143
142
|
this.checkCollection(collection);
|
|
144
|
-
return await
|
|
143
|
+
return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context, true);
|
|
145
144
|
}
|
|
146
145
|
/**
|
|
147
146
|
* Removes a database collection from the file system.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import CollectionManager from "../CollectionManager.js";
|
|
2
|
-
import { Remote
|
|
3
|
-
import { Arg, Search, Updater } from "../types/arg";
|
|
2
|
+
import { Remote } from "./remote.js";
|
|
3
|
+
import { Arg, Search, Updater } from "../types/arg.js";
|
|
4
4
|
import { DbFindOpts, FindOpts } from "../types/options.js";
|
|
5
|
-
import { Context } from "../types/types";
|
|
5
|
+
import { Context } from "../types/types.js";
|
|
6
6
|
import Data from "../types/data.js";
|
|
7
7
|
/**
|
|
8
8
|
* Represents a database management class for performing CRUD operations.
|
|
@@ -15,7 +15,7 @@ declare class DataBaseRemote {
|
|
|
15
15
|
/**
|
|
16
16
|
* Make a request to the remote database.
|
|
17
17
|
*/
|
|
18
|
-
_request<T>(type: string,
|
|
18
|
+
_request<T>(type: string, params?: any[]): Promise<T>;
|
|
19
19
|
/**
|
|
20
20
|
* Create a new instance of a CollectionManager class.
|
|
21
21
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ky from "ky";
|
|
2
2
|
import CollectionManager from "../CollectionManager.js";
|
|
3
3
|
import serializeFunctions from "./function.js";
|
|
4
4
|
/**
|
|
@@ -14,25 +14,23 @@ class DataBaseRemote {
|
|
|
14
14
|
/**
|
|
15
15
|
* Make a request to the remote database.
|
|
16
16
|
*/
|
|
17
|
-
async _request(type,
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
17
|
+
async _request(type, params = []) {
|
|
18
|
+
const processed = serializeFunctions(params);
|
|
19
|
+
const data = {
|
|
20
|
+
db: this.remote.name,
|
|
21
|
+
params: processed.data,
|
|
22
|
+
keys: processed.keys
|
|
23
23
|
};
|
|
24
|
-
const res = await
|
|
25
|
-
|
|
26
|
-
body: data,
|
|
24
|
+
const res = await ky.post(this.remote.url + "/db/" + type, {
|
|
25
|
+
json: data,
|
|
27
26
|
headers: {
|
|
28
27
|
"Authorization": this.remote.auth
|
|
29
28
|
},
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return res.body.result;
|
|
29
|
+
throwHttpErrors: false
|
|
30
|
+
}).json();
|
|
31
|
+
if (res.err)
|
|
32
|
+
throw new Error(res.msg);
|
|
33
|
+
return res.result;
|
|
36
34
|
}
|
|
37
35
|
/**
|
|
38
36
|
* Create a new instance of a CollectionManager class.
|
|
@@ -44,97 +42,73 @@ class DataBaseRemote {
|
|
|
44
42
|
* Get the names of all available databases.
|
|
45
43
|
*/
|
|
46
44
|
async getCollections() {
|
|
47
|
-
return await this._request("getCollections",
|
|
45
|
+
return await this._request("getCollections", []);
|
|
48
46
|
}
|
|
49
47
|
/**
|
|
50
48
|
* Check and create the specified collection if it doesn't exist.
|
|
51
49
|
*/
|
|
52
50
|
async checkCollection(collection) {
|
|
53
|
-
return await this._request("checkCollection",
|
|
51
|
+
return await this._request("checkCollection", [collection]);
|
|
54
52
|
}
|
|
55
53
|
/**
|
|
56
54
|
* Check if a collection exists.
|
|
57
55
|
*/
|
|
58
56
|
async issetCollection(collection) {
|
|
59
|
-
return await this._request("issetCollection",
|
|
57
|
+
return await this._request("issetCollection", [collection]);
|
|
60
58
|
}
|
|
61
59
|
/**
|
|
62
60
|
* Add data to a database.
|
|
63
61
|
*/
|
|
64
62
|
async add(collection, data, id_gen = true) {
|
|
65
|
-
return await this._request("add",
|
|
63
|
+
return await this._request("add", [collection, data, id_gen]);
|
|
66
64
|
}
|
|
67
65
|
/**
|
|
68
66
|
* Find data in a database.
|
|
69
67
|
*/
|
|
70
68
|
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
71
|
-
|
|
72
|
-
const findOptsRemote = {};
|
|
73
|
-
if (findOpts.select)
|
|
74
|
-
findOptsRemote.select = findOpts.select;
|
|
75
|
-
if (findOpts.exclude)
|
|
76
|
-
findOptsRemote.exclude = findOpts.exclude;
|
|
77
|
-
if (findOpts.transform)
|
|
78
|
-
findOptsRemote.transform = findOpts.transform.toString();
|
|
79
|
-
return await this._request("find", { collection, search: searchStr, options, context, findOpts });
|
|
69
|
+
return await this._request("find", [collection, search, context, options, findOpts]);
|
|
80
70
|
}
|
|
81
71
|
/**
|
|
82
72
|
* Find one data entry in a database.
|
|
83
73
|
*/
|
|
84
74
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
85
|
-
|
|
86
|
-
const findOptsRemote = {};
|
|
87
|
-
if (findOpts.select)
|
|
88
|
-
findOptsRemote.select = findOpts.select;
|
|
89
|
-
if (findOpts.exclude)
|
|
90
|
-
findOptsRemote.exclude = findOpts.exclude;
|
|
91
|
-
if (findOpts.transform)
|
|
92
|
-
findOptsRemote.transform = findOpts.transform.toString();
|
|
93
|
-
return await this._request("findOne", { collection, search: searchStr, context, findOpts });
|
|
75
|
+
return await this._request("findOne", [collection, search, context, findOpts]);
|
|
94
76
|
}
|
|
95
77
|
/**
|
|
96
78
|
* Update data in a database.
|
|
97
79
|
*/
|
|
98
80
|
async update(collection, search, updater, context = {}) {
|
|
99
|
-
|
|
100
|
-
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
101
|
-
return await this._request("update", { collection, search: searchStr, arg: argStr, context });
|
|
81
|
+
return await this._request("update", [collection, search, updater, context]);
|
|
102
82
|
}
|
|
103
83
|
/**
|
|
104
84
|
* Update one data entry in a database.
|
|
105
85
|
*/
|
|
106
86
|
async updateOne(collection, search, updater, context = {}) {
|
|
107
|
-
|
|
108
|
-
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
109
|
-
return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
|
|
87
|
+
return await this._request("updateOne", [collection, search, updater, context]);
|
|
110
88
|
}
|
|
111
89
|
/**
|
|
112
90
|
* Remove data from a database.
|
|
113
91
|
*/
|
|
114
92
|
async remove(collection, search, context = {}) {
|
|
115
|
-
|
|
116
|
-
return await this._request("remove", { collection, search: searchStr, context });
|
|
93
|
+
return await this._request("remove", [collection, search, context]);
|
|
117
94
|
}
|
|
118
95
|
/**
|
|
119
96
|
* Remove one data entry from a database.
|
|
120
97
|
*/
|
|
121
98
|
async removeOne(collection, search, context = {}) {
|
|
122
|
-
|
|
123
|
-
return await this._request("removeOne", { collection, search: searchStr, context });
|
|
99
|
+
return await this._request("removeOne", [collection, search, context]);
|
|
124
100
|
}
|
|
125
101
|
/**
|
|
126
102
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
127
103
|
*/
|
|
128
104
|
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
129
|
-
|
|
130
|
-
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
131
|
-
return await this._request("updateOneOrAdd", { collection, search: searchStr, arg: argStr, add_arg, id_gen, context });
|
|
105
|
+
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg, id_gen, context]);
|
|
132
106
|
}
|
|
133
107
|
/**
|
|
134
108
|
* Removes a database collection from the file system.
|
|
135
109
|
*/
|
|
136
110
|
async removeCollection(name) {
|
|
137
|
-
return await this._request("removeCollection",
|
|
111
|
+
return await this._request("removeCollection", [name]);
|
|
138
112
|
}
|
|
139
113
|
}
|
|
140
114
|
export default DataBaseRemote;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Remote
|
|
1
|
+
import { Remote } from "./remote.js";
|
|
2
2
|
/**
|
|
3
3
|
* A class representing a graph database.
|
|
4
4
|
* Uses a remote database.
|
|
@@ -13,7 +13,7 @@ declare class GraphRemote {
|
|
|
13
13
|
/**
|
|
14
14
|
* Make a request to the remote database.
|
|
15
15
|
*/
|
|
16
|
-
_request(type: string,
|
|
16
|
+
_request(type: string, params?: any[]): Promise<any>;
|
|
17
17
|
/**
|
|
18
18
|
* Adds an edge between two nodes.
|
|
19
19
|
*/
|