@aws-amplify/datastore 4.7.6-api-v6-models.b3abc9b.0 → 5.0.0

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.
Files changed (187) hide show
  1. package/README.md +4 -0
  2. package/lib/authModeStrategies/defaultAuthStrategy.js +3 -2
  3. package/lib/authModeStrategies/index.js +3 -3
  4. package/lib/authModeStrategies/multiAuthStrategy.js +38 -53
  5. package/lib/datastore/datastore.d.ts +4 -5
  6. package/lib/datastore/datastore.js +929 -1284
  7. package/lib/index.d.ts +1 -1
  8. package/lib/index.js +26 -13
  9. package/lib/predicates/index.js +54 -69
  10. package/lib/predicates/next.d.ts +2 -2
  11. package/lib/predicates/next.js +313 -462
  12. package/lib/predicates/sort.js +24 -28
  13. package/lib/ssr/index.js +2 -2
  14. package/lib/storage/adapter/AsyncStorageAdapter.js +120 -342
  15. package/lib/storage/adapter/AsyncStorageDatabase.js +217 -421
  16. package/lib/storage/adapter/InMemoryStore.js +28 -51
  17. package/lib/storage/adapter/InMemoryStore.native.js +5 -3
  18. package/lib/storage/adapter/IndexedDBAdapter.js +466 -871
  19. package/lib/storage/adapter/StorageAdapterBase.js +180 -330
  20. package/lib/storage/adapter/getDefaultAdapter/index.js +8 -10
  21. package/lib/storage/adapter/getDefaultAdapter/index.native.js +5 -4
  22. package/lib/storage/adapter/index.js +0 -1
  23. package/lib/storage/relationship.js +177 -253
  24. package/lib/storage/storage.d.ts +4 -4
  25. package/lib/storage/storage.js +255 -433
  26. package/lib/sync/datastoreConnectivity.d.ts +2 -2
  27. package/lib/sync/datastoreConnectivity.js +29 -39
  28. package/lib/sync/datastoreReachability/index.d.ts +1 -3
  29. package/lib/sync/datastoreReachability/index.js +3 -3
  30. package/lib/sync/datastoreReachability/index.native.d.ts +1 -3
  31. package/lib/sync/datastoreReachability/index.native.js +4 -5
  32. package/lib/sync/index.d.ts +2 -2
  33. package/lib/sync/index.js +522 -827
  34. package/lib/sync/merger.js +31 -63
  35. package/lib/sync/outbox.js +148 -232
  36. package/lib/sync/processors/errorMaps.d.ts +1 -1
  37. package/lib/sync/processors/errorMaps.js +30 -47
  38. package/lib/sync/processors/mutation.d.ts +2 -2
  39. package/lib/sync/processors/mutation.js +343 -502
  40. package/lib/sync/processors/subscription.d.ts +5 -2
  41. package/lib/sync/processors/subscription.js +283 -437
  42. package/lib/sync/processors/sync.d.ts +2 -2
  43. package/lib/sync/processors/sync.js +279 -404
  44. package/lib/sync/utils.d.ts +5 -4
  45. package/lib/sync/utils.js +267 -320
  46. package/lib/tsconfig.tsbuildinfo +1 -0
  47. package/lib/types.d.ts +138 -140
  48. package/lib/types.js +17 -24
  49. package/lib/util.d.ts +9 -17
  50. package/lib/util.js +387 -511
  51. package/lib-esm/authModeStrategies/defaultAuthStrategy.js +1 -2
  52. package/lib-esm/authModeStrategies/index.js +0 -1
  53. package/lib-esm/authModeStrategies/multiAuthStrategy.js +35 -52
  54. package/lib-esm/datastore/datastore.d.ts +4 -5
  55. package/lib-esm/datastore/datastore.js +888 -1247
  56. package/lib-esm/index.d.ts +1 -1
  57. package/lib-esm/index.js +6 -7
  58. package/lib-esm/predicates/index.js +53 -70
  59. package/lib-esm/predicates/next.d.ts +2 -2
  60. package/lib-esm/predicates/next.js +306 -459
  61. package/lib-esm/predicates/sort.js +23 -28
  62. package/lib-esm/ssr/index.js +1 -2
  63. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +111 -338
  64. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +212 -416
  65. package/lib-esm/storage/adapter/InMemoryStore.js +27 -52
  66. package/lib-esm/storage/adapter/InMemoryStore.native.js +0 -1
  67. package/lib-esm/storage/adapter/IndexedDBAdapter.js +438 -866
  68. package/lib-esm/storage/adapter/StorageAdapterBase.js +173 -325
  69. package/lib-esm/storage/adapter/getDefaultAdapter/index.js +2 -6
  70. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js +1 -2
  71. package/lib-esm/storage/adapter/index.js +1 -1
  72. package/lib-esm/storage/relationship.js +173 -251
  73. package/lib-esm/storage/storage.d.ts +4 -4
  74. package/lib-esm/storage/storage.js +242 -424
  75. package/lib-esm/sync/datastoreConnectivity.d.ts +2 -2
  76. package/lib-esm/sync/datastoreConnectivity.js +28 -39
  77. package/lib-esm/sync/datastoreReachability/index.d.ts +1 -3
  78. package/lib-esm/sync/datastoreReachability/index.js +2 -3
  79. package/lib-esm/sync/datastoreReachability/index.native.d.ts +1 -3
  80. package/lib-esm/sync/datastoreReachability/index.native.js +3 -4
  81. package/lib-esm/sync/index.d.ts +2 -2
  82. package/lib-esm/sync/index.js +502 -812
  83. package/lib-esm/sync/merger.js +28 -61
  84. package/lib-esm/sync/outbox.js +143 -228
  85. package/lib-esm/sync/processors/errorMaps.d.ts +1 -1
  86. package/lib-esm/sync/processors/errorMaps.js +32 -50
  87. package/lib-esm/sync/processors/mutation.d.ts +2 -2
  88. package/lib-esm/sync/processors/mutation.js +329 -490
  89. package/lib-esm/sync/processors/subscription.d.ts +5 -2
  90. package/lib-esm/sync/processors/subscription.js +266 -421
  91. package/lib-esm/sync/processors/sync.d.ts +2 -2
  92. package/lib-esm/sync/processors/sync.js +271 -397
  93. package/lib-esm/sync/utils.d.ts +5 -4
  94. package/lib-esm/sync/utils.js +252 -307
  95. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  96. package/lib-esm/types.d.ts +138 -140
  97. package/lib-esm/types.js +16 -25
  98. package/lib-esm/util.d.ts +9 -17
  99. package/lib-esm/util.js +335 -497
  100. package/package.json +31 -26
  101. package/src/authModeStrategies/multiAuthStrategy.ts +15 -12
  102. package/src/datastore/datastore.ts +36 -35
  103. package/src/predicates/sort.ts +3 -1
  104. package/src/storage/adapter/InMemoryStore.ts +1 -1
  105. package/src/storage/adapter/IndexedDBAdapter.ts +2 -2
  106. package/src/storage/adapter/StorageAdapterBase.ts +2 -2
  107. package/src/storage/adapter/getDefaultAdapter/index.ts +1 -4
  108. package/src/storage/storage.ts +29 -24
  109. package/src/sync/datastoreConnectivity.ts +6 -6
  110. package/src/sync/datastoreReachability/index.native.ts +5 -3
  111. package/src/sync/datastoreReachability/index.ts +1 -1
  112. package/src/sync/index.ts +79 -89
  113. package/src/sync/processors/errorMaps.ts +7 -7
  114. package/src/sync/processors/mutation.ts +19 -13
  115. package/src/sync/processors/subscription.ts +221 -295
  116. package/src/sync/processors/sync.ts +11 -8
  117. package/src/sync/utils.ts +30 -15
  118. package/src/types.ts +4 -8
  119. package/src/util.ts +46 -9
  120. package/lib/.tsbuildinfo +0 -3
  121. package/lib/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  122. package/lib/authModeStrategies/index.js.map +0 -1
  123. package/lib/authModeStrategies/multiAuthStrategy.js.map +0 -1
  124. package/lib/datastore/datastore.js.map +0 -1
  125. package/lib/index.js.map +0 -1
  126. package/lib/predicates/index.js.map +0 -1
  127. package/lib/predicates/next.js.map +0 -1
  128. package/lib/predicates/sort.js.map +0 -1
  129. package/lib/ssr/index.js.map +0 -1
  130. package/lib/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  131. package/lib/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  132. package/lib/storage/adapter/InMemoryStore.js.map +0 -1
  133. package/lib/storage/adapter/InMemoryStore.native.js.map +0 -1
  134. package/lib/storage/adapter/IndexedDBAdapter.js.map +0 -1
  135. package/lib/storage/adapter/StorageAdapterBase.js.map +0 -1
  136. package/lib/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  137. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  138. package/lib/storage/adapter/index.js.map +0 -1
  139. package/lib/storage/relationship.js.map +0 -1
  140. package/lib/storage/storage.js.map +0 -1
  141. package/lib/sync/datastoreConnectivity.js.map +0 -1
  142. package/lib/sync/datastoreReachability/index.js.map +0 -1
  143. package/lib/sync/datastoreReachability/index.native.js.map +0 -1
  144. package/lib/sync/index.js.map +0 -1
  145. package/lib/sync/merger.js.map +0 -1
  146. package/lib/sync/outbox.js.map +0 -1
  147. package/lib/sync/processors/errorMaps.js.map +0 -1
  148. package/lib/sync/processors/mutation.js.map +0 -1
  149. package/lib/sync/processors/subscription.js.map +0 -1
  150. package/lib/sync/processors/sync.js.map +0 -1
  151. package/lib/sync/utils.js.map +0 -1
  152. package/lib/types.js.map +0 -1
  153. package/lib/util.js.map +0 -1
  154. package/lib-esm/.tsbuildinfo +0 -3
  155. package/lib-esm/authModeStrategies/defaultAuthStrategy.js.map +0 -1
  156. package/lib-esm/authModeStrategies/index.js.map +0 -1
  157. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +0 -1
  158. package/lib-esm/datastore/datastore.js.map +0 -1
  159. package/lib-esm/index.js.map +0 -1
  160. package/lib-esm/predicates/index.js.map +0 -1
  161. package/lib-esm/predicates/next.js.map +0 -1
  162. package/lib-esm/predicates/sort.js.map +0 -1
  163. package/lib-esm/ssr/index.js.map +0 -1
  164. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +0 -1
  165. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +0 -1
  166. package/lib-esm/storage/adapter/InMemoryStore.js.map +0 -1
  167. package/lib-esm/storage/adapter/InMemoryStore.native.js.map +0 -1
  168. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +0 -1
  169. package/lib-esm/storage/adapter/StorageAdapterBase.js.map +0 -1
  170. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +0 -1
  171. package/lib-esm/storage/adapter/getDefaultAdapter/index.native.js.map +0 -1
  172. package/lib-esm/storage/adapter/index.js.map +0 -1
  173. package/lib-esm/storage/relationship.js.map +0 -1
  174. package/lib-esm/storage/storage.js.map +0 -1
  175. package/lib-esm/sync/datastoreConnectivity.js.map +0 -1
  176. package/lib-esm/sync/datastoreReachability/index.js.map +0 -1
  177. package/lib-esm/sync/datastoreReachability/index.native.js.map +0 -1
  178. package/lib-esm/sync/index.js.map +0 -1
  179. package/lib-esm/sync/merger.js.map +0 -1
  180. package/lib-esm/sync/outbox.js.map +0 -1
  181. package/lib-esm/sync/processors/errorMaps.js.map +0 -1
  182. package/lib-esm/sync/processors/mutation.js.map +0 -1
  183. package/lib-esm/sync/processors/subscription.js.map +0 -1
  184. package/lib-esm/sync/processors/sync.js.map +0 -1
  185. package/lib-esm/sync/utils.js.map +0 -1
  186. package/lib-esm/types.js.map +0 -1
  187. package/lib-esm/util.js.map +0 -1
