@wxn0brp/db-core 0.2.1 → 0.2.3
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.
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import ActionsBase from "../base/actions.js";
|
|
2
|
+
import { VQuery } from "../types/query.js";
|
|
3
|
+
export declare class MultiBackend extends ActionsBase {
|
|
4
|
+
backends: ActionsBase[];
|
|
5
|
+
primaryBackend: ActionsBase;
|
|
6
|
+
constructor(backends: ActionsBase[], primaryIndex?: number);
|
|
7
|
+
init(...args: any[]): Promise<void>;
|
|
8
|
+
add(config: VQuery): Promise<import("../types/data.js").Data>;
|
|
9
|
+
find(config: VQuery): Promise<import("../types/data.js").Data[]>;
|
|
10
|
+
findOne(config: VQuery): Promise<import("../types/data.js").Data>;
|
|
11
|
+
update(config: VQuery): Promise<boolean>;
|
|
12
|
+
updateOne(config: VQuery): Promise<boolean>;
|
|
13
|
+
remove(config: VQuery): Promise<boolean>;
|
|
14
|
+
removeOne(config: VQuery): Promise<boolean>;
|
|
15
|
+
ensureCollection(config: VQuery): Promise<boolean>;
|
|
16
|
+
issetCollection(config: VQuery): Promise<boolean>;
|
|
17
|
+
removeCollection(config: VQuery): Promise<boolean>;
|
|
18
|
+
getCollections(): Promise<string[]>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import ActionsBase from "../base/actions.js";
|
|
2
|
+
export class MultiBackend extends ActionsBase {
|
|
3
|
+
backends;
|
|
4
|
+
primaryBackend;
|
|
5
|
+
constructor(backends, primaryIndex = 0) {
|
|
6
|
+
super();
|
|
7
|
+
this.backends = backends;
|
|
8
|
+
this.primaryBackend = backends[primaryIndex] || backends[0];
|
|
9
|
+
}
|
|
10
|
+
async init(...args) {
|
|
11
|
+
await Promise.all(this.backends.map(b => b.init?.(...args)));
|
|
12
|
+
}
|
|
13
|
+
async add(config) {
|
|
14
|
+
return await this.primaryBackend.add({ ...config });
|
|
15
|
+
}
|
|
16
|
+
async find(config) {
|
|
17
|
+
const results = await Promise.all(this.backends.map(b => b.find({ ...config })));
|
|
18
|
+
return results.flat();
|
|
19
|
+
}
|
|
20
|
+
async findOne(config) {
|
|
21
|
+
for (const backend of this.backends) {
|
|
22
|
+
try {
|
|
23
|
+
const result = await backend.findOne({ ...config });
|
|
24
|
+
if (result !== null && result !== undefined) {
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
async update(config) {
|
|
35
|
+
const results = await Promise.all(this.backends.map(b => b.update({ ...config })));
|
|
36
|
+
return results.some(Boolean);
|
|
37
|
+
}
|
|
38
|
+
async updateOne(config) {
|
|
39
|
+
for (const backend of this.backends) {
|
|
40
|
+
try {
|
|
41
|
+
const result = await backend.updateOne({ ...config });
|
|
42
|
+
if (result === true) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
async remove(config) {
|
|
53
|
+
const results = await Promise.all(this.backends.map(b => b.remove({ ...config })));
|
|
54
|
+
return results.some(Boolean);
|
|
55
|
+
}
|
|
56
|
+
async removeOne(config) {
|
|
57
|
+
for (const backend of this.backends) {
|
|
58
|
+
try {
|
|
59
|
+
const result = await backend.removeOne({ ...config });
|
|
60
|
+
if (result === true) {
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
async ensureCollection(config) {
|
|
71
|
+
const results = await Promise.all(this.backends.map(b => b.ensureCollection({ ...config })));
|
|
72
|
+
return results.some(Boolean);
|
|
73
|
+
}
|
|
74
|
+
async issetCollection(config) {
|
|
75
|
+
for (const backend of this.backends) {
|
|
76
|
+
if (await backend.issetCollection({ ...config })) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
async removeCollection(config) {
|
|
83
|
+
const results = await Promise.all(this.backends.map(b => b.removeCollection({ ...config })));
|
|
84
|
+
return results.some(Boolean);
|
|
85
|
+
}
|
|
86
|
+
async getCollections() {
|
|
87
|
+
const allCollections = await Promise.all(this.backends.map(b => b.getCollections()));
|
|
88
|
+
const uniqueCollections = new Set();
|
|
89
|
+
allCollections.forEach(collections => {
|
|
90
|
+
collections.forEach(collection => uniqueCollections.add(collection));
|
|
91
|
+
});
|
|
92
|
+
return Array.from(uniqueCollections);
|
|
93
|
+
}
|
|
94
|
+
}
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -73,7 +73,7 @@ declare class ValtheraClass implements ValtheraCompatible {
|
|
|
73
73
|
/**
|
|
74
74
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
75
75
|
*/
|
|
76
|
-
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, { add_arg, context, id_gen }
|
|
76
|
+
updateOneOrAdd<T = Data>(collection: string, search: Search<T>, updater: Updater<T>, { add_arg, context, id_gen }?: UpdateOneOrAdd<T>): Promise<boolean>;
|
|
77
77
|
/**
|
|
78
78
|
* Removes a database collection from the file system.
|
|
79
79
|
*/
|
package/dist/db/valthera.js
CHANGED
|
@@ -102,7 +102,7 @@ class ValtheraClass {
|
|
|
102
102
|
/**
|
|
103
103
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
104
104
|
*/
|
|
105
|
-
async updateOneOrAdd(collection, search, updater, { add_arg = {}, context = {}, id_gen = true }) {
|
|
105
|
+
async updateOneOrAdd(collection, search, updater, { add_arg = {}, context = {}, id_gen = true } = {}) {
|
|
106
106
|
return await this.execute("updateOneOrAdd", { collection, search, updater, add_arg, context, id_gen });
|
|
107
107
|
}
|
|
108
108
|
/**
|
|
@@ -47,7 +47,7 @@ export type ArrayOperators<T = any> = {
|
|
|
47
47
|
};
|
|
48
48
|
/** String Operators */
|
|
49
49
|
export type StringOperators<T = any> = {
|
|
50
|
-
$regex?: PartialOfType<T, RegExp, string>;
|
|
50
|
+
$regex?: PartialOfType<T, RegExp | string, string>;
|
|
51
51
|
$startsWith?: PartialOfType<T, string>;
|
|
52
52
|
$endsWith?: PartialOfType<T, string>;
|
|
53
53
|
};
|
|
@@ -104,7 +104,8 @@ function checkType(obj, fields) {
|
|
|
104
104
|
}
|
|
105
105
|
function checkRegex(obj, fields) {
|
|
106
106
|
if ("$regex" in fields) {
|
|
107
|
-
for (const [key,
|
|
107
|
+
for (const [key, regexData] of Object.entries(fields["$regex"])) {
|
|
108
|
+
const regex = typeof regexData === "string" ? new RegExp(regexData) : regexData;
|
|
108
109
|
if (!regex.test(obj[key]))
|
|
109
110
|
return false;
|
|
110
111
|
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.2.
|
|
1
|
+
export const version = "0.2.3";
|