@coderich/autograph 0.13.39 → 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.39",
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
  });
@@ -73,17 +73,19 @@ module.exports = class Pipeline {
73
73
  }, { ignoreNull: false });
74
74
 
75
75
  Pipeline.define('$fk', (params) => {
76
- const { fkField } = params.field;
77
- const v = params.value?.[fkField] || params.value;
76
+ const { fkField, isPrimaryKey } = params.field;
77
+ const lookupField = isPrimaryKey ? params.field : fkField;
78
+ const v = params.value?.[lookupField] || params.value;
78
79
  return Util.map(v, value => params.field.generator({ ...params, value }));
79
80
  });
80
81
 
81
82
  //
82
- Pipeline.define('$cast', ({ field, value }) => {
83
- const { type, isEmbedded } = field;
84
- if (isEmbedded) return value;
83
+ Pipeline.define('$cast', (params) => {
84
+ const { field, value } = params;
85
85
 
86
- switch (type.toLowerCase()) {
86
+ if (field.isEmbedded) return value;
87
+
88
+ switch (field.type.toLowerCase()) {
87
89
  case 'string': {
88
90
  return `${value}`;
89
91
  }
@@ -173,7 +173,7 @@ module.exports = class Resolver {
173
173
 
174
174
  // All transactions bound to this resolver are to be rolled back
175
175
  return Util.promiseChain(session.map(transaction => () => {
176
- transaction.rollback().catch(e => errors.push(e));
176
+ return transaction.rollback().catch(e => errors.push(e));
177
177
  })).then(() => {
178
178
  return errors.length ? Promise.reject(errors) : Promise.all(session.thunks.map(thunk => thunk()));
179
179
  });
@@ -459,7 +459,16 @@ module.exports = class Schema {
459
459
  if (scalar) Object.entries(scalar.pipelines).forEach(([key, values]) => $field.pipelines[key].push(...values));
460
460
 
461
461
  if ($field.isArray) $field.pipelines.normalize.unshift('toArray');
462
- if ($field.isPrimaryKey) $field.pipelines.serialize.unshift('$pk'); // Will create/convert to FK type always
462
+
463
+ // Will create/convert to ID type always
464
+ if ($field.isPrimaryKey) {
465
+ $field.pipelines.construct.unshift('$pk');
466
+ $field.pipelines.restruct.unshift('$pk');
467
+ }
468
+
469
+ // Will convert to ID type IFF defined in payload
470
+ if ($field.isFKReference || $field.isPrimaryKey) $field.pipelines.serialize.unshift('$fk');
471
+
463
472
  if ($field.isRequired && $field.isPersistable && !$field.isVirtual) $field.pipelines.validate.push('required');
464
473
 
465
474
  if ($field.isFKReference) {
@@ -468,7 +477,6 @@ module.exports = class Schema {
468
477
  const from = $field.linkField.key;
469
478
  const as = `join_${to}`;
470
479
  $field.join = { to, on, from, as };
471
- $field.pipelines.serialize.unshift('$fk'); // Will convert to FK type IFF defined in payload
472
480
  $field.pipelines.validate.push('ensureFK'); // Absolute Last
473
481
  }
474
482
  });