@coderich/autograph 0.13.33 → 0.13.34

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.33",
4
+ "version": "0.13.34",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -161,9 +161,9 @@ module.exports = class Pipeline {
161
161
  static resolve(params, pipeline) {
162
162
  const transformers = params.field.pipelines[pipeline] || [];
163
163
 
164
- return Util.pipeline(transformers.map(t => (value) => {
165
- return Pipeline[t]({ ...params, value });
166
- }), params.value);
164
+ return transformers.reduce((value, t) => {
165
+ return Util.uvl(Pipeline[t]({ ...params, value }), value);
166
+ }, params.value);
167
167
  }
168
168
  };
169
169
 
@@ -1,8 +1,15 @@
1
1
  const Util = require('@coderich/util');
2
2
 
3
+ // { query, path: path.concat(key), context: this.#context }
4
+
3
5
  module.exports = class Transformer {
4
- #config;
5
- #operation;
6
+ #config = {
7
+ args: {},
8
+ shape: {},
9
+ defaults: {},
10
+ operation: 'set',
11
+ };
12
+
6
13
  #operations = {
7
14
  get: {
8
15
  get: () => {
@@ -10,21 +17,25 @@ module.exports = class Transformer {
10
17
  },
11
18
  },
12
19
  set: {
13
- set: (target, prop, value) => {
20
+ set: (target, prop, startValue) => {
14
21
  const transforms = this.#config.shape[prop] ?? [];
15
- const $value = transforms.reduce((prev, t) => {
16
- if (typeof t === 'function') return t(prev);
22
+
23
+ const result = transforms.reduce((value, t) => {
24
+ if (typeof t === 'function') return Util.uvl(t({ startValue, value, ...this.#config.args }), value);
17
25
  prop = t;
18
- return prev;
19
- }, value);
20
- target[prop] = $value;
26
+ return value;
27
+ }, startValue);
28
+
29
+ target[prop] = result;
21
30
  return true;
22
31
  },
23
32
  },
24
33
  };
25
34
 
35
+ #operation;
36
+
26
37
  /**
27
- * Allowing construction of object before knowing configuration
38
+ * Allowing construction of object before knowing full configuration
28
39
  */
29
40
  constructor(config = {}) {
30
41
  this.config(config);
@@ -34,14 +45,21 @@ module.exports = class Transformer {
34
45
  * Re-assign configuration after instantiation
35
46
  */
36
47
  config(config = {}) {
37
- this.#config = config;
38
- this.#config.shape ??= {};
39
- this.#config.defaults ??= {};
40
- this.#config.operation ??= 'set';
48
+ Object.assign(this.#config, config);
41
49
  this.#operation = this.#operations[this.#config.operation];
50
+ return this;
51
+ }
52
+
53
+ /**
54
+ * Re-assign args after instantiation
55
+ */
56
+ args(args = {}) {
57
+ Object.assign(this.#config.args, args);
58
+ return this;
42
59
  }
43
60
 
44
- transform(mixed) {
61
+ transform(mixed, args) {
62
+ this.args(args);
45
63
  return Util.map(mixed, data => Object.assign(new Proxy({}, this.#operation), this.#config.defaults, data));
46
64
  }
47
65
  };
@@ -64,6 +64,7 @@ module.exports = class Query {
64
64
  */
65
65
  transform(asClone = true) {
66
66
  return Promise.all([
67
+ // this.#model.transformers.input.transform(this.#query.input),
67
68
  this.pipeline('input', this.#query.input),
68
69
  this.#query.isNative ? this.#query.where : this.pipeline('where', this.#query.where ?? {}),
69
70
  this.pipeline('sort', this.#query.sort),
@@ -144,7 +144,10 @@ module.exports = class Schema {
144
144
  loader: this.#config.dataLoaders?.default,
145
145
  generator: this.#config.generators?.default,
146
146
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
147
- transformers: { doc: new Transformer() },
147
+ transformers: {
148
+ input: new Transformer(),
149
+ doc: new Transformer({ args: { model, schema: this.#schema } }),
150
+ },
148
151
  directives: {},
149
152
  toString: () => name,
150
153
  };
@@ -155,7 +158,9 @@ module.exports = class Schema {
155
158
  name,
156
159
  key: name,
157
160
  pipelines: pipelines.reduce((prev, key) => Object.assign(prev, { [key]: [] }), {}),
158
- transformers: { doc: new Transformer() },
161
+ transformers: {
162
+ input: new Transformer({ args: { model, field, schema: this.#schema } }),
163
+ },
159
164
  directives: {},
160
165
  toString: () => name,
161
166
  };
@@ -345,11 +350,27 @@ module.exports = class Schema {
345
350
  where: Object.values($model.fields).filter(f => f.pipelines.instruct.length).reduce((prev, f) => Object.assign(prev, { [f.name]: undefined }), {}),
346
351
  };
347
352
 
353
+ $model.transformers.input.config({
354
+ shape: Object.values($model.fields).reduce((prev, curr) => {
355
+ const rules = [
356
+ a => Pipeline.$default({ ...a, field: curr }),
357
+ a => Pipeline.$cast({ ...a, field: curr }),
358
+ a => Pipeline.$normalize({ ...a, field: curr }),
359
+ a => Pipeline.$instruct({ ...a, field: curr }),
360
+ // a => Pipeline.$finalize({ ...a, field: curr }),
361
+ ];
362
+ // if (curr.isEmbedded) rules.push(a => Util.map(a.value, value => curr.model.transformers.input.transform(value)));
363
+ if (curr.isEmbedded) rules.push(a => curr.model.transformers.input.transform(a.value));
364
+ // rules.push(curr.key); // rename
365
+ return Object.assign(prev, { [curr.name]: rules });
366
+ }, {}),
367
+ });
368
+
348
369
  $model.transformers.doc.config({
349
370
  shape: Object.values($model.fields).reduce((prev, curr) => {
350
371
  const rules = [curr.name]; // Rename key
351
- if (curr.isArray) rules.unshift(v => (v == null ? v : Util.ensureArray(v)));
352
- if (curr.isEmbedded) rules.unshift(v => Util.map(v, el => curr.model.transformers.doc.transform(el)));
372
+ if (curr.isArray) rules.unshift(({ value }) => (value == null ? value : Util.ensureArray(value)));
373
+ if (curr.isEmbedded) rules.unshift(({ value }) => Util.map(value, v => curr.model.transformers.doc.transform(v)));
353
374
  return Object.assign(prev, { [curr.key]: rules });
354
375
  }, {}),
355
376
  });