@coderich/autograph 0.13.40 → 0.13.41

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coderich/autograph",
3
3
  "main": "index.js",
4
- "version": "0.13.40",
4
+ "version": "0.13.41",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -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
- const [values] = key === '__default__' ? [] : Object.values(Util.flatten($query.where, { safe: true }));
36
- const $values = Util.ensureArray(values).map(value => (value instanceof RegExp ? value : new RegExp(`${value}`, 'i')));
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,32 @@ 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
+ // Collect all the $values (Regular Expressions) to match doc (result) data by
54
+ const $values = Array.from(new Set(batches.map(batch => batch.$values).flat()));
55
+ const docsByRegExpKey = $values.reduce((map, re) => map.set(re, []), new Map());
56
+
57
+ // Now we perform 1 query, instead of many smaller ones
50
58
  return this.#model.source.client.resolve($query).then((docs) => {
51
- return batches.map((batch) => {
52
- const matches = docs.filter((doc) => {
53
- let match = false;
54
- Util.pathmap(key, doc, (mixed) => {
55
- match = match || Util.ensureArray(mixed).some(value => batch.$values.some($value => `${value}`.match($value)));
56
- return mixed;
59
+ // This one-time transformation keys all the docs by $value (regex) match
60
+ docs.forEach((doc) => {
61
+ Util.pathmap(key, doc, (value) => {
62
+ docsByRegExpKey.forEach((set, re) => {
63
+ Util.map(value, (v) => {
64
+ if (`${v}`.match(re)) {
65
+ set.push(doc);
66
+ }
67
+ });
57
68
  });
58
- return match;
69
+ return value;
59
70
  });
71
+ });
72
+
73
+ return batches.map((batch) => {
74
+ const matches = Array.from(new Set(batch.$values.map(re => docsByRegExpKey.get(re)).flat().filter(v => v !== undefined)));
60
75
  const data = batch.$query.op === 'findOne' ? matches[0] : matches;
61
76
  return { data, ...batch };
62
77
  });