@coderich/autograph 0.13.19 → 0.13.20

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.19",
4
+ "version": "0.13.20",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -55,15 +55,14 @@ module.exports = class Pipeline {
55
55
  jsStringTransformers.forEach(name => Pipeline.define(`${name}`, ({ value }) => String(value)[name]()));
56
56
 
57
57
  // Additional Transformers
58
- Pipeline.define('toTitleCase', ({ value }) => value.replace(/\w\S*/g, w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()));
59
- Pipeline.define('toSentenceCase', ({ value }) => value.charAt(0).toUpperCase() + value.slice(1));
60
58
  Pipeline.define('toArray', ({ value }) => (Array.isArray(value) ? value : [value]), { itemize: false });
61
59
  Pipeline.define('toDate', ({ value }) => new Date(value), { configurable: true });
62
60
  Pipeline.define('updatedAt', () => new Date(), { ignoreNull: false });
61
+ Pipeline.define('toTitleCase', ({ value }) => value.replace(/\w\S*/g, w => w.charAt(0).toUpperCase() + w.slice(1).toLowerCase()));
62
+ Pipeline.define('toSentenceCase', ({ value }) => value.charAt(0).toUpperCase() + value.slice(1));
63
63
  Pipeline.define('createdAt', ({ value }) => value || new Date(), { ignoreNull: false });
64
64
  Pipeline.define('timestamp', () => Date.now(), { ignoreNull: false });
65
65
  Pipeline.define('dedupe', ({ value }) => uniqWith(value, (b, c) => hashObject(b) === hashObject(c)), { itemize: false });
66
- Pipeline.define('toId', ({ model, value }) => model.source.idValue(value.id || value)); // Deprecate
67
66
 
68
67
  // Structures
69
68
  Pipeline.define('$instruct', params => Pipeline.resolve(params, 'instruct'), { ignoreNull: false });
@@ -74,8 +73,16 @@ module.exports = class Pipeline {
74
73
  Pipeline.define('$finalize', params => Pipeline.resolve(params, 'finalize'), { ignoreNull: false });
75
74
 
76
75
  //
77
- Pipeline.define('$pk', ({ query, model, value, path }) => model.source.idValue(get(query.doc, path) || value?.id || value), { ignoreNull: false });
78
- Pipeline.define('$fk', ({ model, value }) => model.source.idValue(value.id || value));
76
+ Pipeline.define('$pk', (params) => {
77
+ const value = get(params.query.doc, params.path) || params.value?.id || params.value;
78
+ return Pipeline[params.field.id]({ ...params, value });
79
+ }, { ignoreNull: false });
80
+
81
+ Pipeline.define('$fk', (params) => {
82
+ const value = params.value?.id || params.value;
83
+ return Pipeline[params.field.id]({ ...params, value });
84
+ });
85
+
79
86
  Pipeline.define('$default', ({ field: { defaultValue }, value }) => (value === undefined ? defaultValue : value), { ignoreNull: false });
80
87
 
81
88
  //
@@ -64,15 +64,15 @@ module.exports = class Schema {
64
64
 
65
65
  if (decorator) {
66
66
  const { fields } = parse(`type decorator { ${decorator} }`).definitions[0];
67
- node.fields = mergeFields(node, node.fields, fields, { noLocation: true, onFieldTypeConflict: a => a });
67
+ node.fields = mergeFields(node, node.fields, fields, { noLocation: true, onFieldTypeConflict: (f, a, b) => a });
68
68
  return node;
69
69
  }
70
70
  }
71
71
 
72
- return false;
72
+ return false; // Do not traverse any deeper
73
73
  }
74
74
 
75
- return undefined;
75
+ return undefined; // Continue traversal
76
76
  },
77
77
  });
78
78
 
