@coderich/autograph 0.13.107 → 0.13.108

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.108",
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
  }
@@ -611,6 +611,8 @@ module.exports = class Schema {
611
611
  },
612
612
  });
613
613
 
614
+ // console.log(this.#schema.models.Person.referentialIntegrity);
615
+
614
616
  // Return schema
615
617
  return this.#schema;
616
618
  }
@@ -640,17 +642,17 @@ module.exports = class Schema {
640
642
  }
641
643
 
642
644
  #findModelPathsToField(model, field) {
643
- if (!model.isEmbedded) return [{ model, field, path: [`${field}`] }];
645
+ if (!model.isEmbedded) return [{ model, field, path: [`${field}`], isArray: field.isArray }];
644
646
 
645
647
  const arr = [];
646
648
 
647
649
  Object.values(this.#schema.models).forEach((m) => {
648
650
  Util.traverse(Object.values(m.fields), (f, info) => {
649
651
  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}`) });
652
+ if (f.isEmbedded) return { value: Object.values(f.model.fields), info: { path, isArray: info.isArray || f.isArray } };
653
+ if (f.type === model.name) arr.push({ model: m, field, path: path.concat(`${field}`), isArray: info.isArray || field.isArray || f.isArray });
652
654
  return null;
653
- }, { path: [] });
655
+ }, { path: [], isArray: false });
654
656
  });
655
657
 
656
658
  return arr;