@coderich/autograph 0.13.107 → 0.13.109

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.107",
4
+ "version": "0.13.109",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -67,13 +67,11 @@ module.exports = class QueryResolver extends QueryBuilder {
67
67
  }
68
68
  case 'pullOne': {
69
69
  return this.#get(query).then((doc) => {
70
- const [key] = Object.keys(input);
71
- const $query = Object.assign(query.toObject(), { doc });
72
- const args = { query: $query, resolver: this.#resolver, context: this.#context };
73
- const values = get(this.#model.transformers.create.transform(input, args), key, []);
74
- const $doc = Util.pathmap(key, doc, (arr) => {
75
- if (arr == null) return arr;
76
- return arr.filter(el => values.every(v => `${v}` !== `${el}`));
70
+ const [[path, inputs]] = Object.entries(input);
71
+ const [key] = path.split('.');
72
+ const $doc = Util.pathmap(path, doc, (mixed) => { // Pathmap because nested arrays
73
+ if (mixed == null) return mixed;
74
+ return mixed.filter(el => inputs.every(v => `${v}` !== `${el}`));
77
75
  });
78
76
  return this.#resolver.match(this.#model.name).id(doc.id).save({ [key]: get($doc, key) });
79
77
  });
@@ -86,12 +84,21 @@ module.exports = class QueryResolver extends QueryBuilder {
86
84
  }
87
85
  case 'spliceOne': {
88
86
  return this.#get(query).then((doc) => {
89
- const [key] = Object.keys(input);
90
- const $query = Object.assign(query.toObject(), { doc });
91
- const args = { query: $query, resolver: this.#resolver, context: this.#context };
92
- const [find, replace] = get(this.#model.transformers.create.transform(input, args), key);
93
- const $input = { [key]: (get(doc, key) || []).map(el => (`${el}` === `${find}` ? replace : el)) };
94
- return this.#resolver.match(this.#model.name).id(doc.id).save($input);
87
+ const [[path, [find, replace]]] = Object.entries(input);
88
+ const [key] = path.split('.');
89
+ const $doc = Util.pathmap(path, doc, (mixed) => { // Pathmap because nested arrays
90
+ if (mixed == null) return mixed;
91
+ if (Array.isArray(mixed)) return mixed.map(el => (`${el}` === `${find}` ? replace : el));
92
+ if (`${mixed}` === `${find}`) return replace;
93
+ return mixed;
94
+ });
95
+ return this.#resolver.match(this.#model.name).id(doc.id).save({ [key]: get($doc, key) });
96
+ });
97
+ }
98
+ case 'spliceMany': {
99
+ const [[key, values]] = Object.entries(input);
100
+ return this.#find(query).then((docs) => {
101
+ return Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).splice(key, ...values)));
95
102
  });
96
103
  }
97
104
  case 'deleteOne': {
@@ -123,15 +130,15 @@ module.exports = class QueryResolver extends QueryBuilder {
123
130
  #resolveReferentialIntegrity(doc) {
124
131
  const txn = this.#resolver;
125
132
 
126
- return Util.promiseChain(this.#model.referentialIntegrity.map(({ model, field, path }) => () => {
127
- const { onDelete, isArray, fkField } = field;
133
+ return Util.promiseChain(this.#model.referentialIntegrity.map(({ model, field, isArray, path }) => () => {
134
+ const { onDelete, fkField } = field;
128
135
  const id = doc[fkField];
129
136
  const $path = path.join('.');
130
137
  const where = field.isVirtual ? { [field.model.pkField]: get(doc, field.linkBy) } : { [$path]: id };
131
138
 
132
139
  switch (onDelete) {
133
140
  case 'cascade': return isArray ? txn.match(model).where(where).pull($path, id) : txn.match(model).where(where).remove();
134
- case 'nullify': return txn.match(model).where(where).save({ [$path]: null });
141
+ case 'nullify': return isArray ? txn.match(model).where(where).splice($path, id, null) : txn.match(model).where(where).save({ [$path]: null });
135
142
  case 'restrict': return txn.match(model).where(where).count().then(count => (count ? Promise.reject(new Error('Restricted')) : count));
136
143
  default: throw new Error(`Unknown onDelete operator: '${onDelete}'`);
137
144
  }
@@ -205,6 +205,7 @@ module.exports = class Schema {
205
205
  target.directives[name] = target.directives[name] || {};
206
206
 
207
207
  if (name === directives.model) {
208
+ model.isEntity = true;
208
209
  model.isMarkedModel = true;
209
210
  model.isEmbedded = false;
210
211
  } else if (name === directives.index) {
@@ -234,6 +235,7 @@ module.exports = class Schema {
234
235
  }
235
236
  case `${directives.model}-embed`: {
236
237
  model.isEmbedded = value;
238
+ model.isEntity = !value;
237
239
  break;
238
240
  }
239
241
  // Field specific directives
@@ -312,8 +314,6 @@ module.exports = class Schema {
312
314
 
313
315
  // Model resolution after field resolution (push)
314
316
  thunks.push(($schema) => {
315
- $model.isEntity = Boolean($model.isMarkedModel && !$model.isEmbedded);
316
-
317
317
  $model.resolvePath = (path, prop = 'name') => this.#schema.resolvePath(`${$model[prop]}.${path}`, prop);
318
318
 
319
319
  $model.isJoinPath = (path, prop = 'name') => {
@@ -640,17 +640,17 @@ module.exports = class Schema {
640
640
  }
641
641
 
642
642
  #findModelPathsToField(model, field) {
643
- if (!model.isEmbedded) return [{ model, field, path: [`${field}`] }];
643
+ if (!model.isEmbedded) return [{ model, field, path: [`${field}`], isArray: field.isArray }];
644
644
 
645
645
  const arr = [];
646
646
 
647
- Object.values(this.#schema.models).forEach((m) => {
647
+ Object.values(this.#schema.models).filter(m => m.isEntity).forEach((m) => {
648
648
  Util.traverse(Object.values(m.fields), (f, info) => {
649
649
  const path = info.path.concat(f.name);
650
- if (f.isEmbedded) return { value: Object.values(f.model.fields), info: { path } };
651
- if (f.type === model.name) arr.push({ model: m, field, path: path.concat(`${field}`) });
650
+ if (f.type === model.name) arr.push({ model: m, field, path: path.concat(`${field}`), isArray: info.isArray || field.isArray || f.isArray });
651
+ else if (f.isEmbedded) return { value: Object.values(f.model.fields), info: { path, isArray: info.isArray || f.isArray } };
652
652
  return null;
653
- }, { path: [] });
653
+ }, { path: [], isArray: false });
654
654
  });
655
655
 
656
656
  return arr;