@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,20 +1,18 @@
1
- var ModelSortPredicateCreator = /** @class */ (function () {
2
- function ModelSortPredicateCreator() {
3
- }
4
- ModelSortPredicateCreator.createPredicateBuilder = function (modelDefinition) {
5
- var modelName = modelDefinition.name;
6
- var fieldNames = new Set(Object.keys(modelDefinition.fields));
7
- var handler;
8
- var predicate = new Proxy({}, (handler = {
9
- get: function (_target, propertyKey, receiver) {
10
- var field = propertyKey;
1
+ class ModelSortPredicateCreator {
2
+ static createPredicateBuilder(modelDefinition) {
3
+ const { name: modelName } = modelDefinition;
4
+ const fieldNames = new Set(Object.keys(modelDefinition.fields));
5
+ let handler;
6
+ const predicate = new Proxy({}, (handler = {
7
+ get(_target, propertyKey, receiver) {
8
+ const field = propertyKey;
11
9
  if (!fieldNames.has(field)) {
12
- throw new Error("Invalid field for model. field: " + field + ", model: " + modelName);
10
+ throw new Error(`Invalid field for model. field: ${String(field)}, model: ${modelName}`);
13
11
  }
14
- var result = function (sortDirection) {
15
- var _a;
16
- (_a = ModelSortPredicateCreator.sortPredicateGroupsMap
17
- .get(receiver)) === null || _a === void 0 ? void 0 : _a.push({ field: field, sortDirection: sortDirection });
12
+ const result = (sortDirection) => {
13
+ ModelSortPredicateCreator.sortPredicateGroupsMap
14
+ .get(receiver)
15
+ ?.push({ field, sortDirection });
18
16
  return receiver;
19
17
  };
20
18
  return result;
@@ -22,33 +20,30 @@ var ModelSortPredicateCreator = /** @class */ (function () {
22
20
  }));
23
21
  ModelSortPredicateCreator.sortPredicateGroupsMap.set(predicate, []);
24
22
  return predicate;
25
- };
26
- ModelSortPredicateCreator.isValidPredicate = function (predicate) {
23
+ }
24
+ static isValidPredicate(predicate) {
27
25
  return ModelSortPredicateCreator.sortPredicateGroupsMap.has(predicate);
28
- };
29
- ModelSortPredicateCreator.getPredicates = function (predicate, throwOnInvalid) {
30
- if (throwOnInvalid === void 0) { throwOnInvalid = true; }
26
+ }
27
+ static getPredicates(predicate, throwOnInvalid = true) {
31
28
  if (throwOnInvalid &&
32
29
  !ModelSortPredicateCreator.isValidPredicate(predicate)) {
33
30
  throw new Error('The predicate is not valid');
34
31
  }
35
- var predicateGroup = ModelSortPredicateCreator.sortPredicateGroupsMap.get(predicate);
32
+ const predicateGroup = ModelSortPredicateCreator.sortPredicateGroupsMap.get(predicate);
36
33
  if (predicateGroup) {
37
34
  return predicateGroup;
38
35
  }
39
36
  else {
40
37
  throw new Error('Predicate group not found');
41
38
  }
42
- };
39
+ }
43
40
  // transforms cb-style predicate into Proxy
44
- ModelSortPredicateCreator.createFromExisting = function (modelDefinition, existing) {
41
+ static createFromExisting(modelDefinition, existing) {
45
42
  if (!existing || !modelDefinition) {
46
43
  return undefined;
47
44
  }
48
45
  return existing(ModelSortPredicateCreator.createPredicateBuilder(modelDefinition));
49
- };
50
- ModelSortPredicateCreator.sortPredicateGroupsMap = new WeakMap();
51
- return ModelSortPredicateCreator;
52
- }());
46
+ }
47
+ }
48
+ ModelSortPredicateCreator.sortPredicateGroupsMap = new WeakMap();
53
49
  export { ModelSortPredicateCreator };
54
- //# sourceMappingURL=sort.js.map
@@ -1,7 +1,7 @@
1
1
  // Helper for converting JSON back into DataStore models (while respecting IDs)
2
2
  export function deserializeModel(Model, init) {
3
3
  if (Array.isArray(init)) {
4
- return init.map(function (init) { return deserializeModel(Model, init); });
4
+ return init.map(init => deserializeModel(Model, init));
5
5
  }
6
6
  // `fromJSON` is intentionally hidden from types as a "private" method (though it exists on the instance)
7
7
  // @ts-ignore Property 'fromJSON' does not exist on type 'PersistentModelConstructor<T>'.ts(2339)
@@ -11,4 +11,3 @@ export function deserializeModel(Model, init) {
11
11
  export function serializeModel(model) {
12
12
  return JSON.parse(JSON.stringify(model));
13
13
  }
14
- //# sourceMappingURL=index.js.map
@@ -1,26 +1,13 @@
1
- import { __asyncValues, __awaiter, __extends, __generator, __values } from "tslib";
2
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
2
  // SPDX-License-Identifier: Apache-2.0
4
3
  import AsyncStorageDatabase from './AsyncStorageDatabase';
5
4
  import { OpType, QueryOne, } from '../../types';
6
5
  import { DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR, traverseModel, validatePredicate, inMemoryPagination, keysEqual, getStorename, getIndexKeys, } from '../../util';
7
6
  import { StorageAdapterBase } from './StorageAdapterBase';
8
- var AsyncStorageAdapter = /** @class */ (function (_super) {
9
- __extends(AsyncStorageAdapter, _super);
10
- function AsyncStorageAdapter() {
11
- return _super !== null && _super.apply(this, arguments) || this;
12
- }
7
+ export class AsyncStorageAdapter extends StorageAdapterBase {
13
8
  // no-ops for this adapter
14
- AsyncStorageAdapter.prototype.preSetUpChecks = function () {
15
- return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
16
- return [2 /*return*/];
17
- }); });
18
- };
19
- AsyncStorageAdapter.prototype.preOpCheck = function () {
20
- return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) {
21
- return [2 /*return*/];
22
- }); });
23
- };
9
+ async preSetUpChecks() { }
10
+ async preOpCheck() { }
24
11
  /**
25
12
  * Open AsyncStorage database
26
13
  * Create new DB if one doesn't exist
@@ -29,325 +16,114 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
29
16
  *
30
17
  * @returns AsyncStorageDatabase instance
31
18
  */
32
- AsyncStorageAdapter.prototype.initDb = function () {
33
- return __awaiter(this, void 0, void 0, function () {
34
- var db;
35
- return __generator(this, function (_a) {
36
- switch (_a.label) {
37
- case 0:
38
- db = new AsyncStorageDatabase();
39
- return [4 /*yield*/, db.init()];
40
- case 1:
41
- _a.sent();
42
- return [2 /*return*/, db];
43
- }
44
- });
45
- });
46
- };
47
- AsyncStorageAdapter.prototype.clear = function () {
48
- return __awaiter(this, void 0, void 0, function () {
49
- return __generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0: return [4 /*yield*/, this.db.clear()];
52
- case 1:
53
- _a.sent();
54
- this.db = undefined;
55
- this.initPromise = undefined;
56
- return [2 /*return*/];
57
- }
58
- });
59
- });
60
- };
61
- AsyncStorageAdapter.prototype.batchSave = function (modelConstructor, items) {
62
- return __awaiter(this, void 0, void 0, function () {
63
- var modelName, namespaceName, storeName, keys, batch, _loop_1, this_1, items_1, items_1_1, item;
64
- var e_1, _a;
65
- var _this = this;
66
- return __generator(this, function (_b) {
67
- switch (_b.label) {
68
- case 0:
69
- if (items.length === 0) {
70
- return [2 /*return*/, []];
71
- }
72
- modelName = modelConstructor.name;
73
- namespaceName = this.namespaceResolver(modelConstructor);
74
- storeName = getStorename(namespaceName, modelName);
75
- keys = getIndexKeys(this.schema.namespaces[namespaceName], modelName);
76
- batch = [];
77
- _loop_1 = function (item) {
78
- var model = this_1.modelInstanceCreator(modelConstructor, item);
79
- var connectedModels = traverseModel(modelName, model, this_1.schema.namespaces[namespaceName], this_1.modelInstanceCreator, this_1.getModelConstructorByModelName);
80
- var keyValuesPath = this_1.getIndexKeyValuesPath(model);
81
- var instance = connectedModels.find(function (_a) {
82
- var instance = _a.instance;
83
- var instanceKeyValuesPath = _this.getIndexKeyValuesPath(instance);
84
- return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
85
- }).instance;
86
- batch.push(instance);
87
- };
88
- this_1 = this;
89
- try {
90
- for (items_1 = __values(items), items_1_1 = items_1.next(); !items_1_1.done; items_1_1 = items_1.next()) {
91
- item = items_1_1.value;
92
- _loop_1(item);
93
- }
94
- }
95
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
96
- finally {
97
- try {
98
- if (items_1_1 && !items_1_1.done && (_a = items_1.return)) _a.call(items_1);
99
- }
100
- finally { if (e_1) throw e_1.error; }
101
- }
102
- return [4 /*yield*/, this.db.batchSave(storeName, batch, keys)];
103
- case 1: return [2 /*return*/, _b.sent()];
104
- }
105
- });
106
- });
107
- };
108
- AsyncStorageAdapter.prototype._get = function (storeName, keyArr) {
109
- return __awaiter(this, void 0, void 0, function () {
110
- var itemKeyValuesPath;
111
- return __generator(this, function (_a) {
112
- switch (_a.label) {
113
- case 0:
114
- itemKeyValuesPath = keyArr.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
115
- return [4 /*yield*/, this.db.get(itemKeyValuesPath, storeName)];
116
- case 1: return [2 /*return*/, _a.sent()];
117
- }
118
- });
119
- });
120
- };
121
- AsyncStorageAdapter.prototype.save = function (model, condition) {
122
- var e_2, _a;
123
- return __awaiter(this, void 0, void 0, function () {
124
- var _b, storeName, connectionStoreNames, modelKeyValues, fromDB, result, connectionStoreNames_1, connectionStoreNames_1_1, resItem, storeName_1, item, instance, keys, itemKeyValues, fromDB_1, opType, e_2_1;
125
- return __generator(this, function (_c) {
126
- switch (_c.label) {
127
- case 0:
128
- _b = this.saveMetadata(model), storeName = _b.storeName, connectionStoreNames = _b.connectionStoreNames, modelKeyValues = _b.modelKeyValues;
129
- return [4 /*yield*/, this._get(storeName, modelKeyValues)];
130
- case 1:
131
- fromDB = _c.sent();
132
- this.validateSaveCondition(condition, fromDB);
133
- result = [];
134
- _c.label = 2;
135
- case 2:
136
- _c.trys.push([2, 9, 10, 15]);
137
- connectionStoreNames_1 = __asyncValues(connectionStoreNames);
138
- _c.label = 3;
139
- case 3: return [4 /*yield*/, connectionStoreNames_1.next()];
140
- case 4:
141
- if (!(connectionStoreNames_1_1 = _c.sent(), !connectionStoreNames_1_1.done)) return [3 /*break*/, 8];
142
- resItem = connectionStoreNames_1_1.value;
143
- storeName_1 = resItem.storeName, item = resItem.item, instance = resItem.instance, keys = resItem.keys;
144
- itemKeyValues = keys.map(function (key) { return item[key]; });
145
- return [4 /*yield*/, this._get(storeName_1, itemKeyValues)];
146
- case 5:
147
- fromDB_1 = _c.sent();
148
- opType = fromDB_1 ? OpType.UPDATE : OpType.INSERT;
149
- if (!(keysEqual(itemKeyValues, modelKeyValues) ||
150
- opType === OpType.INSERT)) return [3 /*break*/, 7];
151
- return [4 /*yield*/, this.db.save(item, storeName_1, keys, itemKeyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR))];
152
- case 6:
153
- _c.sent();
154
- result.push([instance, opType]);
155
- _c.label = 7;
156
- case 7: return [3 /*break*/, 3];
157
- case 8: return [3 /*break*/, 15];
158
- case 9:
159
- e_2_1 = _c.sent();
160
- e_2 = { error: e_2_1 };
161
- return [3 /*break*/, 15];
162
- case 10:
163
- _c.trys.push([10, , 13, 14]);
164
- if (!(connectionStoreNames_1_1 && !connectionStoreNames_1_1.done && (_a = connectionStoreNames_1.return))) return [3 /*break*/, 12];
165
- return [4 /*yield*/, _a.call(connectionStoreNames_1)];
166
- case 11:
167
- _c.sent();
168
- _c.label = 12;
169
- case 12: return [3 /*break*/, 14];
170
- case 13:
171
- if (e_2) throw e_2.error;
172
- return [7 /*endfinally*/];
173
- case 14: return [7 /*endfinally*/];
174
- case 15: return [2 /*return*/, result];
175
- }
176
- });
177
- });
178
- };
179
- AsyncStorageAdapter.prototype.query = function (modelConstructor, predicate, pagination) {
180
- return __awaiter(this, void 0, void 0, function () {
181
- var _a, storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, records;
182
- var _this = this;
183
- return __generator(this, function (_b) {
184
- switch (_b.label) {
185
- case 0:
186
- _a = this.queryMetadata(modelConstructor, predicate, pagination), storeName = _a.storeName, namespaceName = _a.namespaceName, queryByKey = _a.queryByKey, predicates = _a.predicates, hasSort = _a.hasSort, hasPagination = _a.hasPagination;
187
- return [4 /*yield*/, (function () { return __awaiter(_this, void 0, void 0, function () {
188
- var keyValues, record, filtered, all;
189
- return __generator(this, function (_a) {
190
- switch (_a.label) {
191
- case 0:
192
- if (!queryByKey) return [3 /*break*/, 2];
193
- keyValues = queryByKey.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
194
- return [4 /*yield*/, this.getByKey(storeName, keyValues)];
195
- case 1:
196
- record = _a.sent();
197
- return [2 /*return*/, record ? [record] : []];
198
- case 2:
199
- if (!predicates) return [3 /*break*/, 4];
200
- return [4 /*yield*/, this.filterOnPredicate(storeName, predicates)];
201
- case 3:
202
- filtered = _a.sent();
203
- return [2 /*return*/, this.inMemoryPagination(filtered, pagination)];
204
- case 4:
205
- if (!(hasSort || hasPagination)) return [3 /*break*/, 6];
206
- return [4 /*yield*/, this.getAll(storeName)];
207
- case 5:
208
- all = _a.sent();
209
- return [2 /*return*/, this.inMemoryPagination(all, pagination)];
210
- case 6: return [2 /*return*/, this.getAll(storeName)];
211
- }
212
- });
213
- }); })()];
214
- case 1:
215
- records = (_b.sent());
216
- return [4 /*yield*/, this.load(namespaceName, modelConstructor.name, records)];
217
- case 2: return [2 /*return*/, _b.sent()];
218
- }
219
- });
220
- });
221
- };
222
- AsyncStorageAdapter.prototype.getByKey = function (storeName, keyValuePath) {
223
- return __awaiter(this, void 0, void 0, function () {
224
- return __generator(this, function (_a) {
225
- switch (_a.label) {
226
- case 0: return [4 /*yield*/, this.db.get(keyValuePath, storeName)];
227
- case 1: return [2 /*return*/, _a.sent()];
228
- }
229
- });
230
- });
231
- };
232
- AsyncStorageAdapter.prototype.getAll = function (storeName) {
233
- return __awaiter(this, void 0, void 0, function () {
234
- return __generator(this, function (_a) {
235
- switch (_a.label) {
236
- case 0: return [4 /*yield*/, this.db.getAll(storeName)];
237
- case 1: return [2 /*return*/, _a.sent()];
238
- }
239
- });
240
- });
241
- };
242
- AsyncStorageAdapter.prototype.filterOnPredicate = function (storeName, predicates) {
243
- return __awaiter(this, void 0, void 0, function () {
244
- var predicateObjs, type, all, filtered;
245
- return __generator(this, function (_a) {
246
- switch (_a.label) {
247
- case 0:
248
- predicateObjs = predicates.predicates, type = predicates.type;
249
- return [4 /*yield*/, this.getAll(storeName)];
250
- case 1:
251
- all = _a.sent();
252
- filtered = predicateObjs
253
- ? all.filter(function (m) { return validatePredicate(m, type, predicateObjs); })
254
- : all;
255
- return [2 /*return*/, filtered];
256
- }
19
+ async initDb() {
20
+ const db = new AsyncStorageDatabase();
21
+ await db.init();
22
+ return db;
23
+ }
24
+ async clear() {
25
+ await this.db.clear();
26
+ this.db = undefined;
27
+ this.initPromise = undefined;
28
+ }
29
+ async batchSave(modelConstructor, items) {
30
+ if (items.length === 0) {
31
+ return [];
32
+ }
33
+ const modelName = modelConstructor.name;
34
+ const namespaceName = this.namespaceResolver(modelConstructor);
35
+ const storeName = getStorename(namespaceName, modelName);
36
+ const keys = getIndexKeys(this.schema.namespaces[namespaceName], modelName);
37
+ const batch = [];
38
+ for (const item of items) {
39
+ const model = this.modelInstanceCreator(modelConstructor, item);
40
+ const connectedModels = traverseModel(modelName, model, this.schema.namespaces[namespaceName], this.modelInstanceCreator, this.getModelConstructorByModelName);
41
+ const keyValuesPath = this.getIndexKeyValuesPath(model);
42
+ const { instance } = connectedModels.find(({ instance }) => {
43
+ const instanceKeyValuesPath = this.getIndexKeyValuesPath(instance);
44
+ return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
257
45
  });
258
- });
259
- };
260
- AsyncStorageAdapter.prototype.inMemoryPagination = function (records, pagination) {
46
+ batch.push(instance);
47
+ }
48
+ return await this.db.batchSave(storeName, batch, keys);
49
+ }
50
+ async _get(storeName, keyArr) {
51
+ const itemKeyValuesPath = keyArr.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
52
+ return await this.db.get(itemKeyValuesPath, storeName);
53
+ }
54
+ async save(model, condition) {
55
+ const { storeName, connectionStoreNames, modelKeyValues } = this.saveMetadata(model);
56
+ const fromDB = await this._get(storeName, modelKeyValues);
57
+ this.validateSaveCondition(condition, fromDB);
58
+ const result = [];
59
+ for await (const resItem of connectionStoreNames) {
60
+ const { storeName, item, instance, keys } = resItem;
61
+ const itemKeyValues = keys.map(key => item[key]);
62
+ const fromDB = await this._get(storeName, itemKeyValues);
63
+ const opType = fromDB ? OpType.UPDATE : OpType.INSERT;
64
+ if (keysEqual(itemKeyValues, modelKeyValues) ||
65
+ opType === OpType.INSERT) {
66
+ await this.db.save(item, storeName, keys, itemKeyValues.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR));
67
+ result.push([instance, opType]);
68
+ }
69
+ }
70
+ return result;
71
+ }
72
+ async query(modelConstructor, predicate, pagination) {
73
+ const { storeName, namespaceName, queryByKey, predicates, hasSort, hasPagination, } = this.queryMetadata(modelConstructor, predicate, pagination);
74
+ const records = (await (async () => {
75
+ if (queryByKey) {
76
+ const keyValues = queryByKey.join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
77
+ const record = await this.getByKey(storeName, keyValues);
78
+ return record ? [record] : [];
79
+ }
80
+ if (predicates) {
81
+ const filtered = await this.filterOnPredicate(storeName, predicates);
82
+ return this.inMemoryPagination(filtered, pagination);
83
+ }
84
+ if (hasSort || hasPagination) {
85
+ const all = await this.getAll(storeName);
86
+ return this.inMemoryPagination(all, pagination);
87
+ }
88
+ return this.getAll(storeName);
89
+ })());
90
+ return await this.load(namespaceName, modelConstructor.name, records);
91
+ }
92
+ async getByKey(storeName, keyValuePath) {
93
+ return await this.db.get(keyValuePath, storeName);
94
+ }
95
+ async getAll(storeName) {
96
+ return await this.db.getAll(storeName);
97
+ }
98
+ async filterOnPredicate(storeName, predicates) {
99
+ const { predicates: predicateObjs, type } = predicates;
100
+ const all = await this.getAll(storeName);
101
+ const filtered = predicateObjs
102
+ ? all.filter(m => validatePredicate(m, type, predicateObjs))
103
+ : all;
104
+ return filtered;
105
+ }
106
+ inMemoryPagination(records, pagination) {
261
107
  return inMemoryPagination(records, pagination);
262
- };
263
- AsyncStorageAdapter.prototype.queryOne = function (modelConstructor, firstOrLast) {
264
- if (firstOrLast === void 0) { firstOrLast = QueryOne.FIRST; }
265
- return __awaiter(this, void 0, void 0, function () {
266
- var storeName, result;
267
- return __generator(this, function (_a) {
268
- switch (_a.label) {
269
- case 0:
270
- storeName = this.getStorenameForModel(modelConstructor);
271
- return [4 /*yield*/, this.db.getOne(firstOrLast, storeName)];
272
- case 1:
273
- result = _a.sent();
274
- return [2 /*return*/, result && this.modelInstanceCreator(modelConstructor, result)];
275
- }
276
- });
277
- });
278
- };
279
- AsyncStorageAdapter.prototype.deleteItem = function (deleteQueue) {
280
- var e_3, _a, e_4, _b;
281
- return __awaiter(this, void 0, void 0, function () {
282
- var _c, _d, deleteItem, storeName, items, items_2, items_2_1, item, keyValuesPath, e_4_1, e_3_1;
283
- return __generator(this, function (_e) {
284
- switch (_e.label) {
285
- case 0:
286
- _e.trys.push([0, 17, 18, 23]);
287
- _c = __asyncValues(deleteQueue);
288
- _e.label = 1;
289
- case 1: return [4 /*yield*/, _c.next()];
290
- case 2:
291
- if (!(_d = _e.sent(), !_d.done)) return [3 /*break*/, 16];
292
- deleteItem = _d.value;
293
- storeName = deleteItem.storeName, items = deleteItem.items;
294
- _e.label = 3;
295
- case 3:
296
- _e.trys.push([3, 9, 10, 15]);
297
- items_2 = __asyncValues(items);
298
- _e.label = 4;
299
- case 4: return [4 /*yield*/, items_2.next()];
300
- case 5:
301
- if (!(items_2_1 = _e.sent(), !items_2_1.done)) return [3 /*break*/, 8];
302
- item = items_2_1.value;
303
- if (!item) return [3 /*break*/, 7];
304
- if (!(typeof item === 'object')) return [3 /*break*/, 7];
305
- keyValuesPath = this.getIndexKeyValuesPath(item);
306
- return [4 /*yield*/, this.db.delete(keyValuesPath, storeName)];
307
- case 6:
308
- _e.sent();
309
- _e.label = 7;
310
- case 7: return [3 /*break*/, 4];
311
- case 8: return [3 /*break*/, 15];
312
- case 9:
313
- e_4_1 = _e.sent();
314
- e_4 = { error: e_4_1 };
315
- return [3 /*break*/, 15];
316
- case 10:
317
- _e.trys.push([10, , 13, 14]);
318
- if (!(items_2_1 && !items_2_1.done && (_b = items_2.return))) return [3 /*break*/, 12];
319
- return [4 /*yield*/, _b.call(items_2)];
320
- case 11:
321
- _e.sent();
322
- _e.label = 12;
323
- case 12: return [3 /*break*/, 14];
324
- case 13:
325
- if (e_4) throw e_4.error;
326
- return [7 /*endfinally*/];
327
- case 14: return [7 /*endfinally*/];
328
- case 15: return [3 /*break*/, 1];
329
- case 16: return [3 /*break*/, 23];
330
- case 17:
331
- e_3_1 = _e.sent();
332
- e_3 = { error: e_3_1 };
333
- return [3 /*break*/, 23];
334
- case 18:
335
- _e.trys.push([18, , 21, 22]);
336
- if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 20];
337
- return [4 /*yield*/, _a.call(_c)];
338
- case 19:
339
- _e.sent();
340
- _e.label = 20;
341
- case 20: return [3 /*break*/, 22];
342
- case 21:
343
- if (e_3) throw e_3.error;
344
- return [7 /*endfinally*/];
345
- case 22: return [7 /*endfinally*/];
346
- case 23: return [2 /*return*/];
108
+ }
109
+ async queryOne(modelConstructor, firstOrLast = QueryOne.FIRST) {
110
+ const storeName = this.getStorenameForModel(modelConstructor);
111
+ const result = await this.db.getOne(firstOrLast, storeName);
112
+ return result && this.modelInstanceCreator(modelConstructor, result);
113
+ }
114
+ async deleteItem(deleteQueue) {
115
+ for await (const deleteItem of deleteQueue) {
116
+ const { storeName, items } = deleteItem;
117
+ for await (const item of items) {
118
+ if (item) {
119
+ if (typeof item === 'object') {
120
+ const keyValuesPath = this.getIndexKeyValuesPath(item);
121
+ await this.db.delete(keyValuesPath, storeName);
122
+ }
347
123
  }
348
- });
349
- });
350
- };
124
+ }
125
+ }
126
+ }
351
127
  //#region platform-specific helper methods
352
128
  /**
353
129
  * Retrieves concatenated primary key values from a model
@@ -355,11 +131,8 @@ var AsyncStorageAdapter = /** @class */ (function (_super) {
355
131
  * @param model
356
132
  * @returns
357
133
  */
358
- AsyncStorageAdapter.prototype.getIndexKeyValuesPath = function (model) {
134
+ getIndexKeyValuesPath(model) {
359
135
  return this.getIndexKeyValuesFromModel(model).join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
360
- };
361
- return AsyncStorageAdapter;
362
- }(StorageAdapterBase));
363
- export { AsyncStorageAdapter };
136
+ }
137
+ }
364
138
  export default new AsyncStorageAdapter();
365
- //# sourceMappingURL=AsyncStorageAdapter.js.map