@coderich/autograph 0.13.40 → 0.13.42
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/package.json +1 -1
- package/src/data/DataLoader.js +29 -9
- package/src/data/Resolver.js +16 -1
package/package.json
CHANGED
package/src/data/DataLoader.js
CHANGED
|
@@ -32,8 +32,9 @@ module.exports = class Loader {
|
|
|
32
32
|
const batchesByKey = queries.reduce((prev, query, i) => {
|
|
33
33
|
const $query = query.toDriver().toObject();
|
|
34
34
|
const key = $query.batch ?? '__default__';
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
let [values] = key === '__default__' ? [] : Object.values(Util.flatten($query.where, { safe: true }));
|
|
36
|
+
values = Array.from(new Set(Util.ensureArray(values)));
|
|
37
|
+
const $values = values.map(value => (value instanceof RegExp ? value : new RegExp(`${value}`, 'i')));
|
|
37
38
|
prev[key] = prev[key] || [];
|
|
38
39
|
prev[key].push({ query, $query, values, $values, i });
|
|
39
40
|
return prev;
|
|
@@ -45,18 +46,37 @@ module.exports = class Loader {
|
|
|
45
46
|
return batches.map(batch => this.#model.source.client.resolve(batch.$query).then(data => ({ data, ...batch })));
|
|
46
47
|
}
|
|
47
48
|
default: {
|
|
49
|
+
// Collect all the values for the where clause
|
|
48
50
|
const values = Array.from(new Set(batches.map(batch => batch.values).flat()));
|
|
49
51
|
const $query = { ...batches[0].$query, op: 'findMany', where: { [key]: values } };
|
|
52
|
+
|
|
53
|
+
//
|
|
54
|
+
if (values.length < 3) {
|
|
55
|
+
return batches.map(batch => this.#model.source.client.resolve(batch.$query).then(data => ({ data, ...batch })));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Collect all the $values (Regular Expressions) to match doc (result) data by
|
|
59
|
+
const $values = Array.from(new Set(batches.map(batch => batch.$values).flat()));
|
|
60
|
+
const docsByRegExpKey = $values.reduce((map, re) => map.set(re, []), new Map());
|
|
61
|
+
|
|
62
|
+
// Now we perform 1 query, instead of many smaller ones
|
|
50
63
|
return this.#model.source.client.resolve($query).then((docs) => {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
// This one-time transformation keys all the docs by $value (regex) match
|
|
65
|
+
docs.forEach((doc) => {
|
|
66
|
+
Util.pathmap(key, doc, (value) => {
|
|
67
|
+
docsByRegExpKey.forEach((set, re) => {
|
|
68
|
+
Util.map(value, (v) => {
|
|
69
|
+
if (`${v}`.match(re)) {
|
|
70
|
+
set.push(doc);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
57
73
|
});
|
|
58
|
-
return
|
|
74
|
+
return value;
|
|
59
75
|
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return batches.map((batch) => {
|
|
79
|
+
const matches = Array.from(new Set(batch.$values.map(re => docsByRegExpKey.get(re)).flat().filter(v => v !== undefined)));
|
|
60
80
|
const data = batch.$query.op === 'findOne' ? matches[0] : matches;
|
|
61
81
|
return { data, ...batch };
|
|
62
82
|
});
|
package/src/data/Resolver.js
CHANGED
|
@@ -205,7 +205,22 @@ module.exports = class Resolver {
|
|
|
205
205
|
return this.toResultSet(model, results);
|
|
206
206
|
});
|
|
207
207
|
} else {
|
|
208
|
-
thunk = tquery =>
|
|
208
|
+
thunk = (tquery) => {
|
|
209
|
+
const { where, op } = query.toObject();
|
|
210
|
+
const values = Object.values(where);
|
|
211
|
+
const $values = values.flat();
|
|
212
|
+
const skipQuery = values.length && (!$values.length || $values.includes(undefined));
|
|
213
|
+
|
|
214
|
+
if (skipQuery) {
|
|
215
|
+
switch (op) {
|
|
216
|
+
case 'count': return Promise.resolve(0);
|
|
217
|
+
case 'findMany': return Promise.resolve([]);
|
|
218
|
+
default: return Promise.resolve(null);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return this.#dataLoaders[model].resolve(tquery);
|
|
223
|
+
};
|
|
209
224
|
}
|
|
210
225
|
|
|
211
226
|
return this.#createSystemEvent(query, (tquery) => {
|