@@ -1,16 +1,16 @@
1
- import { __assign, __awaiter, __generator, __read, __rest, __values } from "tslib";
2
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
2
  // SPDX-License-Identifier: Apache-2.0
4
- import { Logger, Mutex } from '@aws-amplify/core';
5
- import PushStream from 'zen-push';
3
+ import { filter, map, Subject } from 'rxjs';
6
4
  import { ModelPredicateCreator } from '../predicates';
7
5
  import { OpType, QueryOne, isTargetNameAssociation, } from '../types';
8
6
  import { isModelConstructor, STORAGE, validatePredicate, valuesEqual, } from '../util';
9
7
  import { getIdentifierValue } from '../sync/utils';
10
8
  import getDefaultAdapter from './adapter/getDefaultAdapter';
11
- var logger = new Logger('DataStore');
12
- var StorageClass = /** @class */ (function () {
13
- function StorageClass(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
9
+ import { Mutex } from '@aws-amplify/core/internals/utils';
10
+ import { ConsoleLogger } from '@aws-amplify/core';
11
+ const logger = new ConsoleLogger('DataStore');
12
+ class StorageClass {
13
+ constructor(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
14
14
  this.schema = schema;
15
15
  this.namespaceResolver = namespaceResolver;
16
16
  this.getModelConstructorByModelName = getModelConstructorByModelName;
@@ -18,10 +18,10 @@ var StorageClass = /** @class */ (function () {
18
18
  this.adapter = adapter;
19
19
  this.sessionId = sessionId;
20
20
  this.adapter = this.adapter || getDefaultAdapter();
21
- this.pushStream = new PushStream();
21
+ this.pushStream = new Subject();
22
22
  }
23
- StorageClass.getNamespace = function () {
24
- var namespace = {
23
+ static getNamespace() {
24
+ const namespace = {
25
25
  name: STORAGE,
26
26
  relationships: {},
27
27
  enums: {},
@@ -29,196 +29,138 @@ var StorageClass = /** @class */ (function () {
29
29
  nonModels: {},
30
30
  };
31
31
  return namespace;
32
- };
33
- StorageClass.prototype.init = function () {
34
- return __awaiter(this, void 0, void 0, function () {
35
- var resolve, reject;
36
- return __generator(this, function (_a) {
37
- switch (_a.label) {
38
- case 0:
39
- if (!(this.initialized !== undefined)) return [3 /*break*/, 2];
40
- return [4 /*yield*/, this.initialized];
41
- case 1:
42
- _a.sent();
43
- return [2 /*return*/];
44
- case 2:
45
- logger.debug('Starting Storage');
46
- this.initialized = new Promise(function (res, rej) {
47
- resolve = res;
48
- reject = rej;
49
- });
50
- this.adapter.setUp(this.schema, this.namespaceResolver, this.modelInstanceCreator, this.getModelConstructorByModelName, this.sessionId).then(resolve, reject);
51
- return [4 /*yield*/, this.initialized];
52
- case 3:
53
- _a.sent();
54
- return [2 /*return*/];
55
- }
56
- });
57
- });
58
- };
59
- StorageClass.prototype.save = function (model, condition, mutator, patchesTuple) {
60
- return __awaiter(this, void 0, void 0, function () {
61
- var result;
62
- var _this = this;
63
- return __generator(this, function (_a) {
64
- switch (_a.label) {
65
- case 0: return [4 /*yield*/, this.init()];
66
- case 1:
67
- _a.sent();
68
- if (!this.adapter) {
69
- throw new Error('Storage adapter is missing');
70
- }
71
- return [4 /*yield*/, this.adapter.save(model, condition)];
72
- case 2:
73
- result = _a.sent();
74
- result.forEach(function (r) {
75
- var _a = __read(r, 2), savedElement = _a[0], opType = _a[1];
76
- // truthy when save is called by the Merger
77
- var syncResponse = !!mutator;
78
- var updateMutationInput;
79
- // don't attempt to calc mutation input when storage.save
80
- // is called by Merger, i.e., when processing an AppSync response
81
- if ((opType === OpType.UPDATE || opType === OpType.INSERT) &&
82
- !syncResponse) {
83
- //
84
- // TODO: LOOK!!!
85
- // the `model` used here is in effect regardless of what model
86
- // comes back from adapter.save().
87
- // Prior to fix, SQLite adapter had been returning two models
88
- // of different types, resulting in invalid outbox entries.
89
- //
90
- // the bug is essentially fixed in SQLite adapter.
91
- // leaving as-is, because it's currently unclear whether anything
92
- // depends on this remaining as-is.
93
- //
94
- updateMutationInput = _this.getChangedFieldsInput(model, savedElement, patchesTuple);
95
- // // an update without changed user fields
96
- // => don't create mutationEvent
97
- if (updateMutationInput === null) {
98
- return result;
99
- }
100
- }
101
- var element = updateMutationInput || savedElement;
102
- var modelConstructor = Object.getPrototypeOf(savedElement)
103
- .constructor;
104
- _this.pushStream.next({
105
- model: modelConstructor,
106
- opType: opType,
107
- element: element,
108
- mutator: mutator,
109
- condition: (condition &&
110
- ModelPredicateCreator.getPredicates(condition, false)) ||
111
- null,
112
- savedElement: savedElement,
113
- });
114
- });
115
- return [2 /*return*/, result];
116
- }
117
- });
118
- });
119
- };
120
- StorageClass.prototype.delete = function (modelOrModelConstructor, condition, mutator) {
121
- return __awaiter(this, void 0, void 0, function () {
122
- var models, deleted, modelConstructor, namespaceName, modelDefinition, modelIds;
123
- var _a;
124
- var _this = this;
125
- return __generator(this, function (_b) {
126
- switch (_b.label) {
127
- case 0: return [4 /*yield*/, this.init()];
128
- case 1:
129
- _b.sent();
130
- if (!this.adapter) {
131
- throw new Error('Storage adapter is missing');
132
- }
133
- return [4 /*yield*/, this.adapter.delete(modelOrModelConstructor, condition)];
134
- case 2:
135
- _a = __read.apply(void 0, [_b.sent(), 2]), models = _a[0], deleted = _a[1];
136
- modelConstructor = isModelConstructor(modelOrModelConstructor)
137
- ? modelOrModelConstructor
138
- : Object.getPrototypeOf(modelOrModelConstructor || {})
139
- .constructor;
140
- namespaceName = this.namespaceResolver(modelConstructor);
141
- modelDefinition = this.schema.namespaces[namespaceName].models[modelConstructor.name];
142
- modelIds = new Set(models.map(function (model) {
143
- var modelId = getIdentifierValue(modelDefinition, model);
144
- return modelId;
145
- }));
146
- if (!isModelConstructor(modelOrModelConstructor) &&
147
- !Array.isArray(deleted)) {
148
- deleted = [deleted];
149
- }
150
- deleted.forEach(function (model) {
151
- var modelConstructor = Object.getPrototypeOf(model)
152
- .constructor;
153
- var theCondition;
154
- if (!isModelConstructor(modelOrModelConstructor)) {
155
- var modelId = getIdentifierValue(modelDefinition, model);
156
- theCondition = modelIds.has(modelId)
157
- ? ModelPredicateCreator.getPredicates(condition, false)
158
- : undefined;
159
- }
160
- _this.pushStream.next({
161
- model: modelConstructor,
162
- opType: OpType.DELETE,
163
- element: model,
164
- mutator: mutator,
165
- condition: theCondition || null,
166
- });
167
- });
168
- return [2 /*return*/, [models, deleted]];
169
- }
170
- });
32
+ }
33
+ async init() {
34
+ if (this.initialized !== undefined) {
35
+ await this.initialized;
36
+ return;
37
+ }
38
+ logger.debug('Starting Storage');
39
+ let resolve;
40
+ let reject;
41
+ this.initialized = new Promise((res, rej) => {
42
+ resolve = res;
43
+ reject = rej;
171
44
  });
172
- };
173
- StorageClass.prototype.query = function (modelConstructor, predicate, pagination) {
174
- return __awaiter(this, void 0, void 0, function () {
175
- return __generator(this, function (_a) {
176
- switch (_a.label) {
177
- case 0: return [4 /*yield*/, this.init()];
178
- case 1:
179
- _a.sent();
180
- if (!this.adapter) {
181
- throw new Error('Storage adapter is missing');
182
- }
183
- return [4 /*yield*/, this.adapter.query(modelConstructor, predicate, pagination)];
184
- case 2: return [2 /*return*/, _a.sent()];
45
+ this.adapter.setUp(this.schema, this.namespaceResolver, this.modelInstanceCreator, this.getModelConstructorByModelName, this.sessionId).then(resolve, reject);
46
+ await this.initialized;
47
+ }
48
+ async save(model, condition, mutator, patchesTuple) {
49
+ await this.init();
50
+ if (!this.adapter) {
51
+ throw new Error('Storage adapter is missing');
52
+ }
53
+ const result = await this.adapter.save(model, condition);
54
+ result.forEach(r => {
55
+ const [savedElement, opType] = r;
56
+ // truthy when save is called by the Merger
57
+ const syncResponse = !!mutator;
58
+ let updateMutationInput;
59
+ // don't attempt to calc mutation input when storage.save
60
+ // is called by Merger, i.e., when processing an AppSync response
61
+ if ((opType === OpType.UPDATE || opType === OpType.INSERT) &&
62
+ !syncResponse) {
63
+ //
64
+ // TODO: LOOK!!!
65
+ // the `model` used here is in effect regardless of what model
66
+ // comes back from adapter.save().
67
+ // Prior to fix, SQLite adapter had been returning two models
68
+ // of different types, resulting in invalid outbox entries.
69
+ //
70
+ // the bug is essentially fixed in SQLite adapter.
71
+ // leaving as-is, because it's currently unclear whether anything
72
+ // depends on this remaining as-is.
73
+ //
74
+ updateMutationInput = this.getChangedFieldsInput(model, savedElement, patchesTuple);
75
+ // // an update without changed user fields
76
+ // => don't create mutationEvent
77
+ if (updateMutationInput === null) {
78
+ return result;
185
79
  }
80
+ }
81
+ const element = updateMutationInput || savedElement;
82
+ const modelConstructor = Object.getPrototypeOf(savedElement)
83
+ .constructor;
84
+ this.pushStream.next({
85
+ model: modelConstructor,
86
+ opType,
87
+ element,
88
+ mutator,
89
+ condition: (condition &&
90
+ ModelPredicateCreator.getPredicates(condition, false)) ||
91
+ null,
92
+ savedElement,
186
93
  });
187
94
  });
188
- };
189
- StorageClass.prototype.queryOne = function (modelConstructor, firstOrLast) {
190
- if (firstOrLast === void 0) { firstOrLast = QueryOne.FIRST; }
191
- return __awaiter(this, void 0, void 0, function () {
192
- return __generator(this, function (_a) {
193
- switch (_a.label) {
194
- case 0: return [4 /*yield*/, this.init()];
195
- case 1:
196
- _a.sent();
197
- if (!this.adapter) {
198
- throw new Error('Storage adapter is missing');
199
- }
200
- return [4 /*yield*/, this.adapter.queryOne(modelConstructor, firstOrLast)];
201
- case 2: return [2 /*return*/, _a.sent()];
202
- }
95
+ return result;
96
+ }
97
+ async delete(modelOrModelConstructor, condition, mutator) {
98
+ await this.init();
99
+ if (!this.adapter) {
100
+ throw new Error('Storage adapter is missing');
101
+ }
102
+ let models;
103
+ let deleted;
104
+ [models, deleted] = await this.adapter.delete(modelOrModelConstructor, condition);
105
+ const modelConstructor = isModelConstructor(modelOrModelConstructor)
106
+ ? modelOrModelConstructor
107
+ : Object.getPrototypeOf(modelOrModelConstructor || {})
108
+ .constructor;
109
+ const namespaceName = this.namespaceResolver(modelConstructor);
110
+ const modelDefinition = this.schema.namespaces[namespaceName].models[modelConstructor.name];
111
+ const modelIds = new Set(models.map(model => {
112
+ const modelId = getIdentifierValue(modelDefinition, model);
113
+ return modelId;
114
+ }));
115
+ if (!isModelConstructor(modelOrModelConstructor) &&
116
+ !Array.isArray(deleted)) {
117
+ deleted = [deleted];
118
+ }
119
+ deleted.forEach(model => {
120
+ const modelConstructor = Object.getPrototypeOf(model)
121
+ .constructor;
122
+ let theCondition;
123
+ if (!isModelConstructor(modelOrModelConstructor)) {
124
+ const modelId = getIdentifierValue(modelDefinition, model);
125
+ theCondition = modelIds.has(modelId)
126
+ ? ModelPredicateCreator.getPredicates(condition, false)
127
+ : undefined;
128
+ }
129
+ this.pushStream.next({
130
+ model: modelConstructor,
131
+ opType: OpType.DELETE,
132
+ element: model,
133
+ mutator,
134
+ condition: theCondition || null,
203
135
  });
204
136
  });
205
- };
206
- StorageClass.prototype.observe = function (modelConstructor, predicate, skipOwn) {
207
- var listenToAll = !modelConstructor;
208
- var _a = (predicate && ModelPredicateCreator.getPredicates(predicate, false)) ||
209
- {}, predicates = _a.predicates, type = _a.type;
210
- var result = this.pushStream.observable
211
- .filter(function (_a) {
212
- var mutator = _a.mutator;
137
+ return [models, deleted];
138
+ }
139
+ async query(modelConstructor, predicate, pagination) {
140
+ await this.init();
141
+ if (!this.adapter) {
142
+ throw new Error('Storage adapter is missing');
143
+ }
144
+ return await this.adapter.query(modelConstructor, predicate, pagination);
145
+ }
146
+ async queryOne(modelConstructor, firstOrLast = QueryOne.FIRST) {
147
+ await this.init();
148
+ if (!this.adapter) {
149
+ throw new Error('Storage adapter is missing');
150
+ }
151
+ return await this.adapter.queryOne(modelConstructor, firstOrLast);
152
+ }
153
+ observe(modelConstructor, predicate, skipOwn) {
154
+ const listenToAll = !modelConstructor;
155
+ const { predicates, type } = (predicate && ModelPredicateCreator.getPredicates(predicate, false)) ||
156
+ {};
157
+ let result = this.pushStream
158
+ .pipe(filter(({ mutator }) => {
213
159
  return !skipOwn || mutator !== skipOwn;
214
- })
215
- .map(function (_a) {
216
- var _mutator = _a.mutator, message = __rest(_a, ["mutator"]);
217
- return message;
218
- });
160
+ }))
161
+ .pipe(map(({ mutator: _mutator, ...message }) => message));
219
162
  if (!listenToAll) {
220
- result = result.filter(function (_a) {
221
- var model = _a.model, element = _a.element;
163
+ result = result.pipe(filter(({ model, element }) => {
222
164
  if (modelConstructor !== model) {
223
165
  return false;
224
166
  }
@@ -226,174 +168,96 @@ var StorageClass = /** @class */ (function () {
226
168
  return validatePredicate(element, type, predicates);
227
169
  }
228
170
  return true;
229
- });
171
+ }));
230
172
  }
231
173
  return result;
232
- };
233
- StorageClass.prototype.clear = function (completeObservable) {
234
- if (completeObservable === void 0) { completeObservable = true; }
235
- return __awaiter(this, void 0, void 0, function () {
236
- return __generator(this, function (_a) {
237
- switch (_a.label) {
238
- case 0:
239
- this.initialized = undefined;
240
- if (!this.adapter) {
241
- throw new Error('Storage adapter is missing');
242
- }
243
- return [4 /*yield*/, this.adapter.clear()];
244
- case 1:
245
- _a.sent();
246
- if (completeObservable) {
247
- this.pushStream.complete();
248
- }
249
- return [2 /*return*/];
250
- }
251
- });
252
- });
253
- };
254
- StorageClass.prototype.batchSave = function (modelConstructor, items, mutator) {
255
- return __awaiter(this, void 0, void 0, function () {
256
- var result;
257
- var _this = this;
258
- return __generator(this, function (_a) {
259
- switch (_a.label) {
260
- case 0: return [4 /*yield*/, this.init()];
261
- case 1:
262
- _a.sent();
263
- if (!this.adapter) {
264
- throw new Error('Storage adapter is missing');
265
- }
266
- return [4 /*yield*/, this.adapter.batchSave(modelConstructor, items)];
267
- case 2:
268
- result = _a.sent();
269
- result.forEach(function (_a) {
270
- var _b = __read(_a, 2), element = _b[0], opType = _b[1];
271
- _this.pushStream.next({
272
- model: modelConstructor,
273
- opType: opType,
274
- element: element,
275
- mutator: mutator,
276
- condition: null,
277
- });
278
- });
279
- return [2 /*return*/, result];
280
- }
174
+ }
175
+ async clear(completeObservable = true) {
176
+ this.initialized = undefined;
177
+ if (!this.adapter) {
178
+ throw new Error('Storage adapter is missing');
179
+ }
180
+ await this.adapter.clear();
181
+ if (completeObservable) {
182
+ this.pushStream.complete();
183
+ }
184
+ }
185
+ async batchSave(modelConstructor, items, mutator) {
186
+ await this.init();
187
+ if (!this.adapter) {
188
+ throw new Error('Storage adapter is missing');
189
+ }
190
+ const result = await this.adapter.batchSave(modelConstructor, items);
191
+ result.forEach(([element, opType]) => {
192
+ this.pushStream.next({
193
+ model: modelConstructor,
194
+ opType,
195
+ element,
196
+ mutator,
197
+ condition: null,
281
198
  });
282
199
  });
283
- };
200
+ return result;
201
+ }
284
202
  // returns null if no user fields were changed (determined by value comparison)
285
- StorageClass.prototype.getChangedFieldsInput = function (model, originalElement, patchesTuple) {
286
- var e_1, _a;
287
- var _b;
288
- var containsPatches = patchesTuple && patchesTuple.length;
203
+ getChangedFieldsInput(model, originalElement, patchesTuple) {
204
+ const containsPatches = patchesTuple && patchesTuple.length;
289
205
  if (!containsPatches) {
290
206
  return null;
291
207
  }
292
- var _c = __read(patchesTuple, 2), patches = _c[0], source = _c[1];
293
- var updatedElement = {};
208
+ const [patches, source] = patchesTuple;
209
+ const updatedElement = {};
294
210
  // extract array of updated fields from patches
295
- var updatedFields = (patches.map(function (patch) { return patch.path && patch.path[0]; }));
211
+ const updatedFields = (patches.map(patch => patch.path && patch.path[0]));
296
212
  // check model def for association and replace with targetName if exists
297
- var modelConstructor = Object.getPrototypeOf(model)
213
+ const modelConstructor = Object.getPrototypeOf(model)
298
214
  .constructor;
299
- var namespace = this.namespaceResolver(modelConstructor);
300
- var fields = this.schema.namespaces[namespace].models[modelConstructor.name].fields;
301
- var _d = ((_b = this.schema.namespaces[namespace].keys) === null || _b === void 0 ? void 0 : _b[modelConstructor.name]) || {}, primaryKey = _d.primaryKey, _e = _d.compositeKeys, compositeKeys = _e === void 0 ? [] : _e;
215
+ const namespace = this.namespaceResolver(modelConstructor);
216
+ const { fields } = this.schema.namespaces[namespace].models[modelConstructor.name];
217
+ const { primaryKey, compositeKeys = [] } = this.schema.namespaces[namespace].keys?.[modelConstructor.name] || {};
302
218
  // set original values for these fields
303
- updatedFields.forEach(function (field) {
304
- var e_2, _a, e_3, _b, e_4, _c, e_5, _d, e_6, _e;
305
- var _f;
306
- var targetNames = isTargetNameAssociation((_f = fields[field]) === null || _f === void 0 ? void 0 : _f.association);
219
+ updatedFields.forEach((field) => {
220
+ const targetNames = isTargetNameAssociation(fields[field]?.association);
307
221
  if (Array.isArray(targetNames)) {
308
- try {
309
- // if field refers to a belongsTo relation, use the target field instead
310
- for (var targetNames_1 = __values(targetNames), targetNames_1_1 = targetNames_1.next(); !targetNames_1_1.done; targetNames_1_1 = targetNames_1.next()) {
311
- var targetName = targetNames_1_1.value;
312
- // check field values by value. Ignore unchanged fields
313
- if (!valuesEqual(source[targetName], originalElement[targetName])) {
314
- // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
315
- updatedElement[targetName] =
316
- originalElement[targetName] === undefined
317
- ? null
318
- : originalElement[targetName];
319
- try {
320
- for (var compositeKeys_1 = (e_3 = void 0, __values(compositeKeys)), compositeKeys_1_1 = compositeKeys_1.next(); !compositeKeys_1_1.done; compositeKeys_1_1 = compositeKeys_1.next()) {
321
- var fieldSet = compositeKeys_1_1.value;
322
- // include all of the fields that comprise the composite key
323
- if (fieldSet.has(targetName)) {
324
- try {
325
- for (var fieldSet_1 = (e_4 = void 0, __values(fieldSet)), fieldSet_1_1 = fieldSet_1.next(); !fieldSet_1_1.done; fieldSet_1_1 = fieldSet_1.next()) {
326
- var compositeField = fieldSet_1_1.value;
327
- updatedElement[compositeField] =
328
- originalElement[compositeField];
329
- }
330
- }
331
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
332
- finally {
333
- try {
334
- if (fieldSet_1_1 && !fieldSet_1_1.done && (_c = fieldSet_1.return)) _c.call(fieldSet_1);
335
- }
336
- finally { if (e_4) throw e_4.error; }
337
- }
338
- }
339
- }
340
- }
341
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
342
- finally {
343
- try {
344
- if (compositeKeys_1_1 && !compositeKeys_1_1.done && (_b = compositeKeys_1.return)) _b.call(compositeKeys_1);
222
+ // if field refers to a belongsTo relation, use the target field instead
223
+ for (const targetName of targetNames) {
224
+ // check field values by value. Ignore unchanged fields
225
+ if (!valuesEqual(source[targetName], originalElement[targetName])) {
226
+ // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
227
+ updatedElement[targetName] =
228
+ originalElement[targetName] === undefined
229
+ ? null
230
+ : originalElement[targetName];
231
+ for (const fieldSet of compositeKeys) {
232
+ // include all of the fields that comprise the composite key
233
+ if (fieldSet.has(targetName)) {
234
+ for (const compositeField of fieldSet) {
235
+ updatedElement[compositeField] =
236
+ originalElement[compositeField];
345
237
  }
346
- finally { if (e_3) throw e_3.error; }
347
238
  }
348
239
  }
349
240
  }
350
241
  }
351
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
352
- finally {
353
- try {
354
- if (targetNames_1_1 && !targetNames_1_1.done && (_a = targetNames_1.return)) _a.call(targetNames_1);
355
- }
356
- finally { if (e_2) throw e_2.error; }
357
- }
358
242
  }
359
243
  else {
360
244
  // Backwards compatibility pre-CPK
361
245
  // if field refers to a belongsTo relation, use the target field instead
362
- var key = targetNames || field;
246
+ const key = targetNames || field;
363
247
  // check field values by value. Ignore unchanged fields
364
248
  if (!valuesEqual(source[key], originalElement[key])) {
365
249
  // if the field was updated to 'undefined', replace with 'null' for compatibility with JSON and GraphQL
366
250
  updatedElement[key] =
367
251
  originalElement[key] === undefined ? null : originalElement[key];
368
- try {
369
- for (var compositeKeys_2 = __values(compositeKeys), compositeKeys_2_1 = compositeKeys_2.next(); !compositeKeys_2_1.done; compositeKeys_2_1 = compositeKeys_2.next()) {
370
- var fieldSet = compositeKeys_2_1.value;
371
- // include all of the fields that comprise the composite key
372
- if (fieldSet.has(key)) {
373
- try {
374
- for (var fieldSet_2 = (e_6 = void 0, __values(fieldSet)), fieldSet_2_1 = fieldSet_2.next(); !fieldSet_2_1.done; fieldSet_2_1 = fieldSet_2.next()) {
375
- var compositeField = fieldSet_2_1.value;
376
- updatedElement[compositeField] =
377
- originalElement[compositeField];
378
- }
379
- }
380
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
381
- finally {
382
- try {
383
- if (fieldSet_2_1 && !fieldSet_2_1.done && (_e = fieldSet_2.return)) _e.call(fieldSet_2);
384
- }
385
- finally { if (e_6) throw e_6.error; }
386
- }
252
+ for (const fieldSet of compositeKeys) {
253
+ // include all of the fields that comprise the composite key
254
+ if (fieldSet.has(key)) {
255
+ for (const compositeField of fieldSet) {
256
+ updatedElement[compositeField] =
257
+ originalElement[compositeField];
387
258
  }
388
259
  }
389
260
  }
390
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
391
- finally {
392
- try {
393
- if (compositeKeys_2_1 && !compositeKeys_2_1.done && (_d = compositeKeys_2.return)) _d.call(compositeKeys_2);
394
- }
395
- finally { if (e_5) throw e_5.error; }
396
- }
397
261
  }
398
262
  }
399
263
  });
@@ -403,111 +267,65 @@ var StorageClass = /** @class */ (function () {
403
267
  }
404
268
  // include field(s) from custom PK if one is specified for the model
405
269
  if (primaryKey && primaryKey.length) {
406
- try {
407
- for (var primaryKey_1 = __values(primaryKey), primaryKey_1_1 = primaryKey_1.next(); !primaryKey_1_1.done; primaryKey_1_1 = primaryKey_1.next()) {
408
- var pkField = primaryKey_1_1.value;
409
- updatedElement[pkField] = originalElement[pkField];
410
- }
411
- }
412
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
413
- finally {
414
- try {
415
- if (primaryKey_1_1 && !primaryKey_1_1.done && (_a = primaryKey_1.return)) _a.call(primaryKey_1);
416
- }
417
- finally { if (e_1) throw e_1.error; }
270
+ for (const pkField of primaryKey) {
271
+ updatedElement[pkField] = originalElement[pkField];
418
272
  }
419
273
  }
420
- var id = originalElement.id, _version = originalElement._version, _lastChangedAt = originalElement._lastChangedAt, _deleted = originalElement._deleted;
274
+ const { id, _version, _lastChangedAt, _deleted } = originalElement;
421
275
  // For update mutations we only want to send fields with changes
422
276
  // and the required internal fields
423
- return __assign(__assign({}, updatedElement), { id: id,
424
- _version: _version,
425
- _lastChangedAt: _lastChangedAt,
426
- _deleted: _deleted });
427
- };
428
- return StorageClass;
429
- }());
430
- var ExclusiveStorage = /** @class */ (function () {
431
- function ExclusiveStorage(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
277
+ return {
278
+ ...updatedElement,
279
+ id,
280
+ _version,
281
+ _lastChangedAt,
282
+ _deleted,
283
+ };
284
+ }
285
+ }
286
+ class ExclusiveStorage {
287
+ constructor(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId) {
432
288
  this.mutex = new Mutex();
433
289
  this.storage = new StorageClass(schema, namespaceResolver, getModelConstructorByModelName, modelInstanceCreator, adapter, sessionId);
434
290
  }
435
- ExclusiveStorage.prototype.runExclusive = function (fn) {
291
+ runExclusive(fn) {
436
292
  return this.mutex.runExclusive(fn.bind(this, this.storage));
437
- };
438
- ExclusiveStorage.prototype.save = function (model, condition, mutator, patchesTuple) {
439
- return __awaiter(this, void 0, void 0, function () {
440
- return __generator(this, function (_a) {
441
- return [2 /*return*/, this.runExclusive(function (storage) {
442
- return storage.save(model, condition, mutator, patchesTuple);
443
- })];
444
- });
445
- });
446
- };
447
- ExclusiveStorage.prototype.delete = function (modelOrModelConstructor, condition, mutator) {
448
- return __awaiter(this, void 0, void 0, function () {
449
- return __generator(this, function (_a) {
450
- return [2 /*return*/, this.runExclusive(function (storage) {
451
- if (isModelConstructor(modelOrModelConstructor)) {
452
- var modelConstructor = modelOrModelConstructor;
453
- return storage.delete(modelConstructor, condition, mutator);
454
- }
455
- else {
456
- var model = modelOrModelConstructor;
457
- return storage.delete(model, condition, mutator);
458
- }
459
- })];
460
- });
461
- });
462
- };
463
- ExclusiveStorage.prototype.query = function (modelConstructor, predicate, pagination) {
464
- return __awaiter(this, void 0, void 0, function () {
465
- return __generator(this, function (_a) {
466
- return [2 /*return*/, this.runExclusive(function (storage) {
467
- return storage.query(modelConstructor, predicate, pagination);
468
- })];
469
- });
470
- });
471
- };
472
- ExclusiveStorage.prototype.queryOne = function (modelConstructor, firstOrLast) {
473
- if (firstOrLast === void 0) { firstOrLast = QueryOne.FIRST; }
474
- return __awaiter(this, void 0, void 0, function () {
475
- return __generator(this, function (_a) {
476
- return [2 /*return*/, this.runExclusive(function (storage) {
477
- return storage.queryOne(modelConstructor, firstOrLast);
478
- })];
479
- });
293
+ }
294
+ async save(model, condition, mutator, patchesTuple) {
295
+ return this.runExclusive(storage => storage.save(model, condition, mutator, patchesTuple));
296
+ }
297
+ async delete(modelOrModelConstructor, condition, mutator) {
298
+ return this.runExclusive(storage => {
299
+ if (isModelConstructor(modelOrModelConstructor)) {
300
+ const modelConstructor = modelOrModelConstructor;
301
+ return storage.delete(modelConstructor, condition, mutator);
302
+ }
303
+ else {
304
+ const model = modelOrModelConstructor;
305
+ return storage.delete(model, condition, mutator);
306
+ }
480
307
  });
481
- };
482
- ExclusiveStorage.getNamespace = function () {
308
+ }
309
+ async query(modelConstructor, predicate, pagination) {
310
+ return this.runExclusive(storage => storage.query(modelConstructor, predicate, pagination));
311
+ }
312
+ async queryOne(modelConstructor, firstOrLast = QueryOne.FIRST) {
313
+ return this.runExclusive(storage => storage.queryOne(modelConstructor, firstOrLast));
314
+ }
315
+ static getNamespace() {
483
316
  return StorageClass.getNamespace();
484
- };
485
- ExclusiveStorage.prototype.observe = function (modelConstructor, predicate, skipOwn) {
317
+ }
318
+ observe(modelConstructor, predicate, skipOwn) {
486
319
  return this.storage.observe(modelConstructor, predicate, skipOwn);
487
- };
488
- ExclusiveStorage.prototype.clear = function () {
489
- return __awaiter(this, void 0, void 0, function () {
490
- return __generator(this, function (_a) {
491
- switch (_a.label) {
492
- case 0: return [4 /*yield*/, this.runExclusive(function (storage) { return storage.clear(); })];
493
- case 1:
494
- _a.sent();
495
- return [2 /*return*/];
496
- }
497
- });
498
- });
499
- };
500
- ExclusiveStorage.prototype.batchSave = function (modelConstructor, items) {
320
+ }
321
+ async clear() {
322
+ await this.runExclusive(storage => storage.clear());
323
+ }
324
+ batchSave(modelConstructor, items) {
501
325
  return this.storage.batchSave(modelConstructor, items);
502
- };
503
- ExclusiveStorage.prototype.init = function () {
504
- return __awaiter(this, void 0, void 0, function () {
505
- return __generator(this, function (_a) {
506
- return [2 /*return*/, this.storage.init()];
507
- });
508
- });
509
- };
510
- return ExclusiveStorage;
511
- }());
326
+ }
327
+ async init() {
328
+ return this.storage.init();
329
+ }
330
+ }
512
331
  export { ExclusiveStorage };
513
- //# sourceMappingURL=storage.js.map