@wxn0brp/db-core 0.12.5 → 0.12.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/dist/db/multiStorage.js +7 -1
- package/dist/db/routedStorage.js +23 -10
- package/dist/db/valthera.d.ts +1 -1
- package/dist/db/valthera.js +14 -8
- package/dist/helpers/forge.js +1 -4
- package/dist/helpers/relation.js +8 -1
- package/dist/types/plugin.d.ts +1 -0
- package/dist/utils/hasFieldsAdvanced.js +32 -12
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/db/multiStorage.js
CHANGED
|
@@ -8,7 +8,13 @@ export class MultiBackend extends ActionsBase {
|
|
|
8
8
|
this.primaryBackend = backends[primaryIndex] || backends[0];
|
|
9
9
|
}
|
|
10
10
|
async init(...args) {
|
|
11
|
-
|
|
11
|
+
for (const b of this.backends) {
|
|
12
|
+
b.adapterOpts = {
|
|
13
|
+
...b.adapterOpts,
|
|
14
|
+
...this.adapterOpts,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
await Promise.all(this.backends.map(b => b.init(...args)));
|
|
12
18
|
}
|
|
13
19
|
async close(...args) {
|
|
14
20
|
await Promise.all(this.backends.map(b => b.close(...args)));
|
package/dist/db/routedStorage.js
CHANGED
|
@@ -43,7 +43,16 @@ export class RoutedStorage extends ActionsBase {
|
|
|
43
43
|
return await fn(first);
|
|
44
44
|
}
|
|
45
45
|
async init(...args) {
|
|
46
|
-
const all = new Set(
|
|
46
|
+
const all = new Set([
|
|
47
|
+
...this.rules.flatMap(r => r.backends),
|
|
48
|
+
...this.defaultBackend,
|
|
49
|
+
]);
|
|
50
|
+
for (const b of all) {
|
|
51
|
+
b.adapterOpts = {
|
|
52
|
+
...b.adapterOpts,
|
|
53
|
+
...this.adapterOpts,
|
|
54
|
+
};
|
|
55
|
+
}
|
|
47
56
|
await Promise.all(Array.from(all).map(b => b.init?.(...args)));
|
|
48
57
|
}
|
|
49
58
|
async close(...args) {
|
|
@@ -54,14 +63,15 @@ export class RoutedStorage extends ActionsBase {
|
|
|
54
63
|
await Promise.all(Array.from(all).map(b => b.close(...args)));
|
|
55
64
|
}
|
|
56
65
|
async add(config) {
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
66
|
+
const backends = this._matchBackends(config);
|
|
67
|
+
const [first, ...rest] = backends;
|
|
68
|
+
const res = await first.add(config);
|
|
69
|
+
if (rest.length > 0) {
|
|
70
|
+
await Promise.all(rest.map(b => b.add({
|
|
71
|
+
...config,
|
|
72
|
+
data: res,
|
|
73
|
+
})));
|
|
74
|
+
}
|
|
65
75
|
return res;
|
|
66
76
|
}
|
|
67
77
|
async find(config) {
|
|
@@ -92,7 +102,10 @@ export class RoutedStorage extends ActionsBase {
|
|
|
92
102
|
return this._withAll(collection, b => b.removeCollection(collection), true);
|
|
93
103
|
}
|
|
94
104
|
async getCollections() {
|
|
95
|
-
const all = await Promise.all(Array.from(new Set(
|
|
105
|
+
const all = await Promise.all(Array.from(new Set([
|
|
106
|
+
...this.rules.flatMap(r => r.backends),
|
|
107
|
+
...this.defaultBackend,
|
|
108
|
+
])).map(b => b.getCollections()));
|
|
96
109
|
const merged = new Set(all.flat());
|
|
97
110
|
return Array.from(merged);
|
|
98
111
|
}
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ export declare class ValtheraClass implements ValtheraCompatible {
|
|
|
26
26
|
version: string;
|
|
27
27
|
_plugins: ValtheraPlugin[];
|
|
28
28
|
_collections: Map<string, Collection<any>>;
|
|
29
|
-
plugin(p: ValtheraPlugin): () => void;
|
|
29
|
+
plugin(p: ValtheraPlugin, collections?: string[] | string): () => void;
|
|
30
30
|
constructor(options: DbOpts);
|
|
31
31
|
init(...args: any[]): Promise<any>;
|
|
32
32
|
close(...args: any[]): Promise<any>;
|
package/dist/db/valthera.js
CHANGED
|
@@ -17,7 +17,14 @@ export class ValtheraClass {
|
|
|
17
17
|
version = version;
|
|
18
18
|
_plugins = [];
|
|
19
19
|
_collections = new Map();
|
|
20
|
-
plugin(p) {
|
|
20
|
+
plugin(p, collections) {
|
|
21
|
+
if (collections) {
|
|
22
|
+
p.collections = Array.isArray(collections)
|
|
23
|
+
? collections
|
|
24
|
+
: [
|
|
25
|
+
collections,
|
|
26
|
+
];
|
|
27
|
+
}
|
|
21
28
|
p.init?.(this);
|
|
22
29
|
this._plugins.push(p);
|
|
23
30
|
return () => {
|
|
@@ -29,15 +36,13 @@ export class ValtheraClass {
|
|
|
29
36
|
constructor(options) {
|
|
30
37
|
this.options = options;
|
|
31
38
|
this.executor = options.executor || new SmartExecutor(undefined, false);
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
else {
|
|
39
|
+
options.executorAware ??= true;
|
|
40
|
+
if (typeof options.adapter !== "function") {
|
|
35
41
|
this.adapter = options.adapter;
|
|
36
42
|
if (options.adapterOpts) {
|
|
37
43
|
this.adapter.adapterOpts = options.adapterOpts;
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
|
-
options.executorAware ??= true;
|
|
41
46
|
}
|
|
42
47
|
async init(...args) {
|
|
43
48
|
if (this.adapter?._inited)
|
|
@@ -65,11 +70,11 @@ export class ValtheraClass {
|
|
|
65
70
|
});
|
|
66
71
|
}
|
|
67
72
|
async close(...args) {
|
|
68
|
-
if (!this.adapter
|
|
73
|
+
if (!this.adapter?._inited)
|
|
69
74
|
return;
|
|
70
75
|
const self = this;
|
|
71
76
|
return await this.executor.addOp(async () => {
|
|
72
|
-
if (!self.adapter
|
|
77
|
+
if (!self.adapter?._inited)
|
|
73
78
|
return;
|
|
74
79
|
await self.adapter.close(...args);
|
|
75
80
|
self.adapter._inited = false;
|
|
@@ -79,7 +84,8 @@ export class ValtheraClass {
|
|
|
79
84
|
await this.init();
|
|
80
85
|
if (txHandle && typeof query === "object")
|
|
81
86
|
query.transaction = txHandle;
|
|
82
|
-
const
|
|
87
|
+
const collection = typeof query === "string" ? query : query.collection;
|
|
88
|
+
const plugins = this._plugins.filter(p => !p.collections || p.collections.includes(collection));
|
|
83
89
|
const self = this;
|
|
84
90
|
let idx = 0;
|
|
85
91
|
const ctx = {
|
package/dist/helpers/forge.js
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
-
import { Collection } from "./collection.js";
|
|
2
1
|
export function forgeTypedValthera(target) {
|
|
3
2
|
return new Proxy(target, {
|
|
4
3
|
get(target, prop, receiver) {
|
|
5
4
|
if (prop in target) {
|
|
6
5
|
return Reflect.get(target, prop, receiver);
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
target[prop] = collection;
|
|
10
|
-
return collection;
|
|
7
|
+
return target.c(prop);
|
|
11
8
|
},
|
|
12
9
|
set(target, prop, value, receiver) {
|
|
13
10
|
return Reflect.set(target, prop, value, receiver);
|
package/dist/helpers/relation.js
CHANGED
|
@@ -88,6 +88,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
88
88
|
[fk]: keys,
|
|
89
89
|
},
|
|
90
90
|
},
|
|
91
|
+
dbFindOpts,
|
|
91
92
|
findOpts: {
|
|
92
93
|
select: selectSafe,
|
|
93
94
|
},
|
|
@@ -180,6 +181,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
180
181
|
const allTargetIds = [
|
|
181
182
|
...new Set(allPivots.map(p => p[through.fk])),
|
|
182
183
|
];
|
|
184
|
+
const [selectSafe, deleteSelect] = autoSelect(rel, fk);
|
|
183
185
|
const allRelated = await db.find({
|
|
184
186
|
collection,
|
|
185
187
|
search: {
|
|
@@ -187,8 +189,9 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
187
189
|
[fk]: allTargetIds,
|
|
188
190
|
},
|
|
189
191
|
},
|
|
192
|
+
dbFindOpts,
|
|
190
193
|
findOpts: {
|
|
191
|
-
select,
|
|
194
|
+
select: selectSafe,
|
|
192
195
|
},
|
|
193
196
|
});
|
|
194
197
|
const targetMap = new Map(allRelated.map(r => [
|
|
@@ -203,6 +206,9 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
203
206
|
await Promise.all(related.map(row => processRelations(dbs, rel.relations, row)));
|
|
204
207
|
}
|
|
205
208
|
}
|
|
209
|
+
if (deleteSelect)
|
|
210
|
+
for (const r of allRelated)
|
|
211
|
+
delete r[fk];
|
|
206
212
|
}
|
|
207
213
|
else if (type === "n1") {
|
|
208
214
|
const allFkIds = [
|
|
@@ -221,6 +227,7 @@ export async function processRelations(dbs, cfg, data, parentList = null) {
|
|
|
221
227
|
[fk]: allFkIds,
|
|
222
228
|
},
|
|
223
229
|
},
|
|
230
|
+
dbFindOpts,
|
|
224
231
|
findOpts: {
|
|
225
232
|
select: selectSafe,
|
|
226
233
|
},
|
package/dist/types/plugin.d.ts
CHANGED
|
@@ -53,14 +53,16 @@ function mainCheck(obj, fields) {
|
|
|
53
53
|
in: (data, value) => value.includes(data),
|
|
54
54
|
nin: (data, value) => !value.includes(data),
|
|
55
55
|
type: (data, value) => typeof data === value,
|
|
56
|
-
exists: (
|
|
57
|
-
if (shouldExist && !(key in
|
|
56
|
+
exists: (targetObj, shouldExist, key) => {
|
|
57
|
+
if (shouldExist && !(key in targetObj))
|
|
58
58
|
return false;
|
|
59
|
-
if (!shouldExist && key in
|
|
59
|
+
if (!shouldExist && key in targetObj)
|
|
60
60
|
return false;
|
|
61
61
|
return true;
|
|
62
62
|
},
|
|
63
63
|
regex: (data, regexData) => {
|
|
64
|
+
if (typeof data !== "string")
|
|
65
|
+
return false;
|
|
64
66
|
const regex = typeof regexData === "string" ? new RegExp(regexData) : regexData;
|
|
65
67
|
return regex.test(data);
|
|
66
68
|
},
|
|
@@ -101,30 +103,48 @@ function _for(fields, obj, opts) {
|
|
|
101
103
|
continue;
|
|
102
104
|
for (const [key, val] of Object.entries(value)) {
|
|
103
105
|
const targetValue = obj?.[key];
|
|
104
|
-
if (typeof val === "object" &&
|
|
105
|
-
|
|
106
|
+
if (typeof val === "object" &&
|
|
107
|
+
val !== null &&
|
|
108
|
+
!Array.isArray(val) &&
|
|
109
|
+
!(val instanceof RegExp)) {
|
|
110
|
+
if (!deepCheck(val, targetValue, fieldFn, fieldRaw))
|
|
106
111
|
return false;
|
|
107
112
|
}
|
|
108
113
|
else {
|
|
109
|
-
if (
|
|
110
|
-
|
|
114
|
+
if (fieldRaw === "exists") {
|
|
115
|
+
if (!fieldFn(obj, val, key))
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
if (!fieldFn(targetValue, val, key))
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
111
122
|
}
|
|
112
123
|
}
|
|
113
124
|
}
|
|
114
125
|
return true;
|
|
115
126
|
}
|
|
116
|
-
function deepCheck(valueObj, targetObj, fieldFn) {
|
|
127
|
+
function deepCheck(valueObj, targetObj, fieldFn, fieldRaw) {
|
|
117
128
|
if (typeof targetObj !== "object" || targetObj === null)
|
|
118
129
|
return false;
|
|
119
130
|
for (const [k, v] of Object.entries(valueObj)) {
|
|
120
131
|
const targetValue = targetObj[k];
|
|
121
|
-
if (typeof v === "object" &&
|
|
122
|
-
|
|
132
|
+
if (typeof v === "object" &&
|
|
133
|
+
v !== null &&
|
|
134
|
+
!Array.isArray(v) &&
|
|
135
|
+
!(v instanceof RegExp)) {
|
|
136
|
+
if (!deepCheck(v, targetValue, fieldFn, fieldRaw))
|
|
123
137
|
return false;
|
|
124
138
|
}
|
|
125
139
|
else {
|
|
126
|
-
if (
|
|
127
|
-
|
|
140
|
+
if (fieldRaw === "exists") {
|
|
141
|
+
if (!fieldFn(targetObj, v, k))
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
if (!fieldFn(targetValue, v, k))
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
128
148
|
}
|
|
129
149
|
}
|
|
130
150
|
return true;
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.12.
|
|
1
|
+
export declare const version = "0.12.6";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.12.
|
|
1
|
+
export const version = "0.12.6";
|