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