@coderich/autograph 0.13.46 → 0.13.47

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.46",
4
+ "version": "0.13.47",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -221,6 +221,7 @@ module.exports = class QueryBuilder {
221
221
  const suffix = id || limit === 1 || (crud === 'create' && args.length < 2) ? 'One' : 'Many';
222
222
  let input = suffix === 'One' ? args[0] : args;
223
223
  if (input === undefined) input = {};
224
+ input.id = id;
224
225
  this.#query.args.input = input;
225
226
  return this.terminate(Object.assign(this.#query, {
226
227
  op: `${crud}${suffix}`,
@@ -34,6 +34,10 @@ module.exports = class QueryResolver extends QueryBuilder {
34
34
  }
35
35
  case 'updateOne': {
36
36
  return this.#get(query).then((doc) => {
37
+ // const $input = this.#model.walk(doc, (node) => {
38
+ // if (node.field.defaultValue !== undefined || ['instruct', 'restruct', 'serialize'].some(el => node.field.pipelines[el]?.length)) return node;
39
+ // return false;
40
+ // });
37
41
  const merged = mergeDeep({}, doc, Util.unflatten(input, { safe: true }));
38
42
  return this.#resolver.resolve(query.clone({ doc, input: merged }));
39
43
  });
@@ -81,7 +85,7 @@ module.exports = class QueryResolver extends QueryBuilder {
81
85
  }
82
86
  case 'deleteOne': {
83
87
  return this.#get(query).then((doc) => {
84
- return this.#resolveReferentialIntegrity(query).then(() => {
88
+ return this.#resolveReferentialIntegrity(doc).then(() => {
85
89
  return this.#resolver.resolve(query.clone({ doc })).then(() => doc);
86
90
  });
87
91
  });
@@ -105,19 +109,19 @@ module.exports = class QueryResolver extends QueryBuilder {
105
109
  return this.#resolver.resolve(query.clone({ op: 'findMany', key: `find${this.#model.name}`, crud: 'read', isMutation: false }));
106
110
  }
107
111
 
108
- #resolveReferentialIntegrity(query) {
109
- const { id } = query.toObject();
112
+ #resolveReferentialIntegrity(doc) {
113
+ const { id } = doc;
110
114
  const txn = this.#resolver.transaction(false);
111
115
 
112
116
  return txn.run(Util.promiseChain(this.#model.referentialIntegrity.map(({ model, field, path }) => () => {
113
117
  const { onDelete, isArray } = field;
114
118
  const $path = path.join('.');
115
- const $where = { [$path]: id };
119
+ const where = field.isVirtual ? { [field.model.pkField]: get(doc, field.linkBy) } : { [$path]: id };
116
120
 
117
121
  switch (onDelete) {
118
- case 'cascade': return isArray ? txn.match(model).where($where).pull($path, id) : txn.match(model).where($where).remove();
119
- case 'nullify': return txn.match(model).where($where).save({ [$path]: null });
120
- case 'restrict': return txn.match(model).where($where).count().then(count => (count ? Promise.reject(new Error('Restricted')) : count));
122
+ case 'cascade': return isArray ? txn.match(model).where(where).pull($path, id) : txn.match(model).where(where).remove();
123
+ case 'nullify': return txn.match(model).where(where).save({ [$path]: null });
124
+ case 'restrict': return txn.match(model).where(where).count().then(count => (count ? Promise.reject(new Error('Restricted')) : count));
121
125
  case 'defer': return Promise.resolve(); // Used for embedded models (could be improved)
122
126
  default: throw new Error(`Unknown onDelete operator: '${onDelete}'`);
123
127
  }