@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/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# @wxn0brp/db
|
|
1
|
+
# ValtheraDB (@wxn0brp/db)
|
|
2
2
|
|
|
3
3
|
A lightweight file-based database management system that supports CRUD operations, custom queries, and graph structures.
|
|
4
4
|
|
|
@@ -20,7 +20,7 @@ import { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId } from "@
|
|
|
20
20
|
|
|
21
21
|
## Documentation
|
|
22
22
|
|
|
23
|
-
Website: [https://wxn0brp.github.io/
|
|
23
|
+
Website: [https://wxn0brp.github.io/ValtheraDB/](https://wxn0brp.github.io/ValtheraDB/)
|
|
24
24
|
|
|
25
25
|
For detailed information, refer to the following resources:
|
|
26
26
|
|
|
@@ -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;
|
|
@@ -52,8 +52,8 @@ class CollectionManager {
|
|
|
52
52
|
/**
|
|
53
53
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
54
54
|
*/
|
|
55
|
-
async updateOneOrAdd(search,
|
|
56
|
-
return await this.db.updateOneOrAdd(this.collection, search,
|
|
55
|
+
async updateOneOrAdd(search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
56
|
+
return await this.db.updateOneOrAdd(this.collection, search, updater, add_arg, context, id_gen);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
exports.default = CollectionManager;
|
package/dist/cjs/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/cjs/action.js
CHANGED
|
@@ -3,10 +3,8 @@ 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 gen_1 = __importDefault(require("./gen.js"));
|
|
6
7
|
const fs_1 = require("fs");
|
|
7
|
-
const gen_js_1 = __importDefault(require("./gen.js"));
|
|
8
|
-
const format_js_1 = require("./format.js");
|
|
9
|
-
const index_js_1 = require("./file/index.js");
|
|
10
8
|
/**
|
|
11
9
|
* A class representing database actions on files.
|
|
12
10
|
* @class
|
|
@@ -14,18 +12,20 @@ const index_js_1 = require("./file/index.js");
|
|
|
14
12
|
class dbActionC {
|
|
15
13
|
folder;
|
|
16
14
|
options;
|
|
15
|
+
fileCpu;
|
|
17
16
|
/**
|
|
18
17
|
* Creates a new instance of dbActionC.
|
|
19
18
|
* @constructor
|
|
20
19
|
* @param folder - The folder where database files are stored.
|
|
21
20
|
* @param options - The options object.
|
|
22
21
|
*/
|
|
23
|
-
constructor(folder, options) {
|
|
22
|
+
constructor(folder, options, fileCpu) {
|
|
24
23
|
this.folder = folder;
|
|
25
24
|
this.options = {
|
|
26
25
|
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
27
26
|
...options,
|
|
28
27
|
};
|
|
28
|
+
this.fileCpu = fileCpu;
|
|
29
29
|
if (!(0, fs_1.existsSync)(folder))
|
|
30
30
|
(0, fs_1.mkdirSync)(folder, { recursive: true });
|
|
31
31
|
}
|
|
@@ -68,9 +68,8 @@ class dbActionC {
|
|
|
68
68
|
const cpath = this._getCollectionPath(collection);
|
|
69
69
|
const file = cpath + getLastFile(cpath, this.options.maxFileSize);
|
|
70
70
|
if (id_gen)
|
|
71
|
-
arg._id = arg._id || (0,
|
|
72
|
-
|
|
73
|
-
(0, fs_1.appendFileSync)(file, data + "\n");
|
|
71
|
+
arg._id = arg._id || (0, gen_1.default)();
|
|
72
|
+
await this.fileCpu.add(file, arg);
|
|
74
73
|
return arg;
|
|
75
74
|
}
|
|
76
75
|
/**
|
|
@@ -87,7 +86,7 @@ class dbActionC {
|
|
|
87
86
|
let datas = [];
|
|
88
87
|
let totalEntries = 0;
|
|
89
88
|
for (let f of files) {
|
|
90
|
-
let data = await
|
|
89
|
+
let data = await this.fileCpu.find(cpath + f, arg, context, findOpts);
|
|
91
90
|
if (options.reverse)
|
|
92
91
|
data.reverse();
|
|
93
92
|
if (options.max !== -1) {
|
|
@@ -114,7 +113,7 @@ class dbActionC {
|
|
|
114
113
|
const cpath = this._getCollectionPath(collection);
|
|
115
114
|
const files = getSortedFiles(cpath).map(f => f.f);
|
|
116
115
|
for (let f of files) {
|
|
117
|
-
let data = await
|
|
116
|
+
let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
|
|
118
117
|
if (data)
|
|
119
118
|
return data;
|
|
120
119
|
}
|
|
@@ -125,28 +124,28 @@ class dbActionC {
|
|
|
125
124
|
*/
|
|
126
125
|
async update(collection, arg, updater, context = {}) {
|
|
127
126
|
this.checkCollection(collection);
|
|
128
|
-
return await
|
|
127
|
+
return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context);
|
|
129
128
|
}
|
|
130
129
|
/**
|
|
131
130
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
132
131
|
*/
|
|
133
132
|
async updateOne(collection, arg, updater, context = {}) {
|
|
134
133
|
this.checkCollection(collection);
|
|
135
|
-
return await
|
|
134
|
+
return await this.fileCpu.update(this._getCollectionPath(collection), arg, updater, context, true);
|
|
136
135
|
}
|
|
137
136
|
/**
|
|
138
137
|
* Remove entries from the specified database based on search criteria.
|
|
139
138
|
*/
|
|
140
139
|
async remove(collection, arg, context = {}) {
|
|
141
140
|
this.checkCollection(collection);
|
|
142
|
-
return await
|
|
141
|
+
return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context);
|
|
143
142
|
}
|
|
144
143
|
/**
|
|
145
144
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
146
145
|
*/
|
|
147
146
|
async removeOne(collection, arg, context = {}) {
|
|
148
147
|
this.checkCollection(collection);
|
|
149
|
-
return await
|
|
148
|
+
return await this.fileCpu.remove(this._getCollectionPath(collection), arg, context, true);
|
|
150
149
|
}
|
|
151
150
|
/**
|
|
152
151
|
* 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
|
*/
|
|
@@ -3,9 +3,9 @@ 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
|
|
7
|
-
const
|
|
8
|
-
const
|
|
6
|
+
const ky_1 = __importDefault(require("ky"));
|
|
7
|
+
const CollectionManager_1 = __importDefault(require("../CollectionManager.js"));
|
|
8
|
+
const function_1 = __importDefault(require("./function.js"));
|
|
9
9
|
/**
|
|
10
10
|
* Represents a database management class for performing CRUD operations.
|
|
11
11
|
* Uses a remote database.
|
|
@@ -19,127 +19,101 @@ class DataBaseRemote {
|
|
|
19
19
|
/**
|
|
20
20
|
* Make a request to the remote database.
|
|
21
21
|
*/
|
|
22
|
-
async _request(type,
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
async _request(type, params = []) {
|
|
23
|
+
const processed = (0, function_1.default)(params);
|
|
24
|
+
const data = {
|
|
25
|
+
db: this.remote.name,
|
|
26
|
+
params: processed.data,
|
|
27
|
+
keys: processed.keys
|
|
28
28
|
};
|
|
29
|
-
const res = await
|
|
30
|
-
|
|
31
|
-
body: data,
|
|
29
|
+
const res = await ky_1.default.post(this.remote.url + "/db/" + type, {
|
|
30
|
+
json: data,
|
|
32
31
|
headers: {
|
|
33
32
|
"Authorization": this.remote.auth
|
|
34
33
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return res.body.result;
|
|
34
|
+
throwHttpErrors: false
|
|
35
|
+
}).json();
|
|
36
|
+
if (res.err)
|
|
37
|
+
throw new Error(res.msg);
|
|
38
|
+
return res.result;
|
|
41
39
|
}
|
|
42
40
|
/**
|
|
43
41
|
* Create a new instance of a CollectionManager class.
|
|
44
42
|
*/
|
|
45
43
|
c(collection) {
|
|
46
|
-
return new
|
|
44
|
+
return new CollectionManager_1.default(this, collection);
|
|
47
45
|
}
|
|
48
46
|
/**
|
|
49
47
|
* Get the names of all available databases.
|
|
50
48
|
*/
|
|
51
49
|
async getCollections() {
|
|
52
|
-
return await this._request("getCollections",
|
|
50
|
+
return await this._request("getCollections", []);
|
|
53
51
|
}
|
|
54
52
|
/**
|
|
55
53
|
* Check and create the specified collection if it doesn't exist.
|
|
56
54
|
*/
|
|
57
55
|
async checkCollection(collection) {
|
|
58
|
-
return await this._request("checkCollection",
|
|
56
|
+
return await this._request("checkCollection", [collection]);
|
|
59
57
|
}
|
|
60
58
|
/**
|
|
61
59
|
* Check if a collection exists.
|
|
62
60
|
*/
|
|
63
61
|
async issetCollection(collection) {
|
|
64
|
-
return await this._request("issetCollection",
|
|
62
|
+
return await this._request("issetCollection", [collection]);
|
|
65
63
|
}
|
|
66
64
|
/**
|
|
67
65
|
* Add data to a database.
|
|
68
66
|
*/
|
|
69
67
|
async add(collection, data, id_gen = true) {
|
|
70
|
-
return await this._request("add",
|
|
68
|
+
return await this._request("add", [collection, data, id_gen]);
|
|
71
69
|
}
|
|
72
70
|
/**
|
|
73
71
|
* Find data in a database.
|
|
74
72
|
*/
|
|
75
73
|
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
76
|
-
|
|
77
|
-
const findOptsRemote = {};
|
|
78
|
-
if (findOpts.select)
|
|
79
|
-
findOptsRemote.select = findOpts.select;
|
|
80
|
-
if (findOpts.exclude)
|
|
81
|
-
findOptsRemote.exclude = findOpts.exclude;
|
|
82
|
-
if (findOpts.transform)
|
|
83
|
-
findOptsRemote.transform = findOpts.transform.toString();
|
|
84
|
-
return await this._request("find", { collection, search: searchStr, options, context, findOpts });
|
|
74
|
+
return await this._request("find", [collection, search, context, options, findOpts]);
|
|
85
75
|
}
|
|
86
76
|
/**
|
|
87
77
|
* Find one data entry in a database.
|
|
88
78
|
*/
|
|
89
79
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
90
|
-
|
|
91
|
-
const findOptsRemote = {};
|
|
92
|
-
if (findOpts.select)
|
|
93
|
-
findOptsRemote.select = findOpts.select;
|
|
94
|
-
if (findOpts.exclude)
|
|
95
|
-
findOptsRemote.exclude = findOpts.exclude;
|
|
96
|
-
if (findOpts.transform)
|
|
97
|
-
findOptsRemote.transform = findOpts.transform.toString();
|
|
98
|
-
return await this._request("findOne", { collection, search: searchStr, context, findOpts });
|
|
80
|
+
return await this._request("findOne", [collection, search, context, findOpts]);
|
|
99
81
|
}
|
|
100
82
|
/**
|
|
101
83
|
* Update data in a database.
|
|
102
84
|
*/
|
|
103
85
|
async update(collection, search, updater, context = {}) {
|
|
104
|
-
|
|
105
|
-
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
106
|
-
return await this._request("update", { collection, search: searchStr, arg: argStr, context });
|
|
86
|
+
return await this._request("update", [collection, search, updater, context]);
|
|
107
87
|
}
|
|
108
88
|
/**
|
|
109
89
|
* Update one data entry in a database.
|
|
110
90
|
*/
|
|
111
91
|
async updateOne(collection, search, updater, context = {}) {
|
|
112
|
-
|
|
113
|
-
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
114
|
-
return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
|
|
92
|
+
return await this._request("updateOne", [collection, search, updater, context]);
|
|
115
93
|
}
|
|
116
94
|
/**
|
|
117
95
|
* Remove data from a database.
|
|
118
96
|
*/
|
|
119
97
|
async remove(collection, search, context = {}) {
|
|
120
|
-
|
|
121
|
-
return await this._request("remove", { collection, search: searchStr, context });
|
|
98
|
+
return await this._request("remove", [collection, search, context]);
|
|
122
99
|
}
|
|
123
100
|
/**
|
|
124
101
|
* Remove one data entry from a database.
|
|
125
102
|
*/
|
|
126
103
|
async removeOne(collection, search, context = {}) {
|
|
127
|
-
|
|
128
|
-
return await this._request("removeOne", { collection, search: searchStr, context });
|
|
104
|
+
return await this._request("removeOne", [collection, search, context]);
|
|
129
105
|
}
|
|
130
106
|
/**
|
|
131
107
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
132
108
|
*/
|
|
133
109
|
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
134
|
-
|
|
135
|
-
const argStr = typeof arg === "function" ? arg.toString() : arg;
|
|
136
|
-
return await this._request("updateOneOrAdd", { collection, search: searchStr, arg: argStr, add_arg, id_gen, context });
|
|
110
|
+
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg, id_gen, context]);
|
|
137
111
|
}
|
|
138
112
|
/**
|
|
139
113
|
* Removes a database collection from the file system.
|
|
140
114
|
*/
|
|
141
115
|
async removeCollection(name) {
|
|
142
|
-
return await this._request("removeCollection",
|
|
116
|
+
return await this._request("removeCollection", [name]);
|
|
143
117
|
}
|
|
144
118
|
}
|
|
145
119
|
exports.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
|
*/
|
package/dist/cjs/client/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 ky_1 = __importDefault(require("ky"));
|
|
7
7
|
/**
|
|
8
8
|
* A class representing a graph database.
|
|
9
9
|
* Uses a remote database.
|
|
@@ -20,74 +20,75 @@ class GraphRemote {
|
|
|
20
20
|
/**
|
|
21
21
|
* Make a request to the remote database.
|
|
22
22
|
*/
|
|
23
|
-
async _request(type,
|
|
24
|
-
data
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
async _request(type, params = []) {
|
|
24
|
+
const data = {
|
|
25
|
+
db: this.remote.name,
|
|
26
|
+
params
|
|
27
|
+
};
|
|
28
|
+
const res = await ky_1.default.post(this.remote.url + "/db/" + type, {
|
|
29
|
+
json: data,
|
|
28
30
|
headers: {
|
|
29
31
|
"Authorization": this.remote.auth
|
|
30
32
|
},
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
return res.body.result;
|
|
33
|
+
throwHttpErrors: false
|
|
34
|
+
}).json();
|
|
35
|
+
if (res.err)
|
|
36
|
+
throw new Error(res.msg);
|
|
37
|
+
return res.result;
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Adds an edge between two nodes.
|
|
40
41
|
*/
|
|
41
42
|
async add(collection, nodeA, nodeB) {
|
|
42
|
-
return await this._request("add",
|
|
43
|
+
return await this._request("add", [collection, nodeA, nodeB]);
|
|
43
44
|
}
|
|
44
45
|
/**
|
|
45
46
|
* Removes an edge between two nodes.
|
|
46
47
|
*/
|
|
47
48
|
async remove(collection, nodeA, nodeB) {
|
|
48
|
-
return await this._request("remove",
|
|
49
|
+
return await this._request("remove", [collection, nodeA, nodeB]);
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
51
52
|
* Finds all edges with either node equal to `node`.
|
|
52
53
|
*/
|
|
53
54
|
async find(collection, node) {
|
|
54
|
-
return await this._request("find",
|
|
55
|
+
return await this._request("find", [collection, node]);
|
|
55
56
|
}
|
|
56
57
|
/**
|
|
57
58
|
* Finds one edge with either node equal to `nodeA` and the other equal to `nodeB`.
|
|
58
59
|
*/
|
|
59
60
|
async findOne(collection, nodeA, nodeB) {
|
|
60
|
-
return await this._request("findOne",
|
|
61
|
+
return await this._request("findOne", [collection, nodeA, nodeB]);
|
|
61
62
|
}
|
|
62
63
|
/**
|
|
63
64
|
* Get all edges in the collection.
|
|
64
65
|
*/
|
|
65
66
|
async getAll(collection) {
|
|
66
|
-
return await this._request("getAll",
|
|
67
|
+
return await this._request("getAll", [collection]);
|
|
67
68
|
}
|
|
68
69
|
/**
|
|
69
70
|
* Get the names of all available databases.
|
|
70
71
|
*/
|
|
71
72
|
async getCollections() {
|
|
72
|
-
return await this._request("getCollections",
|
|
73
|
+
return await this._request("getCollections", []);
|
|
73
74
|
}
|
|
74
75
|
/**
|
|
75
76
|
* Check and create the specified collection if it doesn't exist.
|
|
76
77
|
*/
|
|
77
78
|
async checkCollection(collection) {
|
|
78
|
-
return await this._request("checkCollection",
|
|
79
|
+
return await this._request("checkCollection", [collection]);
|
|
79
80
|
}
|
|
80
81
|
/**
|
|
81
82
|
* Check if a collection exists.
|
|
82
83
|
*/
|
|
83
84
|
async issetCollection(collection) {
|
|
84
|
-
return await this._request("issetCollection",
|
|
85
|
+
return await this._request("issetCollection", [collection]);
|
|
85
86
|
}
|
|
86
87
|
/**
|
|
87
88
|
* Remove the specified collection.
|
|
88
89
|
*/
|
|
89
90
|
removeCollection(collection) {
|
|
90
|
-
return this._request("removeCollection",
|
|
91
|
+
return this._request("removeCollection", [collection]);
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
exports.default = GraphRemote;
|
package/dist/cjs/database.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
|
|
|
5
5
|
import { Arg, Search, Updater } from "./types/arg.js";
|
|
6
6
|
import Data from "./types/data.js";
|
|
7
7
|
import { Context } from "./types/types.js";
|
|
8
|
+
import FileCpu from "./types/fileCpu.js";
|
|
8
9
|
/**
|
|
9
10
|
* Represents a database management class for performing CRUD operations.
|
|
10
11
|
* @class
|
|
@@ -12,7 +13,7 @@ import { Context } from "./types/types.js";
|
|
|
12
13
|
declare class DataBase {
|
|
13
14
|
dbAction: dbActionC;
|
|
14
15
|
executor: executorC;
|
|
15
|
-
constructor(folder: string, options?: DbOpts);
|
|
16
|
+
constructor(folder: string, options?: DbOpts, fileCpu?: FileCpu);
|
|
16
17
|
/**
|
|
17
18
|
* Create a new instance of a CollectionManager class.
|
|
18
19
|
*/
|
package/dist/cjs/database.js
CHANGED
|
@@ -3,9 +3,10 @@ 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
|
|
7
|
-
const
|
|
8
|
-
const
|
|
6
|
+
const action_1 = __importDefault(require("./action.js"));
|
|
7
|
+
const executor_1 = __importDefault(require("./executor.js"));
|
|
8
|
+
const CollectionManager_1 = __importDefault(require("./CollectionManager.js"));
|
|
9
|
+
const file_1 = __importDefault(require("./file/index.js"));
|
|
9
10
|
/**
|
|
10
11
|
* Represents a database management class for performing CRUD operations.
|
|
11
12
|
* @class
|
|
@@ -13,15 +14,17 @@ const CollectionManager_js_1 = __importDefault(require("./CollectionManager.js")
|
|
|
13
14
|
class DataBase {
|
|
14
15
|
dbAction;
|
|
15
16
|
executor;
|
|
16
|
-
constructor(folder, options = {}) {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
constructor(folder, options = {}, fileCpu) {
|
|
18
|
+
if (!fileCpu)
|
|
19
|
+
fileCpu = file_1.default;
|
|
20
|
+
this.dbAction = new action_1.default(folder, options, fileCpu);
|
|
21
|
+
this.executor = new executor_1.default();
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Create a new instance of a CollectionManager class.
|
|
22
25
|
*/
|
|
23
26
|
c(collection) {
|
|
24
|
-
return new
|
|
27
|
+
return new CollectionManager_1.default(this, collection);
|
|
25
28
|
}
|
|
26
29
|
/**
|
|
27
30
|
* Get the names of all available databases.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context } from "vm";
|
|
2
|
+
import { Search, Updater } from "../types/arg.js";
|
|
3
|
+
import Data from "../types/data.js";
|
|
4
|
+
import FileCpu from "../types/fileCpu.js";
|
|
5
|
+
import { FindOpts } from "../types/options.js";
|
|
6
|
+
type WriteFile = (file: string, data: any[]) => Promise<void>;
|
|
7
|
+
type ReadFile = (file: string) => Promise<any[]>;
|
|
8
|
+
declare class CustomFileCpu implements FileCpu {
|
|
9
|
+
_readFile: ReadFile;
|
|
10
|
+
_writeFile: WriteFile;
|
|
11
|
+
constructor(readFile: ReadFile, writeFile: WriteFile);
|
|
12
|
+
add(file: string, data: Data): Promise<void>;
|
|
13
|
+
addMany(file: string, data: Data[]): Promise<void>;
|
|
14
|
+
find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
|
|
15
|
+
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
16
|
+
remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
|
|
17
|
+
update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
export default CustomFileCpu;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const hasFieldsAdvanced_1 = __importDefault(require("../utils/hasFieldsAdvanced.js"));
|
|
7
|
+
const updateFindObject_1 = __importDefault(require("../utils/updateFindObject.js"));
|
|
8
|
+
const utils_1 = require("./utils.js");
|
|
9
|
+
class CustomFileCpu {
|
|
10
|
+
_readFile;
|
|
11
|
+
_writeFile;
|
|
12
|
+
constructor(readFile, writeFile) {
|
|
13
|
+
this._readFile = readFile;
|
|
14
|
+
this._writeFile = writeFile;
|
|
15
|
+
}
|
|
16
|
+
async add(file, data) {
|
|
17
|
+
file = (0, utils_1.pathRepair)(file);
|
|
18
|
+
let entries = await this._readFile(file);
|
|
19
|
+
entries.push(data);
|
|
20
|
+
await this._writeFile(file, entries);
|
|
21
|
+
}
|
|
22
|
+
async addMany(file, data) {
|
|
23
|
+
file = (0, utils_1.pathRepair)(file);
|
|
24
|
+
let entries = await this._readFile(file);
|
|
25
|
+
entries = entries.concat(data);
|
|
26
|
+
await this._writeFile(file, entries);
|
|
27
|
+
}
|
|
28
|
+
async find(file, arg, context = {}, findOpts = {}) {
|
|
29
|
+
file = (0, utils_1.pathRepair)(file);
|
|
30
|
+
const entries = await this._readFile(file);
|
|
31
|
+
const results = entries.filter(entry => typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg));
|
|
32
|
+
return results.length ? results.map(res => (0, updateFindObject_1.default)(res, findOpts)) : false;
|
|
33
|
+
}
|
|
34
|
+
async findOne(file, arg, context = {}, findOpts = {}) {
|
|
35
|
+
file = (0, utils_1.pathRepair)(file);
|
|
36
|
+
const entries = await this._readFile(file);
|
|
37
|
+
const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg));
|
|
38
|
+
return result ? (0, updateFindObject_1.default)(result, findOpts) : false;
|
|
39
|
+
}
|
|
40
|
+
async remove(cpath, arg, context = {}, one = false) {
|
|
41
|
+
const file = (0, utils_1.pathRepair)(cpath);
|
|
42
|
+
let entries = await this._readFile(file);
|
|
43
|
+
let removed = false;
|
|
44
|
+
entries = entries.filter(entry => {
|
|
45
|
+
if (removed && one)
|
|
46
|
+
return true;
|
|
47
|
+
let match = typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg);
|
|
48
|
+
if (match) {
|
|
49
|
+
removed = true;
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
return true;
|
|
53
|
+
});
|
|
54
|
+
if (!removed)
|
|
55
|
+
return false;
|
|
56
|
+
await this._writeFile(file, entries);
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
async update(cpath, arg, updater, context = {}, one = false) {
|
|
60
|
+
const file = (0, utils_1.pathRepair)(cpath);
|
|
61
|
+
let entries = await this._readFile(file);
|
|
62
|
+
let updated = false;
|
|
63
|
+
entries = entries.map(entry => {
|
|
64
|
+
if (updated && one)
|
|
65
|
+
return entry;
|
|
66
|
+
let match = typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg);
|
|
67
|
+
if (match) {
|
|
68
|
+
updated = true;
|
|
69
|
+
return typeof updater === "function" ? updater(entry, context) : { ...entry, ...updater };
|
|
70
|
+
}
|
|
71
|
+
return entry;
|
|
72
|
+
});
|
|
73
|
+
if (!updated)
|
|
74
|
+
return false;
|
|
75
|
+
await this._writeFile(file, entries);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
exports.default = CustomFileCpu;
|
package/dist/cjs/file/find.d.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { FindOpts } from "../types/options.js";
|
|
|
4
4
|
/**
|
|
5
5
|
* Asynchronously finds entries in a file based on search criteria.
|
|
6
6
|
*/
|
|
7
|
-
export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<
|
|
7
|
+
export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
|
|
8
8
|
/**
|
|
9
9
|
* Asynchronously finds one entry in a file based on search criteria.
|
|
10
10
|
*/
|
|
11
|
-
export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<
|
|
11
|
+
export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|