@coderich/autograph 0.13.100 → 0.13.102

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.100",
4
+ "version": "0.13.102",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -231,6 +231,8 @@ module.exports = class Resolver {
231
231
  return thunk(tquery).then((result) => {
232
232
  if (flags?.required && (result == null || result?.length === 0)) throw Boom.notFound(`${model} Not Found`);
233
233
  return result;
234
+ }).finally(() => {
235
+ query.resolve();
234
236
  });
235
237
  });
236
238
  }
@@ -5,6 +5,7 @@ module.exports = class Transaction {
5
5
  #context;
6
6
  #resolver;
7
7
  #sourceMap;
8
+ #queries = [];
8
9
 
9
10
  constructor(config) {
10
11
  this.#schema = config.schema;
@@ -24,13 +25,15 @@ module.exports = class Transaction {
24
25
  }));
25
26
  }
26
27
 
27
- return new QueryResolverTransaction({
28
+ this.#queries.push(new QueryResolverTransaction({
28
29
  resolver: this.#resolver,
29
30
  schema: this.#schema,
30
31
  context: this.#context,
31
32
  transaction: this.#sourceMap.get(client),
32
33
  query: { model: `${model}` },
33
- });
34
+ }));
35
+
36
+ return this.#queries.at(-1);
34
37
  }
35
38
 
36
39
  commit() {
@@ -42,8 +45,10 @@ module.exports = class Transaction {
42
45
  }
43
46
 
44
47
  #close(op) {
45
- return Promise.all(Array.from(this.#sourceMap.entries()).map(([client, promise]) => {
46
- return promise.then(transaction => transaction[op]());
47
- }));
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
53
  }
49
54
  };
@@ -1,5 +1,5 @@
1
1
  const Util = require('@coderich/util');
2
- const { isGlob, globToRegex, mergeDeep, JSONParse } = require('../service/AppService');
2
+ const { isGlob, globToRegex, mergeDeep, JSONParse, withResolvers } = require('../service/AppService');
3
3
 
4
4
  module.exports = class Query {
5
5
  #config;
@@ -8,20 +8,30 @@ module.exports = class Query {
8
8
  #schema;
9
9
  #model;
10
10
  #query;
11
+ #resolution;
11
12
 
12
13
  constructor(config) {
13
- const { schema, context, resolver, query } = config;
14
+ const { schema, context, resolver, query, resolution = withResolvers() } = config;
14
15
  this.#config = config;
15
16
  this.#resolver = resolver;
16
17
  this.#context = context;
17
18
  this.#schema = schema;
18
19
  this.#model = schema.models[query.model];
19
20
  this.#query = query;
21
+ this.#resolution = resolution;
22
+ }
23
+
24
+ promise() {
25
+ return this.#resolution.promise;
26
+ }
27
+
28
+ resolve() {
29
+ this.#resolution.resolve();
20
30
  }
21
31
 
22
32
  clone(query) {
23
33
  query = { ...this.#query, ...query }; // NO deepMerge here; must replace fields entirely
24
- return new Query({ ...this.#config, query });
34
+ return new Query({ ...this.#config, query, resolution: this.#resolution });
25
35
  }
26
36
 
27
37
  toObject() {
@@ -1,7 +1,7 @@
1
1
  const get = require('lodash.get');
2
2
  const Util = require('@coderich/util');
3
3
  const QueryBuilder = require('./QueryBuilder');
4
- const { mergeDeep } = require('../service/AppService');
4
+ const { mergeDeep, withResolvers } = require('../service/AppService');
5
5
 
6
6
  module.exports = class QueryResolver extends QueryBuilder {
7
7
  #model;
@@ -9,6 +9,7 @@ module.exports = class QueryResolver extends QueryBuilder {
9
9
  #config;
10
10
  #context;
11
11
  #resolver;
12
+ #resolution = withResolvers(); // Promise for when the query is resolved
12
13
 
13
14
  constructor(config) {
14
15
  const { schema, context, resolver, query } = config;
@@ -20,8 +21,13 @@ module.exports = class QueryResolver extends QueryBuilder {
20
21
  this.#model = schema.models[query.model];
21
22
  }
22
23
 
24
+ promise() {
25
+ return this.#resolution.promise;
26
+ }
27
+
23
28
  terminate() {
24
29
  const query = super.terminate();
30
+ query.promise().then(this.#resolution.resolve).catch(this.#resolution.reject);
25
31
  const { op, args: { input } } = query.toObject();
26
32
 
27
33
  // Resolve
@@ -101,7 +107,7 @@ module.exports = class QueryResolver extends QueryBuilder {
101
107
  });
102
108
  }
103
109
  default: {
104
- throw new Error(`Unknown operation "${op}"`);
110
+ return Promise.reject(new Error(`Unknown operation "${op}"`));
105
111
  }
106
112
  }
107
113
  }
@@ -44,3 +44,9 @@ exports.JSONParse = (mixed) => {
44
44
  return undefined;
45
45
  }
46
46
  };
47
+
48
+ exports.withResolvers = () => {
49
+ let resolve, reject;
50
+ const promise = new Promise((res, rej) => { resolve = res; reject = rej; });
51
+ return { promise, resolve, reject };
52
+ };