@coderich/autograph 0.13.6 → 0.13.7

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.6",
4
+ "version": "0.13.7",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -24,17 +24,24 @@ module.exports = class Schema {
24
24
  constructor(config) {
25
25
  this.#config = config;
26
26
  this.#config.namespace ??= 'autograph';
27
- this.#typeDefs = Schema.#framework();
27
+ this.#config.directives ??= {};
28
+ this.#config.directives.model ??= 'model';
29
+ this.#config.directives.field ??= 'field';
30
+ this.#config.directives.link ??= 'link';
31
+ this.#config.directives.index ??= 'index';
32
+ this.#typeDefs = Schema.#framework(this.#config.directives);
28
33
  }
29
34
 
30
35
  /**
31
36
  * Decorate each marked @model with config-driven field decorators
32
37
  */
33
38
  decorate() {
39
+ const { directives } = this.#config;
40
+
34
41
  this.#typeDefs = visit(this.#typeDefs, {
35
42
  enter: (node) => {
36
43
  if (modelKinds.includes(node.kind) && !operations.includes(node.name.value)) {
37
- const directive = node.directives.find(({ name }) => name.value === 'model');
44
+ const directive = node.directives.find(({ name }) => name.value === directives.model);
38
45
 
39
46
  if (directive) {
40
47
  const arg = directive.arguments.find(({ name }) => name.value === 'decorate');
@@ -94,7 +101,8 @@ module.exports = class Schema {
94
101
  if (this.#schema) return this.#schema;
95
102
 
96
103
  // const schema = buildASTSchema(this.#typeDefs);
97
- this.#schema = { types: {}, models: {}, indexes: [], namespace: this.#config.namespace };
104
+ const { directives, namespace } = this.#config;
105
+ this.#schema = { types: {}, models: {}, indexes: [], namespace };
98
106
  let model, field, isField, isList;
99
107
  const thunks = [];
100
108
 
@@ -141,8 +149,8 @@ module.exports = class Schema {
141
149
  const target = isField ? field : model;
142
150
  target.directives[name] = target.directives[name] || {};
143
151
 
144
- if (name === 'model') model.isMarkedModel = true;
145
- else if (name === 'index') this.#schema.indexes.push({ model });
152
+ if (name === directives.model) model.isMarkedModel = true;
153
+ else if (name === directives.index) this.#schema.indexes.push({ model });
146
154
 
147
155
  node.arguments.forEach((arg) => {
148
156
  const key = arg.name.value;
@@ -150,54 +158,54 @@ module.exports = class Schema {
150
158
  const value = values ? values.map(n => n.value) : val;
151
159
  target.directives[name][key] = value;
152
160
 
153
- if (name === 'index') this.#schema.indexes[this.#schema.indexes.length - 1][key] = value;
161
+ if (name === directives.index) this.#schema.indexes[this.#schema.indexes.length - 1][key] = value;
154
162
 
155
163
  switch (`${name}-${key}`) {
156
164
  // Model specific directives
157
- case 'model-id': {
165
+ case `${directives.model}-id`: {
158
166
  model.idField = value;
159
167
  break;
160
168
  }
161
- case 'model-source': {
169
+ case `${directives.model}-source`: {
162
170
  model.source = this.#config.dataSources?.[value];
163
171
  break;
164
172
  }
165
- case 'model-loader': {
173
+ case `${directives.model}-loader`: {
166
174
  model.loader = this.#config.dataLoaders?.[value];
167
175
  break;
168
176
  }
169
- case 'model-embed': {
177
+ case `${directives.model}-embed`: {
170
178
  model.isEmbedded = value;
171
179
  break;
172
180
  }
173
181
  // Field specific directives
174
- case 'field-default': {
182
+ case `${directives.field}-default`: {
175
183
  field.defaultValue = value;
176
184
  break;
177
185
  }
178
- case 'field-connection': {
186
+ case `${directives.field}-connection`: {
179
187
  field.isConnection = value;
180
188
  break;
181
189
  }
182
- case 'field-validate': { // Alias for finalize
190
+ case `${directives.field}-validate`: { // Alias for finalize
183
191
  target.pipelines.finalize = target.pipelines.finalize.concat(value).filter(Boolean);
184
192
  break;
185
193
  }
186
- case 'link-by': {
194
+ case `${directives.link}-by`: {
187
195
  field.linkBy = value;
188
196
  field.isVirtual = true;
189
197
  break;
190
198
  }
191
199
  // Generic by target directives
192
- case 'model-persist': case 'field-persist': {
200
+ case `${directives.model}-persist`: case `${directives.field}-persist`: {
193
201
  target.isPersistable = value;
194
202
  break;
195
203
  }
196
- case 'model-crud': case 'model-scope': case 'field-crud': {
204
+ case `${directives.model}-crud`: case `${directives.model}-scope`: case `${directives.field}-crud`: {
197
205
  target[key] = Util.nvl(value, '');
198
206
  break;
199
207
  }
200
- case 'model-key': case 'model-meta': case 'field-key': case 'field-onDelete': {
208
+ case `${directives.model}-key`: case `${directives.model}-meta`: case `${directives.field}-key`: case `${directives.field}-onDelete`: {
201
209
  target[key] = value;
202
210
  break;
203
211
  }
@@ -354,7 +362,9 @@ module.exports = class Schema {
354
362
  return this.#config.makeExecutableSchema(this.toObject());
355
363
  }
356
364
 
357
- static #framework() {
365
+ static #framework(directives) {
366
+ const { model, field, link, index } = directives;
367
+
358
368
  return parse(`
359
369
  scalar AutoGraphMixed
360
370
 
@@ -362,7 +372,7 @@ module.exports = class Schema {
362
372
  enum AutoGraphOnDeleteEnum { cascade nullify restrict defer }
363
373
  enum AutoGraphPipelineEnum { ${Object.keys(Pipeline).filter(k => !k.startsWith('$')).join(' ')} }
364
374
 
365
- directive @model(
375
+ directive @${model}(
366
376
  id: String # Specify the ID/PK field (default "id")
367
377
  key: String # Specify db table/collection name
368
378
  crud: AutoGraphMixed # CRUD API
@@ -374,7 +384,7 @@ module.exports = class Schema {
374
384
  persist: Boolean # Persist this model (default true)
375
385
  ) on OBJECT | INTERFACE
376
386
 
377
- directive @field(
387
+ directive @${field}(
378
388
  key: String # Specify db key
379
389
  persist: Boolean # Persist this field (default true)
380
390
  connection: Boolean # Treat this field as a connection type (default false - rolling this out slowly)
@@ -392,13 +402,13 @@ module.exports = class Schema {
392
402
  validate: [AutoGraphPipelineEnum!] # Alias for finalize
393
403
  ) on FIELD_DEFINITION | INPUT_FIELD_DEFINITION | SCALAR
394
404
 
395
- directive @link(
405
+ directive @${link}(
396
406
  to: AutoGraphMixed # The MODEL to link to (default's to modelRef)
397
407
  by: AutoGraphMixed! # The FIELD to match yourself by
398
408
  use: AutoGraphMixed # The VALUE to use (default's to @link'd value); useful for many-to-many relationships
399
409
  ) on FIELD_DEFINITION
400
410
 
401
- directive @index(
411
+ directive @${index}(
402
412
  name: String
403
413
  on: [AutoGraphMixed!]!
404
414
  type: AutoGraphIndexEnum!