@wxn0brp/db 0.0.5 → 0.0.7
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 +4 -1
- package/dist/cjs/CollectionManager.d.ts +44 -0
- package/dist/cjs/CollectionManager.js +59 -0
- package/dist/cjs/action.d.ts +66 -0
- package/dist/cjs/action.js +193 -0
- package/dist/cjs/client/database.d.ts +72 -0
- package/dist/cjs/client/database.js +145 -0
- package/dist/cjs/client/function.d.ts +5 -0
- package/dist/cjs/client/function.js +32 -0
- package/dist/cjs/client/graph.d.ts +54 -0
- package/dist/cjs/client/graph.js +93 -0
- package/dist/cjs/client/remote.d.ts +16 -0
- package/dist/cjs/client/remote.js +2 -0
- package/dist/cjs/database.d.ts +69 -0
- package/dist/cjs/database.js +110 -0
- package/dist/cjs/executor.d.ts +28 -0
- package/{executor.js → dist/cjs/executor.js} +47 -54
- package/dist/cjs/file/find.d.ts +11 -0
- package/dist/cjs/file/find.js +80 -0
- package/dist/cjs/file/index.d.ts +3 -0
- package/dist/cjs/file/index.js +25 -0
- package/dist/cjs/file/remove.d.ts +7 -0
- package/dist/cjs/file/remove.js +61 -0
- package/dist/cjs/file/update.d.ts +7 -0
- package/dist/cjs/file/update.js +70 -0
- package/dist/cjs/file/utils.d.ts +8 -0
- package/dist/cjs/file/utils.js +23 -0
- package/dist/cjs/format.d.ts +18 -0
- package/dist/cjs/format.js +36 -0
- package/dist/cjs/gen.d.ts +5 -0
- package/dist/cjs/gen.js +78 -0
- package/dist/cjs/graph.d.ts +47 -0
- package/dist/cjs/graph.js +90 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/relation.d.ts +64 -0
- package/{relation.js → dist/cjs/relation.js} +26 -45
- package/dist/cjs/types/Id.d.ts +3 -0
- package/dist/cjs/types/Id.js +2 -0
- package/dist/cjs/types/arg.d.ts +12 -0
- package/dist/cjs/types/arg.js +2 -0
- package/dist/cjs/types/data.d.ts +4 -0
- package/dist/cjs/types/data.js +2 -0
- package/dist/cjs/types/options.d.ts +12 -0
- package/dist/cjs/types/options.js +2 -0
- package/dist/cjs/types/searchOpts.d.ts +61 -0
- package/dist/cjs/types/searchOpts.js +8 -0
- package/dist/cjs/types/types.d.ts +6 -0
- package/dist/cjs/types/types.js +2 -0
- package/dist/cjs/types/updater.d.ts +26 -0
- package/dist/cjs/types/updater.js +5 -0
- package/dist/cjs/utils/hasFields.d.ts +8 -0
- package/dist/cjs/utils/hasFields.js +22 -0
- package/dist/cjs/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/cjs/utils/hasFieldsAdvanced.js +182 -0
- package/dist/cjs/utils/updateFindObject.d.ts +11 -0
- package/dist/cjs/utils/updateFindObject.js +32 -0
- package/dist/cjs/utils/updateObject.d.ts +7 -0
- package/dist/cjs/utils/updateObject.js +148 -0
- package/dist/esm/CollectionManager.d.ts +44 -0
- package/dist/esm/CollectionManager.js +57 -0
- package/dist/esm/action.d.ts +66 -0
- package/dist/esm/action.js +188 -0
- package/dist/esm/client/database.d.ts +72 -0
- package/dist/esm/client/database.js +140 -0
- package/dist/esm/client/function.d.ts +5 -0
- package/dist/esm/client/function.js +30 -0
- package/dist/esm/client/graph.d.ts +54 -0
- package/dist/esm/client/graph.js +88 -0
- package/dist/esm/client/remote.d.ts +16 -0
- package/dist/esm/client/remote.js +1 -0
- package/dist/esm/database.d.ts +69 -0
- package/dist/esm/database.js +105 -0
- package/dist/esm/executor.d.ts +28 -0
- package/dist/esm/executor.js +45 -0
- package/dist/esm/file/find.d.ts +11 -0
- package/dist/esm/file/find.js +73 -0
- package/dist/esm/file/index.d.ts +3 -0
- package/{file → dist/esm/file}/index.js +1 -1
- package/dist/esm/file/remove.d.ts +7 -0
- package/dist/esm/file/remove.js +56 -0
- package/dist/esm/file/update.d.ts +7 -0
- package/dist/esm/file/update.js +65 -0
- package/dist/esm/file/utils.d.ts +8 -0
- package/{file → dist/esm/file}/utils.js +3 -11
- package/dist/esm/format.d.ts +18 -0
- package/{format.js → dist/esm/format.js} +29 -29
- package/dist/esm/gen.d.ts +5 -0
- package/dist/esm/gen.js +75 -0
- package/dist/esm/graph.d.ts +47 -0
- package/dist/esm/graph.js +85 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/relation.d.ts +64 -0
- package/dist/esm/relation.js +65 -0
- package/dist/esm/types/Id.d.ts +3 -0
- package/dist/esm/types/Id.js +1 -0
- package/dist/esm/types/arg.d.ts +12 -0
- package/dist/esm/types/arg.js +1 -0
- package/dist/esm/types/data.d.ts +4 -0
- package/dist/esm/types/data.js +1 -0
- package/dist/esm/types/options.d.ts +12 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/searchOpts.d.ts +61 -0
- package/dist/esm/types/searchOpts.js +7 -0
- package/dist/esm/types/types.d.ts +6 -0
- package/dist/esm/types/types.js +1 -0
- package/dist/esm/types/updater.d.ts +26 -0
- package/dist/esm/types/updater.js +4 -0
- package/dist/esm/utils/hasFields.d.ts +8 -0
- package/{utils → dist/esm/utils}/hasFields.js +4 -4
- package/dist/esm/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/esm/utils/hasFieldsAdvanced.js +176 -0
- package/dist/esm/utils/updateFindObject.d.ts +11 -0
- package/{utils → dist/esm/utils}/updateFindObject.js +11 -16
- package/dist/esm/utils/updateObject.d.ts +7 -0
- package/dist/esm/utils/updateObject.js +145 -0
- package/package.json +52 -36
- package/CollectionManager.js +0 -119
- package/action.js +0 -258
- package/database.d.ts +0 -44
- package/database.js +0 -203
- package/docs/database.md +0 -140
- package/docs/graph.md +0 -86
- package/docs/relation.md +0 -51
- package/docs/remote.md +0 -30
- package/docs/remote_server.md +0 -35
- package/docs/search_opts.md +0 -227
- package/file/find.js +0 -89
- package/file/remove.js +0 -74
- package/file/update.js +0 -83
- package/gen.d.ts +0 -1
- package/gen.js +0 -97
- package/graph.d.ts +0 -27
- package/graph.js +0 -140
- package/index.d.ts +0 -7
- package/index.js +0 -15
- package/relation.d.ts +0 -23
- package/remote/client/database.d.ts +0 -41
- package/remote/client/database.js +0 -228
- package/remote/client/graph.d.ts +0 -31
- package/remote/client/graph.js +0 -148
- package/remote/server/auth.js +0 -100
- package/remote/server/db.js +0 -197
- package/remote/server/function.js +0 -43
- package/remote/server/graph.js +0 -136
- package/remote/server/gui/css/main.css +0 -130
- package/remote/server/gui/css/scrool.css +0 -81
- package/remote/server/gui/css/style.css +0 -61
- package/remote/server/gui/favicon.svg +0 -12
- package/remote/server/gui/html/data.html +0 -15
- package/remote/server/gui/html/main.html +0 -46
- package/remote/server/gui/html/nav.html +0 -25
- package/remote/server/gui/html/popup.html +0 -51
- package/remote/server/gui/index.html +0 -49
- package/remote/server/gui/js/api.js +0 -166
- package/remote/server/gui/js/index.js +0 -17
- package/remote/server/gui/js/loadHTML.js +0 -16
- package/remote/server/gui/js/popUp.js +0 -72
- package/remote/server/gui/js/queryApi.js +0 -51
- package/remote/server/gui/js/queryDb.js +0 -79
- package/remote/server/gui/js/queryGraph.js +0 -144
- package/remote/server/gui/js/render.js +0 -64
- package/remote/server/gui/js/templates.js +0 -31
- package/remote/server/gui/js/utils.js +0 -36
- package/remote/server/gui/js/vars.js +0 -9
- package/remote/server/gui/libs/core.js +0 -176
- package/remote/server/gui/libs/d3.v7.min.js +0 -2
- package/remote/server/gui/libs/handlebars.min.js +0 -29
- package/remote/server/gui/libs/json5.min.js +0 -1
- package/remote/server/index.js +0 -63
- package/remote/server/initDataBases.js +0 -20
- package/remote/server/pathUtils.js +0 -7
- package/remote/server/secret.js +0 -23
- package/remote/serverMgmt/index.js +0 -86
- package/test/hasFieldsAdvanced.test.js +0 -70
- package/utils/hasFieldsAdvanced.js +0 -184
- package/utils/updateObject.js +0 -15
package/README.md
CHANGED
|
@@ -20,11 +20,14 @@ import { DataBase, Graph, DataBaseRemote, GraphRemote, Relation, genId } from "@
|
|
|
20
20
|
|
|
21
21
|
## Documentation
|
|
22
22
|
|
|
23
|
+
Website: [https://wxn0brp.github.io/database/](https://wxn0brp.github.io/database/)
|
|
24
|
+
|
|
23
25
|
For detailed information, refer to the following resources:
|
|
24
26
|
|
|
25
27
|
- [DataBase Documentation](./docs/database.md)
|
|
26
28
|
- [Graph Documentation](./docs/graph.md)
|
|
27
29
|
- [Remote Database and Graph Client Documentation](./docs/remote.md)
|
|
28
|
-
- [Remote Server Documentation](./docs/remote_server.md)
|
|
29
30
|
- [Search Options Documentation](./docs/search_opts.md)
|
|
31
|
+
- [Find Options Documentation](./docs/find_opts.md)
|
|
32
|
+
- [Updater Options Documentation](./docs/updater.md)
|
|
30
33
|
- [Relation Documentation](./docs/relation.md)
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import DataBase from "./database.js";
|
|
2
|
+
import DataBaseRemote from "./client/database.js";
|
|
3
|
+
import { Arg, Search, Updater } from "./types/arg.js";
|
|
4
|
+
import { DbFindOpts, FindOpts } from "./types/options.js";
|
|
5
|
+
import { Context } from "./types/types.js";
|
|
6
|
+
import Data from "./types/data.js";
|
|
7
|
+
declare class CollectionManager {
|
|
8
|
+
db: DataBase | DataBaseRemote;
|
|
9
|
+
collection: string;
|
|
10
|
+
constructor(db: DataBase | DataBaseRemote, collection: string);
|
|
11
|
+
/**
|
|
12
|
+
* Add data to a database.
|
|
13
|
+
*/
|
|
14
|
+
add<T = Data>(data: Arg, id_gen?: boolean): Promise<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Find data in a database.
|
|
17
|
+
*/
|
|
18
|
+
find<T = Data>(search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Find one data entry in a database.
|
|
21
|
+
*/
|
|
22
|
+
findOne<T = Data>(search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Update data in a database.
|
|
25
|
+
*/
|
|
26
|
+
update(search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Update one data entry in a database.
|
|
29
|
+
*/
|
|
30
|
+
updateOne(search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Remove data from a database.
|
|
33
|
+
*/
|
|
34
|
+
remove(search: Search, context?: Context): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Remove one data entry from a database.
|
|
37
|
+
*/
|
|
38
|
+
removeOne(search: Search, context?: Context): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
41
|
+
*/
|
|
42
|
+
updateOneOrAdd(search: Search, arg: Search, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
|
|
43
|
+
}
|
|
44
|
+
export default CollectionManager;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class CollectionManager {
|
|
4
|
+
db;
|
|
5
|
+
collection;
|
|
6
|
+
constructor(db, collection) {
|
|
7
|
+
this.db = db;
|
|
8
|
+
this.collection = collection;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Add data to a database.
|
|
12
|
+
*/
|
|
13
|
+
async add(data, id_gen = true) {
|
|
14
|
+
return await this.db.add(this.collection, data, id_gen);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Find data in a database.
|
|
18
|
+
*/
|
|
19
|
+
async find(search, context = {}, options = {}, findOpts = {}) {
|
|
20
|
+
return await this.db.find(this.collection, search, context, options, findOpts);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Find one data entry in a database.
|
|
24
|
+
*/
|
|
25
|
+
async findOne(search, context = {}, findOpts = {}) {
|
|
26
|
+
return await this.db.findOne(this.collection, search, context, findOpts);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Update data in a database.
|
|
30
|
+
*/
|
|
31
|
+
async update(search, updater, context = {}) {
|
|
32
|
+
return await this.db.update(this.collection, search, updater, context);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Update one data entry in a database.
|
|
36
|
+
*/
|
|
37
|
+
async updateOne(search, updater, context = {}) {
|
|
38
|
+
return await this.db.updateOne(this.collection, search, updater, context);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Remove data from a database.
|
|
42
|
+
*/
|
|
43
|
+
async remove(search, context = {}) {
|
|
44
|
+
return await this.db.remove(this.collection, search, context);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Remove one data entry from a database.
|
|
48
|
+
*/
|
|
49
|
+
async removeOne(search, context = {}) {
|
|
50
|
+
return await this.db.removeOne(this.collection, search, context);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
54
|
+
*/
|
|
55
|
+
async updateOneOrAdd(search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
56
|
+
return await this.db.updateOneOrAdd(this.collection, search, arg, add_arg, context, id_gen);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.default = CollectionManager;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { Arg, Search, Updater } from "./types/arg.js";
|
|
2
|
+
import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
|
|
3
|
+
import { Context } from "./types/types";
|
|
4
|
+
import { SearchOptions } from "./types/searchOpts.js";
|
|
5
|
+
import Data from "./types/data.js";
|
|
6
|
+
/**
|
|
7
|
+
* A class representing database actions on files.
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
declare class dbActionC {
|
|
11
|
+
folder: string;
|
|
12
|
+
options: DbOpts;
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new instance of dbActionC.
|
|
15
|
+
* @constructor
|
|
16
|
+
* @param folder - The folder where database files are stored.
|
|
17
|
+
* @param options - The options object.
|
|
18
|
+
*/
|
|
19
|
+
constructor(folder: string, options: DbOpts);
|
|
20
|
+
_getCollectionPath(collection: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Get a list of available databases in the specified folder.
|
|
23
|
+
*/
|
|
24
|
+
getCollections(): string[];
|
|
25
|
+
/**
|
|
26
|
+
* Check and create the specified collection if it doesn't exist.
|
|
27
|
+
*/
|
|
28
|
+
checkCollection(collection: string): void;
|
|
29
|
+
/**
|
|
30
|
+
* Check if a collection exists.
|
|
31
|
+
*/
|
|
32
|
+
issetCollection(collection: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Add a new entry to the specified database.
|
|
35
|
+
*/
|
|
36
|
+
add(collection: string, arg: Arg, id_gen?: boolean): Promise<Arg>;
|
|
37
|
+
/**
|
|
38
|
+
* Find entries in the specified database based on search criteria.
|
|
39
|
+
*/
|
|
40
|
+
find(collection: string, arg: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<any[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Find the first matching entry in the specified database based on search criteria.
|
|
43
|
+
*/
|
|
44
|
+
findOne(collection: string, arg: SearchOptions, context?: Context, findOpts?: FindOpts): Promise<Data>;
|
|
45
|
+
/**
|
|
46
|
+
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
47
|
+
*/
|
|
48
|
+
update(collection: string, arg: Search, updater: Updater, context?: {}): Promise<boolean>;
|
|
49
|
+
/**
|
|
50
|
+
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
51
|
+
*/
|
|
52
|
+
updateOne(collection: string, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Remove entries from the specified database based on search criteria.
|
|
55
|
+
*/
|
|
56
|
+
remove(collection: string, arg: Search, context?: Context): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Remove the first matching entry from the specified database based on search criteria.
|
|
59
|
+
*/
|
|
60
|
+
removeOne(collection: string, arg: Search, context?: Context): Promise<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* Removes a database collection from the file system.
|
|
63
|
+
*/
|
|
64
|
+
removeCollection(collection: string): void;
|
|
65
|
+
}
|
|
66
|
+
export default dbActionC;
|
|
@@ -0,0 +1,193 @@
|
|
|
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 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
|
+
/**
|
|
11
|
+
* A class representing database actions on files.
|
|
12
|
+
* @class
|
|
13
|
+
*/
|
|
14
|
+
class dbActionC {
|
|
15
|
+
folder;
|
|
16
|
+
options;
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new instance of dbActionC.
|
|
19
|
+
* @constructor
|
|
20
|
+
* @param folder - The folder where database files are stored.
|
|
21
|
+
* @param options - The options object.
|
|
22
|
+
*/
|
|
23
|
+
constructor(folder, options) {
|
|
24
|
+
this.folder = folder;
|
|
25
|
+
this.options = {
|
|
26
|
+
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
27
|
+
...options,
|
|
28
|
+
};
|
|
29
|
+
if (!(0, fs_1.existsSync)(folder))
|
|
30
|
+
(0, fs_1.mkdirSync)(folder, { recursive: true });
|
|
31
|
+
}
|
|
32
|
+
_getCollectionPath(collection) {
|
|
33
|
+
return this.folder + "/" + collection + "/";
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get a list of available databases in the specified folder.
|
|
37
|
+
*/
|
|
38
|
+
getCollections() {
|
|
39
|
+
const collections = (0, fs_1.readdirSync)(this.folder, { recursive: true, withFileTypes: true })
|
|
40
|
+
.filter(dirent => dirent.isDirectory())
|
|
41
|
+
.map(dirent => {
|
|
42
|
+
if (dirent.parentPath === this.folder)
|
|
43
|
+
return dirent.name;
|
|
44
|
+
return dirent.parentPath.replace(this.folder + "/", "") + "/" + dirent.name;
|
|
45
|
+
});
|
|
46
|
+
return collections;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Check and create the specified collection if it doesn't exist.
|
|
50
|
+
*/
|
|
51
|
+
checkCollection(collection) {
|
|
52
|
+
const cpath = this._getCollectionPath(collection);
|
|
53
|
+
if (!(0, fs_1.existsSync)(cpath))
|
|
54
|
+
(0, fs_1.mkdirSync)(cpath, { recursive: true });
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Check if a collection exists.
|
|
58
|
+
*/
|
|
59
|
+
issetCollection(collection) {
|
|
60
|
+
const path = this.folder + "/" + collection;
|
|
61
|
+
return (0, fs_1.existsSync)(path);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Add a new entry to the specified database.
|
|
65
|
+
*/
|
|
66
|
+
async add(collection, arg, id_gen = true) {
|
|
67
|
+
this.checkCollection(collection);
|
|
68
|
+
const cpath = this._getCollectionPath(collection);
|
|
69
|
+
const file = cpath + getLastFile(cpath, this.options.maxFileSize);
|
|
70
|
+
if (id_gen)
|
|
71
|
+
arg._id = arg._id || (0, gen_js_1.default)();
|
|
72
|
+
const data = (0, format_js_1.stringify)(arg);
|
|
73
|
+
(0, fs_1.appendFileSync)(file, data + "\n");
|
|
74
|
+
return arg;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Find entries in the specified database based on search criteria.
|
|
78
|
+
*/
|
|
79
|
+
async find(collection, arg, context = {}, options = {}, findOpts = {}) {
|
|
80
|
+
options.reverse = options.reverse || false;
|
|
81
|
+
options.max = options.max || -1;
|
|
82
|
+
this.checkCollection(collection);
|
|
83
|
+
const cpath = this._getCollectionPath(collection);
|
|
84
|
+
const files = getSortedFiles(cpath).map(f => f.f);
|
|
85
|
+
if (options.reverse)
|
|
86
|
+
files.reverse();
|
|
87
|
+
let datas = [];
|
|
88
|
+
let totalEntries = 0;
|
|
89
|
+
for (let f of files) {
|
|
90
|
+
let data = await (0, index_js_1.find)(cpath + f, arg, context, findOpts);
|
|
91
|
+
if (options.reverse)
|
|
92
|
+
data.reverse();
|
|
93
|
+
if (options.max !== -1) {
|
|
94
|
+
if (totalEntries + data.length > options.max) {
|
|
95
|
+
let remainingEntries = options.max - totalEntries;
|
|
96
|
+
data = data.slice(0, remainingEntries);
|
|
97
|
+
totalEntries = options.max;
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
totalEntries += data.length;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
datas = datas.concat(data);
|
|
104
|
+
if (options.max !== -1 && totalEntries >= options.max)
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
return datas;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Find the first matching entry in the specified database based on search criteria.
|
|
111
|
+
*/
|
|
112
|
+
async findOne(collection, arg, context = {}, findOpts = {}) {
|
|
113
|
+
this.checkCollection(collection);
|
|
114
|
+
const cpath = this._getCollectionPath(collection);
|
|
115
|
+
const files = getSortedFiles(cpath).map(f => f.f);
|
|
116
|
+
for (let f of files) {
|
|
117
|
+
let data = await (0, index_js_1.findOne)(cpath + f, arg, context, findOpts);
|
|
118
|
+
if (data)
|
|
119
|
+
return data;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
125
|
+
*/
|
|
126
|
+
async update(collection, arg, updater, context = {}) {
|
|
127
|
+
this.checkCollection(collection);
|
|
128
|
+
return await (0, index_js_1.update)(this._getCollectionPath(collection), arg, updater, context);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
132
|
+
*/
|
|
133
|
+
async updateOne(collection, arg, updater, context = {}) {
|
|
134
|
+
this.checkCollection(collection);
|
|
135
|
+
return await (0, index_js_1.update)(this._getCollectionPath(collection), arg, updater, context, true);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Remove entries from the specified database based on search criteria.
|
|
139
|
+
*/
|
|
140
|
+
async remove(collection, arg, context = {}) {
|
|
141
|
+
this.checkCollection(collection);
|
|
142
|
+
return await (0, index_js_1.remove)(this._getCollectionPath(collection), arg, context);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Remove the first matching entry from the specified database based on search criteria.
|
|
146
|
+
*/
|
|
147
|
+
async removeOne(collection, arg, context = {}) {
|
|
148
|
+
this.checkCollection(collection);
|
|
149
|
+
return await (0, index_js_1.remove)(this._getCollectionPath(collection), arg, context, true);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Removes a database collection from the file system.
|
|
153
|
+
*/
|
|
154
|
+
removeCollection(collection) {
|
|
155
|
+
(0, fs_1.rmSync)(this.folder + "/" + collection, { recursive: true, force: true });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Get the last file in the specified directory.
|
|
160
|
+
*/
|
|
161
|
+
function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
162
|
+
if (!(0, fs_1.existsSync)(path))
|
|
163
|
+
(0, fs_1.mkdirSync)(path, { recursive: true });
|
|
164
|
+
const files = getSortedFiles(path);
|
|
165
|
+
if (files.length == 0) {
|
|
166
|
+
(0, fs_1.writeFileSync)(path + "/1.db", "");
|
|
167
|
+
return "1.db";
|
|
168
|
+
}
|
|
169
|
+
const last = files[files.length - 1];
|
|
170
|
+
const info = path + "/" + last.f;
|
|
171
|
+
if ((0, fs_1.statSync)(info).size < maxFileSize)
|
|
172
|
+
return last.f;
|
|
173
|
+
const num = last.i + 1;
|
|
174
|
+
(0, fs_1.writeFileSync)(path + "/" + num + ".db", "");
|
|
175
|
+
return num + ".db";
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Get all files in a directory sorted by name.
|
|
179
|
+
*/
|
|
180
|
+
function getSortedFiles(path) {
|
|
181
|
+
const files = (0, fs_1.readdirSync)(path).filter(file => file.endsWith(".db"));
|
|
182
|
+
if (files.length == 0)
|
|
183
|
+
return [];
|
|
184
|
+
const filesWithoutExt = files.map(file => parseInt(file.replace(".db", "")));
|
|
185
|
+
filesWithoutExt.sort();
|
|
186
|
+
return filesWithoutExt.map(file => {
|
|
187
|
+
return {
|
|
188
|
+
i: file,
|
|
189
|
+
f: file + ".db"
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
exports.default = dbActionC;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import CollectionManager from "../CollectionManager.js";
|
|
2
|
+
import { Remote, RequestData } from "./remote.js";
|
|
3
|
+
import { Arg, Search, Updater } from "../types/arg";
|
|
4
|
+
import { DbFindOpts, FindOpts } from "../types/options.js";
|
|
5
|
+
import { Context } from "../types/types";
|
|
6
|
+
import Data from "../types/data.js";
|
|
7
|
+
/**
|
|
8
|
+
* Represents a database management class for performing CRUD operations.
|
|
9
|
+
* Uses a remote database.
|
|
10
|
+
* @class
|
|
11
|
+
*/
|
|
12
|
+
declare class DataBaseRemote {
|
|
13
|
+
remote: Remote;
|
|
14
|
+
constructor(remote: Remote);
|
|
15
|
+
/**
|
|
16
|
+
* Make a request to the remote database.
|
|
17
|
+
*/
|
|
18
|
+
_request<T>(type: string, data: RequestData): Promise<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Create a new instance of a CollectionManager class.
|
|
21
|
+
*/
|
|
22
|
+
c(collection: string): CollectionManager;
|
|
23
|
+
/**
|
|
24
|
+
* Get the names of all available databases.
|
|
25
|
+
*/
|
|
26
|
+
getCollections(): Promise<string[]>;
|
|
27
|
+
/**
|
|
28
|
+
* Check and create the specified collection if it doesn't exist.
|
|
29
|
+
*/
|
|
30
|
+
checkCollection(collection: string): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Check if a collection exists.
|
|
33
|
+
*/
|
|
34
|
+
issetCollection(collection: string): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Add data to a database.
|
|
37
|
+
*/
|
|
38
|
+
add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
|
|
39
|
+
/**
|
|
40
|
+
* Find data in a database.
|
|
41
|
+
*/
|
|
42
|
+
find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
43
|
+
/**
|
|
44
|
+
* Find one data entry in a database.
|
|
45
|
+
*/
|
|
46
|
+
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
47
|
+
/**
|
|
48
|
+
* Update data in a database.
|
|
49
|
+
*/
|
|
50
|
+
update(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
51
|
+
/**
|
|
52
|
+
* Update one data entry in a database.
|
|
53
|
+
*/
|
|
54
|
+
updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
55
|
+
/**
|
|
56
|
+
* Remove data from a database.
|
|
57
|
+
*/
|
|
58
|
+
remove(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
59
|
+
/**
|
|
60
|
+
* Remove one data entry from a database.
|
|
61
|
+
*/
|
|
62
|
+
removeOne(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
63
|
+
/**
|
|
64
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
65
|
+
*/
|
|
66
|
+
updateOneOrAdd(collection: string, search: Search, arg: Search, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* Removes a database collection from the file system.
|
|
69
|
+
*/
|
|
70
|
+
removeCollection(name: string): Promise<boolean>;
|
|
71
|
+
}
|
|
72
|
+
export default DataBaseRemote;
|
|
@@ -0,0 +1,145 @@
|
|
|
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 got_1 = __importDefault(require("got"));
|
|
7
|
+
const CollectionManager_js_1 = __importDefault(require("../CollectionManager.js"));
|
|
8
|
+
const function_js_1 = __importDefault(require("./function.js"));
|
|
9
|
+
/**
|
|
10
|
+
* Represents a database management class for performing CRUD operations.
|
|
11
|
+
* Uses a remote database.
|
|
12
|
+
* @class
|
|
13
|
+
*/
|
|
14
|
+
class DataBaseRemote {
|
|
15
|
+
remote;
|
|
16
|
+
constructor(remote) {
|
|
17
|
+
this.remote = remote;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Make a request to the remote database.
|
|
21
|
+
*/
|
|
22
|
+
async _request(type, data) {
|
|
23
|
+
data.db = this.remote.name;
|
|
24
|
+
const processed = (0, function_js_1.default)(data);
|
|
25
|
+
data = {
|
|
26
|
+
keys: processed.keys,
|
|
27
|
+
...processed.data
|
|
28
|
+
};
|
|
29
|
+
const res = await got_1.default.post(this.remote.url + "/db/database/" + type, {
|
|
30
|
+
// @ts-ignore: Some jerk can't do the types correctly.
|
|
31
|
+
body: data,
|
|
32
|
+
headers: {
|
|
33
|
+
"Authorization": this.remote.auth
|
|
34
|
+
},
|
|
35
|
+
json: true,
|
|
36
|
+
responseType: "json"
|
|
37
|
+
});
|
|
38
|
+
if (res.body.err)
|
|
39
|
+
throw new Error(res.body.msg);
|
|
40
|
+
return res.body.result;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create a new instance of a CollectionManager class.
|
|
44
|
+
*/
|
|
45
|
+
c(collection) {
|
|
46
|
+
return new CollectionManager_js_1.default(this, collection);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the names of all available databases.
|
|
50
|
+
*/
|
|
51
|
+
async getCollections() {
|
|
52
|
+
return await this._request("getCollections", {});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Check and create the specified collection if it doesn't exist.
|
|
56
|
+
*/
|
|
57
|
+
async checkCollection(collection) {
|
|
58
|
+
return await this._request("checkCollection", { collection });
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Check if a collection exists.
|
|
62
|
+
*/
|
|
63
|
+
async issetCollection(collection) {
|
|
64
|
+
return await this._request("issetCollection", { collection });
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Add data to a database.
|
|
68
|
+
*/
|
|
69
|
+
async add(collection, data, id_gen = true) {
|
|
70
|
+
return await this._request("add", { collection, data, id_gen });
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Find data in a database.
|
|
74
|
+
*/
|
|
75
|
+
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
76
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
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 });
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Find one data entry in a database.
|
|
88
|
+
*/
|
|
89
|
+
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
90
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
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 });
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Update data in a database.
|
|
102
|
+
*/
|
|
103
|
+
async update(collection, search, updater, context = {}) {
|
|
104
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
105
|
+
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
106
|
+
return await this._request("update", { collection, search: searchStr, arg: argStr, context });
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Update one data entry in a database.
|
|
110
|
+
*/
|
|
111
|
+
async updateOne(collection, search, updater, context = {}) {
|
|
112
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
113
|
+
const argStr = typeof updater === "function" ? updater.toString() : updater;
|
|
114
|
+
return await this._request("updateOne", { collection, search: searchStr, arg: argStr, context });
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Remove data from a database.
|
|
118
|
+
*/
|
|
119
|
+
async remove(collection, search, context = {}) {
|
|
120
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
121
|
+
return await this._request("remove", { collection, search: searchStr, context });
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Remove one data entry from a database.
|
|
125
|
+
*/
|
|
126
|
+
async removeOne(collection, search, context = {}) {
|
|
127
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
128
|
+
return await this._request("removeOne", { collection, search: searchStr, context });
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
132
|
+
*/
|
|
133
|
+
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
134
|
+
const searchStr = typeof search === "function" ? search.toString() : search;
|
|
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 });
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Removes a database collection from the file system.
|
|
140
|
+
*/
|
|
141
|
+
async removeCollection(name) {
|
|
142
|
+
return await this._request("removeCollection", { name });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
exports.default = DataBaseRemote;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
function serializeFunctions(data) {
|
|
4
|
+
const functionKeys = [];
|
|
5
|
+
function convertFunctionToString(fn) {
|
|
6
|
+
return typeof fn === "function" ? fn.toString() : fn;
|
|
7
|
+
}
|
|
8
|
+
function traverseAndSerialize(obj, path = "") {
|
|
9
|
+
Object.keys(obj).forEach((key) => {
|
|
10
|
+
const value = obj[key];
|
|
11
|
+
const fullPath = path ? `${path}.${key.replace(/\./g, "[dot]")}` : key;
|
|
12
|
+
if (typeof value === "function") {
|
|
13
|
+
functionKeys.push(fullPath);
|
|
14
|
+
obj[key] = convertFunctionToString(value);
|
|
15
|
+
}
|
|
16
|
+
else if (Array.isArray(value)) {
|
|
17
|
+
value.forEach((item, index) => {
|
|
18
|
+
if (typeof item === "function") {
|
|
19
|
+
functionKeys.push(`${fullPath}[${index}]`);
|
|
20
|
+
value[index] = convertFunctionToString(item);
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
else if (typeof value === "object" && value !== null) {
|
|
25
|
+
traverseAndSerialize(value, fullPath);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
traverseAndSerialize(data);
|
|
30
|
+
return { data, keys: functionKeys };
|
|
31
|
+
}
|
|
32
|
+
exports.default = serializeFunctions;
|