@coderich/autograph 0.13.99 → 0.13.101

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.99",
4
+ "version": "0.13.101",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -72,7 +72,7 @@ module.exports = class Resolver {
72
72
  * @returns {QueryResolver|QueryResolverTransaction} - An API to build and execute a query
73
73
  */
74
74
  match(model) {
75
- return this.#sessions.slice(-1).pop()?.slice(-1).pop()?.match(model) ?? new QueryResolver({
75
+ return this.#sessions.at(-1)?.at(-1)?.match(model) ?? new QueryResolver({
76
76
  resolver: this,
77
77
  schema: this.#schema,
78
78
  context: this.#context,
@@ -104,8 +104,8 @@ module.exports = class Resolver {
104
104
  transaction(isolated = true, parent = this) {
105
105
  if (isolated) return this.clone().transaction(false, parent);
106
106
 
107
- const currSession = this.#sessions.slice(-1).pop();
108
- const currTransaction = currSession?.slice(-1).pop();
107
+ const currSession = this.#sessions.at(-1);
108
+ const currTransaction = currSession?.at(-1);
109
109
  const realTransaction = new Transaction({ resolver: this, schema: this.#schema, context: this.#context });
110
110
  const thunks = currTransaction ? currSession.thunks : []; // If in a transaction, piggy back off session
111
111
 
@@ -194,7 +194,7 @@ module.exports = class Resolver {
194
194
  async resolve(query) {
195
195
  let thunk;
196
196
  const { doc, model, crud, isMutation, flags } = query.toObject();
197
- const currSession = this.#sessions.slice(-1).pop();
197
+ const currSession = this.#sessions.at(-1);
198
198
 
199
199
  if (isMutation) {
200
200
  thunk = tquery => this.#schema.models[model].source.client.resolve(tquery.toDriver().toObject()).then((results) => {
@@ -336,7 +336,7 @@ module.exports = class Resolver {
336
336
  query.match = event.args.where;
337
337
  query.toObject = () => query;
338
338
  event.merged = event.input;
339
- event.input = event.args?.input;
339
+ event.input = Util.unflatten(event.args?.input, { safe: true });
340
340
  event.doc ??= {};
341
341
 
342
342
  return event;
@@ -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())).then(() => {
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,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,7 +21,21 @@ 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() {
29
+ return this.#terminate().then((result) => {
30
+ this.#resolution.resolve(result);
31
+ return result;
32
+ }).catch((e) => {
33
+ this.#resolution.reject(e);
34
+ return Promise.reject(e);
35
+ });
36
+ }
37
+
38
+ #terminate() {
24
39
  const query = super.terminate();
25
40
  const { op, args: { input } } = query.toObject();
26
41
 
@@ -101,7 +116,7 @@ module.exports = class QueryResolver extends QueryBuilder {
101
116
  });
102
117
  }
103
118
  default: {
104
- throw new Error(`Unknown operation "${op}"`);
119
+ return Promise.reject(new Error(`Unknown operation "${op}"`));
105
120
  }
106
121
  }
107
122
  }
@@ -495,7 +495,7 @@ module.exports = class Schema {
495
495
 
496
496
  Util.traverse(Object.values($model.fields), (f, info) => {
497
497
  const path = info.path.concat(f.name);
498
- if (f.isEmbedded) return { value: f.model.fields, info: { path } };
498
+ if (f.isEmbedded) return { value: Object.values(f.model.fields), info: { path } };
499
499
  if (f.isScalar) $model.ignorePaths.push(path.join('.'));
500
500
  return null;
501
501
  }, { path: [] });
@@ -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
+ };