@wxn0brp/db-core 0.0.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/.github/workflows/build-relase.yml +25 -0
- package/.github/workflows/build.yml +20 -0
- package/.github/workflows/types.yml +89 -0
- package/CHANGELOG.md +9 -0
- package/LICENSE +21 -0
- package/README.md +5 -0
- package/dist/base/actions.d.ts +17 -0
- package/dist/base/actions.js +56 -0
- package/dist/base/db.d.ts +21 -0
- package/dist/base/db.js +56 -0
- package/dist/customFileCpu.d.ts +19 -0
- package/dist/customFileCpu.js +70 -0
- package/dist/db/memory.d.ts +72 -0
- package/dist/db/memory.js +145 -0
- package/dist/db/valthera.d.ts +74 -0
- package/dist/db/valthera.js +107 -0
- package/dist/helpers/CollectionManager.d.ts +43 -0
- package/dist/helpers/CollectionManager.js +57 -0
- package/dist/helpers/eventEmiter.d.ts +31 -0
- package/dist/helpers/eventEmiter.js +65 -0
- package/dist/helpers/executor.d.ts +28 -0
- package/dist/helpers/executor.js +45 -0
- package/dist/helpers/gen.d.ts +8 -0
- package/dist/helpers/gen.js +134 -0
- package/dist/helpers/relation.d.ts +10 -0
- package/dist/helpers/relation.js +155 -0
- package/dist/helpers/updateOneOrAdd.d.ts +2 -0
- package/dist/helpers/updateOneOrAdd.js +18 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +6 -0
- package/dist/types/Id.d.ts +3 -0
- package/dist/types/Id.js +1 -0
- package/dist/types/arg.d.ts +12 -0
- package/dist/types/arg.js +1 -0
- package/dist/types/data.d.ts +4 -0
- package/dist/types/data.js +1 -0
- package/dist/types/export.d.ts +13 -0
- package/dist/types/export.js +1 -0
- package/dist/types/fileCpu.d.ts +51 -0
- package/dist/types/fileCpu.js +1 -0
- package/dist/types/options.d.ts +19 -0
- package/dist/types/options.js +1 -0
- package/dist/types/query.d.ts +15 -0
- package/dist/types/query.js +1 -0
- package/dist/types/relation.d.ts +28 -0
- package/dist/types/relation.js +1 -0
- package/dist/types/searchOpts.d.ts +62 -0
- package/dist/types/searchOpts.js +7 -0
- package/dist/types/types.d.ts +3 -0
- package/dist/types/types.js +1 -0
- package/dist/types/updater.d.ts +26 -0
- package/dist/types/updater.js +4 -0
- package/dist/types/valthera.d.ts +36 -0
- package/dist/types/valthera.js +1 -0
- package/dist/utils/hasFields.d.ts +7 -0
- package/dist/utils/hasFields.js +18 -0
- package/dist/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/utils/hasFieldsAdvanced.js +176 -0
- package/dist/utils/sort.d.ts +1 -0
- package/dist/utils/sort.js +23 -0
- package/dist/utils/updateFindObject.d.ts +11 -0
- package/dist/utils/updateFindObject.js +28 -0
- package/dist/utils/updateObject.d.ts +7 -0
- package/dist/utils/updateObject.js +145 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/package.json +26 -0
- package/src/base/actions.ts +71 -0
- package/src/base/db.ts +75 -0
- package/src/customFileCpu.ts +100 -0
- package/src/db/memory.ts +177 -0
- package/src/db/valthera.ts +132 -0
- package/src/helpers/CollectionManager.ts +67 -0
- package/src/helpers/eventEmiter.ts +73 -0
- package/src/helpers/executor.ts +55 -0
- package/src/helpers/gen.ts +154 -0
- package/src/helpers/relation.ts +192 -0
- package/src/helpers/updateOneOrAdd.ts +24 -0
- package/src/index.ts +25 -0
- package/src/types/Id.ts +3 -0
- package/src/types/arg.ts +15 -0
- package/src/types/data.ts +4 -0
- package/src/types/export.ts +13 -0
- package/src/types/fileCpu.ts +78 -0
- package/src/types/options.ts +22 -0
- package/src/types/query.ts +16 -0
- package/src/types/relation.ts +34 -0
- package/src/types/searchOpts.ts +78 -0
- package/src/types/types.ts +3 -0
- package/src/types/updater.ts +33 -0
- package/src/types/valthera.ts +38 -0
- package/src/utils/hasFields.ts +18 -0
- package/src/utils/hasFieldsAdvanced.ts +181 -0
- package/src/utils/sort.ts +25 -0
- package/src/utils/updateFindObject.ts +35 -0
- package/src/utils/updateObject.ts +156 -0
- package/src/version.ts +1 -0
- package/suglite.json +10 -0
- package/tsconfig.json +22 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { UniversalEventEmitter } from "../helpers/eventEmiter.js";
|
|
2
|
+
import dbActionBase from "../base/actions.js";
|
|
3
|
+
import CollectionManager from "../helpers/CollectionManager.js";
|
|
4
|
+
import executorC from "../helpers/executor.js";
|
|
5
|
+
import { Arg, Search, Updater } from "../types/arg.js";
|
|
6
|
+
import Data from "../types/data.js";
|
|
7
|
+
import { DbOpts, DbFindOpts, FindOpts } from "../types/options.js";
|
|
8
|
+
import { VContext } from "../types/types.js";
|
|
9
|
+
import { ValtheraCompatible } from "../types/valthera.js";
|
|
10
|
+
/**
|
|
11
|
+
* Represents a database management class for performing CRUD operations.
|
|
12
|
+
* @class
|
|
13
|
+
*/
|
|
14
|
+
declare class ValtheraClass implements ValtheraCompatible {
|
|
15
|
+
dbAction: dbActionBase;
|
|
16
|
+
executor: executorC;
|
|
17
|
+
emiter: UniversalEventEmitter;
|
|
18
|
+
version: string;
|
|
19
|
+
constructor(options?: DbOpts);
|
|
20
|
+
private execute;
|
|
21
|
+
/**
|
|
22
|
+
* Create a new instance of a CollectionManager class.
|
|
23
|
+
*/
|
|
24
|
+
c(collection: string): CollectionManager;
|
|
25
|
+
/**
|
|
26
|
+
* Get the names of all available databases.
|
|
27
|
+
*/
|
|
28
|
+
getCollections(): Promise<string[]>;
|
|
29
|
+
/**
|
|
30
|
+
* Check and create the specified collection if it doesn't exist.
|
|
31
|
+
*/
|
|
32
|
+
checkCollection(collection: string): Promise<boolean>;
|
|
33
|
+
/**
|
|
34
|
+
* Check if a collection exists.
|
|
35
|
+
*/
|
|
36
|
+
issetCollection(collection: string): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* Add data to a database.
|
|
39
|
+
*/
|
|
40
|
+
add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
|
|
41
|
+
/**
|
|
42
|
+
* Find data in a database.
|
|
43
|
+
*/
|
|
44
|
+
find<T = Data>(collection: string, search: Search, context?: VContext, dbFindOpts?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Find one data entry in a database.
|
|
47
|
+
*/
|
|
48
|
+
findOne<T = Data>(collection: string, search: Search, context?: VContext, findOpts?: FindOpts): Promise<T>;
|
|
49
|
+
/**
|
|
50
|
+
* Update data in a database.
|
|
51
|
+
*/
|
|
52
|
+
update(collection: string, search: Search, updater: Updater, context?: {}): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Update one data entry in a database.
|
|
55
|
+
*/
|
|
56
|
+
updateOne(collection: string, search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Remove data from a database.
|
|
59
|
+
*/
|
|
60
|
+
remove(collection: string, search: Search, context?: VContext): Promise<boolean>;
|
|
61
|
+
/**
|
|
62
|
+
* Remove one data entry from a database.
|
|
63
|
+
*/
|
|
64
|
+
removeOne(collection: string, search: Search, context?: VContext): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
67
|
+
*/
|
|
68
|
+
updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: VContext, id_gen?: boolean): Promise<boolean>;
|
|
69
|
+
/**
|
|
70
|
+
* Removes a database collection from the file system.
|
|
71
|
+
*/
|
|
72
|
+
removeCollection(collection: string): Promise<boolean>;
|
|
73
|
+
}
|
|
74
|
+
export default ValtheraClass;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { version } from "../version.js";
|
|
2
|
+
import { UniversalEventEmitter } from "../helpers/eventEmiter.js";
|
|
3
|
+
import dbActionBase from "../base/actions.js";
|
|
4
|
+
import CollectionManager from "../helpers/CollectionManager.js";
|
|
5
|
+
import executorC from "../helpers/executor.js";
|
|
6
|
+
/**
|
|
7
|
+
* Represents a database management class for performing CRUD operations.
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
class ValtheraClass {
|
|
11
|
+
dbAction;
|
|
12
|
+
executor;
|
|
13
|
+
emiter;
|
|
14
|
+
version = version;
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.dbAction = options.dbAction || new dbActionBase();
|
|
17
|
+
this.executor = options.executor || new executorC();
|
|
18
|
+
this.emiter = new UniversalEventEmitter();
|
|
19
|
+
}
|
|
20
|
+
async execute(name, ...args) {
|
|
21
|
+
const result = await this.executor.addOp(this.dbAction[name].bind(this.dbAction), ...args);
|
|
22
|
+
if (this.emiter.listenerCount(name) !== 0)
|
|
23
|
+
this.emiter.emit(name, args, result);
|
|
24
|
+
if (this.emiter.listenerCount("*") !== 0)
|
|
25
|
+
this.emiter.emit("*", name, args, result);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a new instance of a CollectionManager class.
|
|
30
|
+
*/
|
|
31
|
+
c(collection) {
|
|
32
|
+
return new CollectionManager(this, collection);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the names of all available databases.
|
|
36
|
+
*/
|
|
37
|
+
async getCollections() {
|
|
38
|
+
return await this.execute("getCollections");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check and create the specified collection if it doesn't exist.
|
|
42
|
+
*/
|
|
43
|
+
async checkCollection(collection) {
|
|
44
|
+
return await this.execute("checkCollection", { collection });
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Check if a collection exists.
|
|
48
|
+
*/
|
|
49
|
+
async issetCollection(collection) {
|
|
50
|
+
return await this.execute("issetCollection", { collection });
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Add data to a database.
|
|
54
|
+
*/
|
|
55
|
+
async add(collection, data, id_gen = true) {
|
|
56
|
+
return await this.execute("add", { collection, data, id_gen });
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Find data in a database.
|
|
60
|
+
*/
|
|
61
|
+
async find(collection, search, context = {}, dbFindOpts = {}, findOpts = {}) {
|
|
62
|
+
return await this.execute("find", { collection, search, context, dbFindOpts, findOpts });
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Find one data entry in a database.
|
|
66
|
+
*/
|
|
67
|
+
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
68
|
+
return await this.execute("findOne", { collection, search, context, findOpts });
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Update data in a database.
|
|
72
|
+
*/
|
|
73
|
+
async update(collection, search, updater, context = {}) {
|
|
74
|
+
return await this.execute("update", { collection, search, updater, context });
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Update one data entry in a database.
|
|
78
|
+
*/
|
|
79
|
+
async updateOne(collection, search, updater, context = {}) {
|
|
80
|
+
return await this.execute("updateOne", { collection, search, updater, context });
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Remove data from a database.
|
|
84
|
+
*/
|
|
85
|
+
async remove(collection, search, context = {}) {
|
|
86
|
+
return await this.execute("remove", { collection, search, context });
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Remove one data entry from a database.
|
|
90
|
+
*/
|
|
91
|
+
async removeOne(collection, search, context = {}) {
|
|
92
|
+
return await this.execute("removeOne", { 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, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
98
|
+
return await this.execute("updateOneOrAdd", { collection, search, updater, add_arg, context, id_gen });
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Removes a database collection from the file system.
|
|
102
|
+
*/
|
|
103
|
+
async removeCollection(collection) {
|
|
104
|
+
return await this.execute("removeCollection", { collection });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export default ValtheraClass;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Arg, Search, Updater } from "../types/arg.js";
|
|
2
|
+
import { DbFindOpts, FindOpts } from "../types/options.js";
|
|
3
|
+
import { VContext } from "../types/types.js";
|
|
4
|
+
import Data from "../types/data.js";
|
|
5
|
+
import { ValtheraCompatible } from "../types/valthera.js";
|
|
6
|
+
declare class CollectionManager {
|
|
7
|
+
private db;
|
|
8
|
+
private collection;
|
|
9
|
+
constructor(db: ValtheraCompatible, collection: string);
|
|
10
|
+
/**
|
|
11
|
+
* Add data to a database.
|
|
12
|
+
*/
|
|
13
|
+
add<T = Data>(data: Arg, id_gen?: boolean): Promise<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Find data in a database.
|
|
16
|
+
*/
|
|
17
|
+
find<T = Data>(search: Search, context?: VContext, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Find one data entry in a database.
|
|
20
|
+
*/
|
|
21
|
+
findOne<T = Data>(search: Search, context?: VContext, findOpts?: FindOpts): Promise<T>;
|
|
22
|
+
/**
|
|
23
|
+
* Update data in a database.
|
|
24
|
+
*/
|
|
25
|
+
update(search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* Update one data entry in a database.
|
|
28
|
+
*/
|
|
29
|
+
updateOne(search: Search, updater: Updater, context?: VContext): Promise<boolean>;
|
|
30
|
+
/**
|
|
31
|
+
* Remove data from a database.
|
|
32
|
+
*/
|
|
33
|
+
remove(search: Search, context?: VContext): Promise<boolean>;
|
|
34
|
+
/**
|
|
35
|
+
* Remove one data entry from a database.
|
|
36
|
+
*/
|
|
37
|
+
removeOne(search: Search, context?: VContext): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
40
|
+
*/
|
|
41
|
+
updateOneOrAdd(search: Search, updater: Updater, add_arg?: Arg, context?: VContext, id_gen?: boolean): Promise<boolean>;
|
|
42
|
+
}
|
|
43
|
+
export default CollectionManager;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
class CollectionManager {
|
|
2
|
+
db;
|
|
3
|
+
collection;
|
|
4
|
+
constructor(db, collection) {
|
|
5
|
+
this.db = db;
|
|
6
|
+
this.collection = collection;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Add data to a database.
|
|
10
|
+
*/
|
|
11
|
+
async add(data, id_gen = true) {
|
|
12
|
+
return await this.db.add(this.collection, data, id_gen);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Find data in a database.
|
|
16
|
+
*/
|
|
17
|
+
async find(search, context = {}, options = {}, findOpts = {}) {
|
|
18
|
+
return await this.db.find(this.collection, search, context, options, findOpts);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Find one data entry in a database.
|
|
22
|
+
*/
|
|
23
|
+
async findOne(search, context = {}, findOpts = {}) {
|
|
24
|
+
return await this.db.findOne(this.collection, search, context, findOpts);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Update data in a database.
|
|
28
|
+
*/
|
|
29
|
+
async update(search, updater, context = {}) {
|
|
30
|
+
return await this.db.update(this.collection, search, updater, context);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Update one data entry in a database.
|
|
34
|
+
*/
|
|
35
|
+
async updateOne(search, updater, context = {}) {
|
|
36
|
+
return await this.db.updateOne(this.collection, search, updater, context);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Remove data from a database.
|
|
40
|
+
*/
|
|
41
|
+
async remove(search, context = {}) {
|
|
42
|
+
return await this.db.remove(this.collection, search, context);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Remove one data entry from a database.
|
|
46
|
+
*/
|
|
47
|
+
async removeOne(search, context = {}) {
|
|
48
|
+
return await this.db.removeOne(this.collection, search, context);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
52
|
+
*/
|
|
53
|
+
async updateOneOrAdd(search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
54
|
+
return await this.db.updateOneOrAdd(this.collection, search, updater, add_arg, context, id_gen);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
export default CollectionManager;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare class UniversalEventEmitter {
|
|
2
|
+
private _events;
|
|
3
|
+
/**
|
|
4
|
+
* Registers an event listener
|
|
5
|
+
* @param {string} event - event name
|
|
6
|
+
* @param {Function} listener - function to be called when event occurs
|
|
7
|
+
*/
|
|
8
|
+
on(event: string, listener: Function): void;
|
|
9
|
+
/**
|
|
10
|
+
* Registers a one-time event listener
|
|
11
|
+
* @param {string} event - event name
|
|
12
|
+
* @param {Function} listener - function to be called once
|
|
13
|
+
*/
|
|
14
|
+
once(event: string, listener: Function): void;
|
|
15
|
+
/**
|
|
16
|
+
* Removes an event listener
|
|
17
|
+
* @param {string} event - event name
|
|
18
|
+
* @param {Function} [listener] - optional: specific listener to remove
|
|
19
|
+
*/
|
|
20
|
+
off(event?: string, listener?: Function): void;
|
|
21
|
+
/**
|
|
22
|
+
* Emits an event
|
|
23
|
+
* @param {string} event - event name
|
|
24
|
+
* @param {...any} args - arguments to be passed to listeners
|
|
25
|
+
*/
|
|
26
|
+
emit(event: string, ...args: any[]): void;
|
|
27
|
+
/**
|
|
28
|
+
* Returns the number of listeners for the given event
|
|
29
|
+
*/
|
|
30
|
+
listenerCount(event: string): number;
|
|
31
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export class UniversalEventEmitter {
|
|
2
|
+
_events = {};
|
|
3
|
+
/**
|
|
4
|
+
* Registers an event listener
|
|
5
|
+
* @param {string} event - event name
|
|
6
|
+
* @param {Function} listener - function to be called when event occurs
|
|
7
|
+
*/
|
|
8
|
+
on(event, listener) {
|
|
9
|
+
if (!this._events[event])
|
|
10
|
+
this._events[event] = [];
|
|
11
|
+
this._events[event].push(listener);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Registers a one-time event listener
|
|
15
|
+
* @param {string} event - event name
|
|
16
|
+
* @param {Function} listener - function to be called once
|
|
17
|
+
*/
|
|
18
|
+
once(event, listener) {
|
|
19
|
+
const onceListener = (...args) => {
|
|
20
|
+
this.off(event, onceListener);
|
|
21
|
+
listener(...args);
|
|
22
|
+
};
|
|
23
|
+
this.on(event, onceListener);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Removes an event listener
|
|
27
|
+
* @param {string} event - event name
|
|
28
|
+
* @param {Function} [listener] - optional: specific listener to remove
|
|
29
|
+
*/
|
|
30
|
+
off(event, listener) {
|
|
31
|
+
if (!event) {
|
|
32
|
+
// Remove all events
|
|
33
|
+
this._events = {};
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if (!listener) {
|
|
37
|
+
// Remove all listeners for the given event
|
|
38
|
+
delete this._events[event];
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
// Remove specific listener
|
|
42
|
+
if (this._events[event]) {
|
|
43
|
+
this._events[event] = this._events[event].filter(l => l !== listener);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Emits an event
|
|
48
|
+
* @param {string} event - event name
|
|
49
|
+
* @param {...any} args - arguments to be passed to listeners
|
|
50
|
+
*/
|
|
51
|
+
emit(event, ...args) {
|
|
52
|
+
const listeners = this._events[event];
|
|
53
|
+
if (listeners && listeners.length > 0) {
|
|
54
|
+
listeners.forEach(listener => {
|
|
55
|
+
listener(...args);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Returns the number of listeners for the given event
|
|
61
|
+
*/
|
|
62
|
+
listenerCount(event) {
|
|
63
|
+
return this._events[event]?.length || 0;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -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;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple executor for queuing and executing asynchronous operations sequentially.
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
class executorC {
|
|
6
|
+
quote;
|
|
7
|
+
isExecuting;
|
|
8
|
+
/**
|
|
9
|
+
* Create a new executor instance.
|
|
10
|
+
* @constructor
|
|
11
|
+
*/
|
|
12
|
+
constructor() {
|
|
13
|
+
this.quote = [];
|
|
14
|
+
this.isExecuting = false;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Add an asynchronous operation to the execution queue.
|
|
18
|
+
*/
|
|
19
|
+
async addOp(func, ...param) {
|
|
20
|
+
return await new Promise((resolve, reject) => {
|
|
21
|
+
this.quote.push({
|
|
22
|
+
func,
|
|
23
|
+
param,
|
|
24
|
+
resolve,
|
|
25
|
+
reject
|
|
26
|
+
});
|
|
27
|
+
this.execute();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Execute the queued asynchronous operations sequentially.
|
|
32
|
+
*/
|
|
33
|
+
async execute() {
|
|
34
|
+
if (this.isExecuting)
|
|
35
|
+
return;
|
|
36
|
+
this.isExecuting = true;
|
|
37
|
+
while (this.quote.length > 0) {
|
|
38
|
+
let q = this.quote.shift();
|
|
39
|
+
let res = await q.func(...q.param);
|
|
40
|
+
q.resolve(res);
|
|
41
|
+
}
|
|
42
|
+
this.isExecuting = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export default executorC;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import Id from "../types/Id.js";
|
|
2
|
+
/**
|
|
3
|
+
* Generates a unique random identifier based on time and parts.
|
|
4
|
+
*
|
|
5
|
+
* @param {number[]} [parts] - an array of lengths of parts of the identifier
|
|
6
|
+
* @returns {Id} - a new unique identifier
|
|
7
|
+
*/
|
|
8
|
+
export default function genId(parts?: number[]): Id;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const usedIdsMap = new Map();
|
|
2
|
+
let lastId;
|
|
3
|
+
const recentIdsTimestamps = [];
|
|
4
|
+
let startIndex = 0;
|
|
5
|
+
let lastGeneratedMs = 0;
|
|
6
|
+
let lastRandomValue = 0;
|
|
7
|
+
/**
|
|
8
|
+
* Generates a unique random identifier based on time and parts.
|
|
9
|
+
*
|
|
10
|
+
* @param {number[]} [parts] - an array of lengths of parts of the identifier
|
|
11
|
+
* @returns {Id} - a new unique identifier
|
|
12
|
+
*/
|
|
13
|
+
export default function genId(parts = null) {
|
|
14
|
+
if (parts === null)
|
|
15
|
+
parts = [1, 1];
|
|
16
|
+
const time = getTime();
|
|
17
|
+
const id = getUniqueRandom({
|
|
18
|
+
time,
|
|
19
|
+
partsSchema: parts,
|
|
20
|
+
s: 0,
|
|
21
|
+
});
|
|
22
|
+
return id;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Generates a unique random identifier based on time and parts.
|
|
26
|
+
*/
|
|
27
|
+
function getUniqueRandom(opts) {
|
|
28
|
+
while (true) {
|
|
29
|
+
let minValue = 0;
|
|
30
|
+
if (lastId) {
|
|
31
|
+
const parts = lastId.split("-");
|
|
32
|
+
const lastTime = parts.shift();
|
|
33
|
+
if (lastTime === opts.time) {
|
|
34
|
+
const int36 = parts.join("");
|
|
35
|
+
minValue = parseInt(int36, 36) + 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const partsLengthSum = opts.partsSchema.reduce((acc, num) => acc + num, 0);
|
|
39
|
+
const partsData = generateBase36InRange(minValue, partsLengthSum);
|
|
40
|
+
const parts = splitStringByArray(opts.partsSchema, partsData);
|
|
41
|
+
const id = [opts.time, ...parts].join("-");
|
|
42
|
+
if (!usedIdsMap.has(id)) {
|
|
43
|
+
usedIdsMap.set(id, true);
|
|
44
|
+
setTimeout(() => usedIdsMap.delete(id), 1000);
|
|
45
|
+
lastId = id;
|
|
46
|
+
return id;
|
|
47
|
+
}
|
|
48
|
+
opts.s++;
|
|
49
|
+
if (opts.s >= partsLengthSum) {
|
|
50
|
+
opts.time = getTime();
|
|
51
|
+
opts.s = 0;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Tracks the current load of the application by counting the number of ids that have been generated in the last second.
|
|
57
|
+
* @returns The number of ids that have been generated in the last second.
|
|
58
|
+
*/
|
|
59
|
+
function trackLoad() {
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
recentIdsTimestamps.push(now);
|
|
62
|
+
while (startIndex < recentIdsTimestamps.length && recentIdsTimestamps[startIndex] < now - 1) {
|
|
63
|
+
startIndex++;
|
|
64
|
+
}
|
|
65
|
+
return recentIdsTimestamps.length - startIndex;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generates a base-36 encoded string with a specified length based on a random value within a range.
|
|
69
|
+
*
|
|
70
|
+
* The function calculates a random value within the base-36 range determined by the provided
|
|
71
|
+
* `minValue` and `length`. It uses a bias mechanism to adjust the randomness based on the
|
|
72
|
+
* current load, ensuring unique generation over short periods. If the system load is low or
|
|
73
|
+
* the current millisecond has changed, a new random value is generated. Otherwise, the random
|
|
74
|
+
* value is incremented by a small amount with certain probabilities.
|
|
75
|
+
*
|
|
76
|
+
* @param minValue - The minimum value for the base-36 encoded number.
|
|
77
|
+
* @param length - The length of the resulting base-36 string.
|
|
78
|
+
* @returns A base-36 encoded string representation of the random value, padded to the specified length.
|
|
79
|
+
*/
|
|
80
|
+
function generateBase36InRange(minValue, length) {
|
|
81
|
+
const maxValue = Math.pow(36, length) - 1;
|
|
82
|
+
const load = trackLoad();
|
|
83
|
+
const currentMs = Date.now();
|
|
84
|
+
if (load < 3 || currentMs !== lastGeneratedMs) {
|
|
85
|
+
lastRandomValue = biasedRandomInRange(minValue, maxValue);
|
|
86
|
+
lastGeneratedMs = currentMs;
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
const rand = Math.random();
|
|
90
|
+
if (rand < 0.6) {
|
|
91
|
+
lastRandomValue += 1;
|
|
92
|
+
}
|
|
93
|
+
else if (rand < 0.9) {
|
|
94
|
+
lastRandomValue += 2;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
lastRandomValue = biasedRandomInRange(lastRandomValue, maxValue);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return lastRandomValue.toString(36).padStart(length, "0");
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generates a random number between the specified range, but with a bias towards the lower
|
|
104
|
+
* end of the range. The bias is calculated as the cube root of the random value, which
|
|
105
|
+
* will make the random value more likely to be closer to the minimum value.
|
|
106
|
+
* @param min - The minimum value of the range.
|
|
107
|
+
* @param max - The maximum value of the range.
|
|
108
|
+
* @returns A random number between the specified range, with a bias towards the lower end.
|
|
109
|
+
*/
|
|
110
|
+
function biasedRandomInRange(min, max) {
|
|
111
|
+
let randomValue = min;
|
|
112
|
+
const bias = Math.pow(Math.random(), 3);
|
|
113
|
+
randomValue = Math.floor(min + (max - min) * bias);
|
|
114
|
+
return Math.min(randomValue, max);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Splits a string into an array of substrings, where each substring is determined by
|
|
118
|
+
* the values in the provided array.
|
|
119
|
+
*
|
|
120
|
+
* @param arr - An array of numbers, where each number represents the length of a substring.
|
|
121
|
+
* @param str - The string to split.
|
|
122
|
+
* @returns An array of substrings, where each element is a substring of the original string,
|
|
123
|
+
* determined by the corresponding element in the provided array.
|
|
124
|
+
*/
|
|
125
|
+
function splitStringByArray(arr, str) {
|
|
126
|
+
let start = 0;
|
|
127
|
+
return arr.map(length => str.slice(start, start += length));
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Gets the current time in a base36 string format.
|
|
131
|
+
*/
|
|
132
|
+
function getTime() {
|
|
133
|
+
return new Date().getTime().toString(36);
|
|
134
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Search } from "../types/arg.js";
|
|
2
|
+
import { DbFindOpts } from "../types/options.js";
|
|
3
|
+
import { RelationTypes } from "../types/relation.js";
|
|
4
|
+
declare class Relation {
|
|
5
|
+
dbs: RelationTypes.DBS;
|
|
6
|
+
constructor(dbs: RelationTypes.DBS);
|
|
7
|
+
findOne(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select?: string[][] | Record<string, any>): Promise<any>;
|
|
8
|
+
find(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select?: string[][] | Record<string, any>, findOpts?: DbFindOpts): Promise<any[]>;
|
|
9
|
+
}
|
|
10
|
+
export default Relation;
|