@coderich/autograph 0.10.16 → 0.10.18

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
  "author": "Richard Livolsi (coderich)",
4
- "version": "0.10.16",
4
+ "version": "0.10.18",
5
5
  "description": "AutoGraph",
6
6
  "keywords": [
7
7
  "graphql",
@@ -1,161 +1,96 @@
1
- const { flatten } = require('lodash');
1
+ const { get } = require('lodash');
2
2
  const TreeMap = require('./TreeMap');
3
3
  const QueryBuilderTransaction = require('../query/QueryBuilderTransaction');
4
4
 
5
+ const makeMap = (resolver) => {
6
+ let resolve, reject;
7
+ const map = new TreeMap();
8
+ map.promise = new Promise((good, bad) => { resolve = good; reject = bad; });
9
+ map.resolve = resolve;
10
+ map.reject = reject;
11
+
12
+ map.ready = () => {
13
+ const elements = map.elements();
14
+ const notReady = elements.filter(el => !el.marker);
15
+ if (notReady.length) return [undefined, undefined];
16
+ let rollbackIndex = elements.findIndex(el => el.marker === 'rollback');
17
+ if (rollbackIndex === -1) rollbackIndex = Infinity;
18
+ return [elements.slice(0, rollbackIndex), elements.slice(rollbackIndex)];
19
+ };
20
+
21
+ map.perform = () => {
22
+ const [commits, rollbacks] = map.ready();
23
+
24
+ if (commits && rollbacks) {
25
+ const rollbackData = rollbacks.map(tnx => tnx.data).flat();
26
+ const commitData = commits.map(tnx => tnx.data).flat();
27
+
28
+ Promise.all(rollbackData.map(rbd => rbd.$rollback())).then(() => {
29
+ if (commits.length) resolver.clearAll();
30
+ Promise.all(commitData.map(cd => cd.$commit())).then(d => map.resolve(d));
31
+ }).catch(e => map.reject(e));
32
+ }
33
+
34
+ return map.promise;
35
+ };
36
+
37
+ return map;
38
+ };
39
+
5
40
  module.exports = class DataTransaction {
6
41
  constructor(resolver, parentTxn) {
7
- const txnMap = (parentTxn || {}).txnMap || (() => {
8
- let resolve, reject;
9
- const map = new TreeMap();
10
- map.promise = new Promise((good, bad) => { resolve = good; reject = bad; });
11
- map.resolve = resolve;
12
- map.reject = reject;
13
-
14
- map.ready = () => {
15
- const elements = map.elements();
16
- const notReady = elements.filter(el => !el.marker);
17
- if (notReady.length) return [undefined, undefined];
18
- let rollbackIndex = elements.findIndex(el => el.marker === 'rollback');
19
- if (rollbackIndex === -1) rollbackIndex = Infinity;
20
- return [elements.slice(0, rollbackIndex), elements.slice(rollbackIndex)];
21
- };
22
-
23
- map.perform = () => {
24
- const [commits, rollbacks] = map.ready();
25
-
26
- if (commits && rollbacks) {
27
- const rollbackData = flatten(rollbacks.map(tnx => tnx.data));
28
- const commitData = flatten(commits.map(tnx => tnx.data));
29
-
30
- Promise.all(rollbackData.map(rbd => rbd.$rollback())).then(() => {
31
- if (commits.length) resolver.clearAll();
32
- Promise.all(commitData.map(cd => cd.$commit())).then(d => map.resolve(d));
33
- }).catch(e => map.reject(e));
34
- }
35
-
36
- return map.promise;
37
- };
38
-
39
- return map;
40
- })();
41
-
42
- // Create txn
43
- const txn = ((data, driverMap, txMap) => {
44
- return {
45
- get match() {
46
- return (modelName) => {
47
- const model = resolver.toModelMarked(modelName);
48
- const driver = model.getDriver();
49
- if (!driverMap.has(driver)) driverMap.set(driver, []);
50
- const op = new QueryBuilderTransaction(resolver, model, this);
51
- driverMap.get(driver).push(op);
52
- return op;
53
- };
54
- },
55
- get exec() {
56
- return () => {
57
- return Promise.all(Array.from(driverMap.entries()).map(([driver, ops]) => {
58
- if (driver.getDirectives().transactions === false) {
59
- return Promise.all(ops.map(op => op.exec())).then((results) => {
60
- results.$commit = () => resolver.clearAll();
61
- results.$rollback = () => resolver.clearAll();
62
- return results;
63
- });
64
- }
65
-
66
- return driver.transaction(ops);
67
- })).then((results) => {
68
- data = results;
69
- return flatten(results);
70
- });
71
- };
72
- },
73
- get run() {
74
- return () => {
75
- return this.exec().then((results) => {
76
- if (txMap.root(this) === this) return this.commit().then(() => results);
77
- this.commit();
78
- return results;
79
- }).catch((e) => {
80
- if (txMap.root(this) === this) return this.rollback().then(() => Promise.reject(e));
81
- this.rollback();
82
- throw e;
83
- });
84
- };
85
- },
86
- get commit() {
87
- return () => {
88
- if (this.marker !== 'rollback') this.marker = 'commit';
89
- return txMap.perform();
90
- };
91
- },
92
- get rollback() {
93
- return () => {
94
- this.marker = 'rollback';
95
- return txMap.perform();
96
- };
97
- },
98
- get data() {
99
- return data;
100
- },
101
- get txnMap() {
102
- return txMap;
103
- },
104
- };
105
- })([], new Map(), txnMap);
106
-
107
- // Save txn to map
108
- txnMap.add(parentTxn, txn);
109
-
110
- // Return to caller
111
- return txn;
42
+ this.data = [];
43
+ this.resolver = resolver;
44
+ this.driverMap = new Map();
45
+ this.txnMap = get(parentTxn, 'txnMap') || makeMap(resolver);
46
+ this.txnMap.add(parentTxn, this);
112
47
  }
113
48
 
114
- // match(modelish) {
115
- // const model = this.resolver.toModelMarked(modelish);
116
- // const driver = model.getDriver();
117
- // if (!this.driverMap.has(driver)) this.driverMap.set(driver, []);
118
- // const op = new QueryBuilderTransaction(model, this.resolver, this);
119
- // this.driverMap.get(driver).push(op);
120
- // return op;
121
- // }
122
-
123
- // exec() {
124
- // return Promise.all(Array.from(this.driverMap.entries()).map(([driver, ops]) => {
125
- // if (driver.getDirectives().transactions === false) {
126
- // return Promise.all(ops.map(op => op.exec())).then((results) => {
127
- // results.$commit = () => this.resolver.clearAll();
128
- // results.$rollback = () => this.resolver.clearAll();
129
- // return results;
130
- // });
131
- // }
49
+ match(modelish) {
50
+ const model = this.resolver.toModelMarked(modelish);
51
+ const driver = model.getDriver();
52
+ if (!this.driverMap.has(driver)) this.driverMap.set(driver, []);
53
+ const op = new QueryBuilderTransaction(this.resolver, model, this);
54
+ this.driverMap.get(driver).push(op);
55
+ return op;
56
+ }
132
57
 
133
- // return driver.transaction(ops);
134
- // })).then((results) => {
135
- // this.data = results;
136
- // return flatten(results);
137
- // });
138
- // }
58
+ exec() {
59
+ return Promise.all(Array.from(this.driverMap.entries()).map(([driver, ops]) => {
60
+ if (driver.getDirectives().transactions === false) {
61
+ return Promise.all(ops.map(op => op.exec())).then((results) => {
62
+ results.$commit = () => this.resolver.clearAll();
63
+ results.$rollback = () => this.resolver.clearAll();
64
+ return results;
65
+ });
66
+ }
67
+
68
+ return driver.transaction(ops);
69
+ })).then((results) => {
70
+ this.data = results;
71
+ return results.flat();
72
+ });
73
+ }
139
74
 
140
- // run() {
141
- // return this.exec().then((results) => {
142
- // if (this.txMap.root(this) === this) return this.commit().then(() => results);
143
- // this.commit();
144
- // return results;
145
- // }).catch((e) => {
146
- // if (this.txMap.root(this) === this) return this.rollback().then(() => Promise.reject(e));
147
- // this.rollback();
148
- // throw e;
149
- // });
150
- // }
75
+ run() {
76
+ return this.exec().then((results) => {
77
+ if (this.txnMap.root(this) === this) return this.commit().then(() => results);
78
+ this.commit();
79
+ return results;
80
+ }).catch((e) => {
81
+ if (this.txnMap.root(this) === this) return this.rollback().then(() => Promise.reject(e));
82
+ this.rollback();
83
+ throw e;
84
+ });
85
+ }
151
86
 
152
- // commit() {
153
- // if (this.marker !== 'rollback') this.marker = 'commit';
154
- // return this.txMap.perform();
155
- // }
87
+ commit() {
88
+ if (this.marker !== 'rollback') this.marker = 'commit';
89
+ return this.txnMap.perform();
90
+ }
156
91
 
157
- // rollback() {
158
- // this.marker = 'rollback';
159
- // return this.txMap.perform();
160
- // }
92
+ rollback() {
93
+ this.marker = 'rollback';
94
+ return this.txnMap.perform();
95
+ }
161
96
  };
@@ -99,25 +99,23 @@ module.exports = class MongoDriver {
99
99
  }
100
100
 
101
101
  transaction(ops) {
102
- const promise = async () => {
102
+ return promiseRetry(() => {
103
103
  // Create session and start transaction
104
- const session = await this.connection.then(client => client.startSession({ readPreference: { mode: 'primary' } }));
105
- session.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
106
- const close = () => { session.endSession(); };
107
-
108
- // Execute each operation with session
109
- return Promise.all(ops.map(op => op.exec({ session }))).then((results) => {
110
- results.$commit = () => session.commitTransaction().then(close);
111
- results.$rollback = () => session.abortTransaction().then(close);
112
- return results;
113
- }).catch((e) => {
114
- close();
115
- throw e;
104
+ return this.connection.then(client => client.startSession({ readPreference: { mode: 'primary' } })).then((session) => {
105
+ session.startTransaction({ readConcern: { level: 'snapshot' }, writeConcern: { w: 'majority' } });
106
+ const close = () => { session.endSession(); };
107
+
108
+ // Execute each operation with session
109
+ return Promise.all(ops.map(op => op.exec({ session }))).then((results) => {
110
+ results.$commit = () => session.commitTransaction().then(close);
111
+ results.$rollback = () => session.abortTransaction().then(close);
112
+ return results;
113
+ }).catch((e) => {
114
+ close();
115
+ throw e;
116
+ });
116
117
  });
117
- };
118
-
119
- // Retry promise conditionally
120
- return promiseRetry(promise, 200, 5, e => e.errorLabels && e.errorLabels.indexOf('TransientTransactionError') > -1);
118
+ }, 200, 5, e => e.errorLabels && e.errorLabels.indexOf('TransientTransactionError') > -1);
121
119
  }
122
120
 
123
121
  static idKey() {