@coderich/autograph 0.13.103 → 0.13.105

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.103",
4
+ "version": "0.13.105",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -18,10 +18,14 @@ class Emitter extends EventEmitter {
18
18
  return prev[isBasic ? 0 : 1].push(wrapper) && prev;
19
19
  }, [[], []]);
20
20
 
21
+ // Basic functions are not designed to be bound to the query execution so we need an isolated resolver from any transactions
22
+ const resolver = data?.resolver?.clone();
23
+ const basicData = { ...data, resolver };
24
+
21
25
  return new Promise((resolve, reject) => {
22
26
  // Basic functions run first; if they return a value they abort the flow of execution
23
27
  basicFuncs.sort(Emitter.sort).forEach((fn) => {
24
- const value = fn(data);
28
+ const value = fn(basicData);
25
29
  if (value !== undefined && !(value instanceof Promise)) throw new AbortEarlyError(value);
26
30
  });
27
31
 
@@ -142,10 +142,11 @@ module.exports = class Resolver {
142
142
  * @returns {*} - The promise resolution
143
143
  */
144
144
  run(promise) {
145
- return this.commit().then(() => promise);
146
- // return this.commit().then(() => promise).catch((e) => {
147
- // return this.rollback().then(() => Promise.reject(e));
148
- // });
145
+ return promise.catch((e) => {
146
+ return this.rollback().then(() => Promise.reject(e));
147
+ }).then((results) => {
148
+ return this.commit().then(() => results);
149
+ });
149
150
  }
150
151
 
151
152
  /**
@@ -308,7 +309,7 @@ module.exports = class Resolver {
308
309
  const type = query.isMutation ? 'Mutation' : 'Query';
309
310
  const event = this.#createEvent(query);
310
311
 
311
- const promise = Emitter.emit(`pre${type}`, event).then(async (resultEarly) => {
312
+ return Emitter.emit(`pre${type}`, event).then(async (resultEarly) => {
312
313
  if (resultEarly !== undefined) return resultEarly; // Nothing to validate/transform
313
314
  // if (query.crud === 'update' && Util.isEqual({ added: {}, updated: {}, deleted: {} }, Util.changeset(query.doc, query.input))) return query.doc;
314
315
 
@@ -319,9 +320,7 @@ module.exports = class Resolver {
319
320
  }
320
321
 
321
322
  return thunk(tquery);
322
- });
323
-
324
- promise.then((result) => {
323
+ }).then((result) => {
325
324
  event.result = result; // backwards compat
326
325
  query.result = result;
327
326
  return Emitter.emit(`post${type}`, event);
@@ -330,8 +329,6 @@ module.exports = class Resolver {
330
329
  // const { data = {} } = e;
331
330
  // throw Boom.boomify(e, { data: { ...event, ...data } });
332
331
  });
333
-
334
- return promise;
335
332
  }
336
333
 
337
334
  #createEvent(query) {
@@ -45,10 +45,10 @@ module.exports = class Transaction {
45
45
  }
46
46
 
47
47
  #close(op) {
48
- return Promise.all(this.#queries.map(q => q.promise())).finally(() => {
49
- return Promise.all(Array.from(this.#sourceMap.entries()).map(([client, promise]) => {
50
- return promise.then(transaction => transaction[op]());
51
- }));
52
- });
48
+ // return Promise.all(this.#queries.map(q => q.promise())).finally(() => {
49
+ return Promise.all(Array.from(this.#sourceMap.entries()).map(([client, promise]) => {
50
+ return promise.then(transaction => transaction[op]());
51
+ }));
52
+ // });
53
53
  }
54
54
  };
@@ -36,7 +36,7 @@ module.exports = class QueryResolver extends QueryBuilder {
36
36
  return this.#resolver.resolve(query);
37
37
  }
38
38
  case 'createMany': {
39
- return this.#resolver.transaction(false).run(Promise.all(input.map(el => this.#resolver.match(this.#model.name).save(el))));
39
+ return Promise.all(input.map(el => this.#resolver.match(this.#model.name).save(el)));
40
40
  }
41
41
  case 'updateOne': {
42
42
  return this.#get(query).then((doc) => {
@@ -46,7 +46,7 @@ module.exports = class QueryResolver extends QueryBuilder {
46
46
  }
47
47
  case 'updateMany': {
48
48
  return this.#find(query).then((docs) => {
49
- return this.#resolver.transaction(false).run(Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).save(input))));
49
+ return Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).save(input)));
50
50
  });
51
51
  }
52
52
  case 'pushOne': {
@@ -62,7 +62,7 @@ module.exports = class QueryResolver extends QueryBuilder {
62
62
  case 'pushMany': {
63
63
  const [[key, values]] = Object.entries(input);
64
64
  return this.#find(query).then((docs) => {
65
- return this.#resolver.transaction(false).run(Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).push(key, values))));
65
+ return Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).push(key, values)));
66
66
  });
67
67
  }
68
68
  case 'pullOne': {
@@ -81,7 +81,7 @@ module.exports = class QueryResolver extends QueryBuilder {
81
81
  case 'pullMany': {
82
82
  const [[key, values]] = Object.entries(input);
83
83
  return this.#find(query).then((docs) => {
84
- return this.#resolver.transaction(false).run(Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).pull(key, values))));
84
+ return Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).pull(key, values)));
85
85
  });
86
86
  }
87
87
  case 'spliceOne': {
@@ -103,7 +103,7 @@ module.exports = class QueryResolver extends QueryBuilder {
103
103
  }
104
104
  case 'deleteMany': {
105
105
  return this.#find(query).then((docs) => {
106
- return this.#resolver.transaction(false).run(Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).delete())));
106
+ return Promise.all(docs.map(doc => this.#resolver.match(this.#model.name).id(doc.id).delete()));
107
107
  });
108
108
  }
109
109
  default: {
@@ -121,9 +121,9 @@ module.exports = class QueryResolver extends QueryBuilder {
121
121
  }
122
122
 
123
123
  #resolveReferentialIntegrity(doc) {
124
- const txn = this.#resolver.transaction(false);
124
+ const txn = this.#resolver;
125
125
 
126
- return txn.run(Util.promiseChain(this.#model.referentialIntegrity.map(({ model, field, path }) => () => {
126
+ return Util.promiseChain(this.#model.referentialIntegrity.map(({ model, field, path }) => () => {
127
127
  const { onDelete, isArray, fkField } = field;
128
128
  const id = doc[fkField];
129
129
  const $path = path.join('.');
@@ -136,6 +136,6 @@ module.exports = class QueryResolver extends QueryBuilder {
136
136
  case 'defer': return Promise.resolve(); // Used for embedded models (could be improved)
137
137
  default: throw new Error(`Unknown onDelete operator: '${onDelete}'`);
138
138
  }
139
- })));
139
+ }));
140
140
  }
141
141
  };