@@ -135,7 +135,7 @@ module.exports = class Schema {
135
135
  fields: {},
136
136
  crud: 'crud', // For use when creating API Queries and Mutations
137
137
  scope: 'crud', // For use when defining types (how it's field.model reference can be used)
138
- idField: 'id',
138
+ pkField: 'id',
139
139
  isPersistable: true,
140
140
  source: this.#config.dataSources?.default,
141
141
  loader: this.#config.dataLoaders?.default,
@@ -197,8 +197,8 @@ module.exports = class Schema {
197
197
 
198
198
  switch (`${name}-${key}`) {
199
199
  // Model specific directives
200
- case `${directives.model}-id`: {
201
- model.idField = value;
200
+ case `${directives.model}-pk`: {
201
+ model.pkField = value;
202
202
  break;
203
203
  }
204
204
  case `${directives.model}-source`: {
@@ -226,6 +226,10 @@ module.exports = class Schema {
226
226
  target.pipelines.finalize = target.pipelines.finalize.concat(value).filter(Boolean);
227
227
  break;
228
228
  }
229
+ case `${directives.field}-transform`: { // Deprecated
230
+ target.pipelines.normalize = target.pipelines.normalize.concat(value).filter(Boolean);
231
+ break;
232
+ }
229
233
  case `${directives.link}-by`: {
230
234
  target.linkBy = value;
231
235
  target.isVirtual = true;
@@ -240,7 +244,12 @@ module.exports = class Schema {
240
244
  target[key] = Util.nvl(value, '');
241
245
  break;
242
246
  }
243
- case `${directives.model}-key`: case `${directives.model}-meta`: case `${directives.field}-key`: case `${directives.field}-onDelete`: {
247
+ case `${directives.model}-id`:
248
+ case `${directives.model}-key`:
249
+ case `${directives.model}-meta`:
250
+ case `${directives.field}-id`:
251
+ case `${directives.field}-key`:
252
+ case `${directives.field}-onDelete`: {
244
253
  target[key] = value;
245
254
  break;
246
255
  }
@@ -266,8 +275,8 @@ module.exports = class Schema {
266
275
  leave: (node) => {
267
276
  if (modelKinds.includes(node.kind)) {
268
277
  const $model = model;
269
- // const idField = $model.fields[$model.idField];
270
- // $model.primaryKey = Util.nvl(idField?.key, idField?.name, 'id');
278
+
279
+ model.id ??= model.source?.id;
271
280
 
272
281
  // Model resolution after field resolution (push)
273
282
  thunks.push(($schema) => {
@@ -323,15 +332,16 @@ module.exports = class Schema {
323
332
  const $field = field;
324
333
  const $model = model;
325
334
 
326
- $field.isPrimaryKey = Boolean($field.name === model.idField);
327
- $field.isPersistable = Util.uvl($field.isPersistable, model.isPersistable, true);
335
+ field.isPrimaryKey = Boolean(field.name === model.pkField);
336
+ field.isPersistable = Util.uvl(field.isPersistable, model.isPersistable, true);
328
337
 
329
338
  // Field resolution comes first (unshift)
330
339
  thunks.unshift(($schema) => {
340
+ $field.id ??= $model.id;
331
341
  $field.model = $schema.models[$field.type];
342
+ $field.linkBy ??= $field.model?.pkField;
332
343
  $field.crud = Util.uvl($field.crud, $field.model?.scope, 'crud');
333
- $field.linkBy ??= $field.model?.idField;
334
- $field.linkField = $field.isVirtual ? $model.fields[$model.idField] : $field;
344
+ $field.linkField = $field.isVirtual ? $model.fields[$model.pkField] : $field;
335
345
  $field.isFKReference = !$field.isPrimaryKey && $field.model?.isMarkedModel && !$field.model?.isEmbedded;
336
346
  $field.isEmbedded = Boolean($field.model && !$field.isFKReference && !$field.isPrimaryKey);
337
347
  $field.isScalar = scalars.includes($field.type);
@@ -459,7 +469,8 @@ module.exports = class Schema {
459
469
  enum AutoGraphPipelineEnum { ${Object.keys(Pipeline).filter(k => !k.startsWith('$')).join(' ')} }
460
470
 
461
471
  directive @${model}(
462
- id: String # Specify the ID/PK field (default "id")
472
+ id: String # Specify the ID strategy (eg. "toObjectId")
473
+ pk: String # Specify the PK field (default "id")
463
474
  key: String # Specify db table/collection name
464
475
  crud: AutoGraphMixed # CRUD API
465
476
  scope: AutoGraphMixed #
@@ -481,6 +492,7 @@ module.exports = class Schema {
481
492
  ) on OBJECT | INTERFACE
482
493
 
483
494
  directive @${field}(
495
+ id: String # Specify the ID strategy (eg. "toObjectId")
484
496
  key: String # Specify db key
485
497
  persist: Boolean # Persist this field (default true)
486
498
  connection: Boolean # Treat this field as a connection type (default false - rolling this out slowly)
@@ -498,11 +510,9 @@ module.exports = class Schema {
498
510
  validate: [AutoGraphPipelineEnum!] # Alias for finalize
499
511
 
500
512
  # TEMP TO APPEASE TRANSITION
501
- id: String # Specify the ModelRef this field FK References
502
513
  ref: AutoGraphMixed # Specify the modelRef field's name (overrides isEmbedded)
503
514
  gqlScope: AutoGraphMixed # Dictate how GraphQL API behaves
504
515
  dalScope: AutoGraphMixed # Dictate how the DAL behaves
505
- destruct: [AutoGraphPipelineEnum!]
506
516
  transform: [AutoGraphPipelineEnum!]
507
517
  deserialize: [AutoGraphPipelineEnum!]
508
518
  ) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | SCALAR