@aws-amplify/datastore 3.14.4 → 3.14.5-unstable.2

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 (178) hide show
  1. package/CHANGELOG.md +0 -8
  2. package/lib/authModeStrategies/defaultAuthStrategy.d.ts +2 -0
  3. package/lib/authModeStrategies/index.d.ts +2 -0
  4. package/lib/authModeStrategies/multiAuthStrategy.d.ts +13 -0
  5. package/lib/authModeStrategies/multiAuthStrategy.js +6 -64
  6. package/lib/authModeStrategies/multiAuthStrategy.js.map +1 -1
  7. package/lib/datastore/datastore.d.ts +207 -0
  8. package/lib/datastore/datastore.js +703 -297
  9. package/lib/datastore/datastore.js.map +1 -1
  10. package/lib/index.d.ts +16 -0
  11. package/lib/index.js +6 -4
  12. package/lib/index.js.map +1 -1
  13. package/lib/predicates/index.d.ts +30 -0
  14. package/lib/predicates/index.js +127 -6
  15. package/lib/predicates/index.js.map +1 -1
  16. package/lib/predicates/next.d.ts +301 -0
  17. package/lib/predicates/next.js +816 -0
  18. package/lib/predicates/next.js.map +1 -0
  19. package/lib/predicates/sort.d.ts +8 -0
  20. package/lib/predicates/sort.js +10 -4
  21. package/lib/predicates/sort.js.map +1 -1
  22. package/lib/ssr/index.d.ts +3 -0
  23. package/lib/storage/adapter/AsyncStorageAdapter.d.ts +42 -0
  24. package/lib/storage/adapter/AsyncStorageAdapter.js +141 -382
  25. package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  26. package/lib/storage/adapter/AsyncStorageDatabase.d.ts +39 -0
  27. package/lib/storage/adapter/AsyncStorageDatabase.js +37 -98
  28. package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  29. package/lib/storage/adapter/InMemoryStore.d.ts +11 -0
  30. package/lib/storage/adapter/InMemoryStore.js +16 -67
  31. package/lib/storage/adapter/InMemoryStore.js.map +1 -1
  32. package/lib/storage/adapter/InMemoryStore.native.d.ts +1 -0
  33. package/lib/storage/adapter/InMemoryStore.native.js +2 -4
  34. package/lib/storage/adapter/InMemoryStore.native.js.map +1 -1
  35. package/lib/storage/adapter/IndexedDBAdapter.d.ts +61 -0
  36. package/lib/storage/adapter/IndexedDBAdapter.js +275 -419
  37. package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
  38. package/lib/storage/adapter/getDefaultAdapter/index.d.ts +3 -0
  39. package/lib/storage/adapter/getDefaultAdapter/index.js +3 -5
  40. package/lib/storage/adapter/getDefaultAdapter/index.js.map +1 -1
  41. package/lib/storage/adapter/getDefaultAdapter/index.native.d.ts +3 -0
  42. package/lib/storage/adapter/getDefaultAdapter/index.native.js +2 -4
  43. package/lib/storage/adapter/getDefaultAdapter/index.native.js.map +1 -1
  44. package/lib/storage/adapter/index.d.ts +9 -0
  45. package/lib/storage/relationship.d.ts +140 -0
  46. package/lib/storage/relationship.js +335 -0
  47. package/lib/storage/relationship.js.map +1 -0
  48. package/lib/storage/storage.d.ts +50 -0
  49. package/lib/storage/storage.js +72 -143
  50. package/lib/storage/storage.js.map +1 -1
  51. package/lib/sync/datastoreConnectivity.d.ts +16 -0
  52. package/lib/sync/datastoreConnectivity.js +6 -55
  53. package/lib/sync/datastoreConnectivity.js.map +1 -1
  54. package/lib/sync/datastoreReachability/index.d.ts +3 -0
  55. package/lib/sync/datastoreReachability/index.native.d.ts +3 -0
  56. package/lib/sync/datastoreReachability/index.native.js +2 -4
  57. package/lib/sync/datastoreReachability/index.native.js.map +1 -1
  58. package/lib/sync/index.d.ts +89 -0
  59. package/lib/sync/index.js +49 -124
  60. package/lib/sync/index.js.map +1 -1
  61. package/lib/sync/merger.d.ts +17 -0
  62. package/lib/sync/merger.js +8 -74
  63. package/lib/sync/merger.js.map +1 -1
  64. package/lib/sync/outbox.d.ts +27 -0
  65. package/lib/sync/outbox.js +24 -97
  66. package/lib/sync/outbox.js.map +1 -1
  67. package/lib/sync/processors/errorMaps.d.ts +17 -0
  68. package/lib/sync/processors/errorMaps.js +5 -35
  69. package/lib/sync/processors/errorMaps.js.map +1 -1
  70. package/lib/sync/processors/mutation.d.ts +58 -0
  71. package/lib/sync/processors/mutation.js +47 -131
  72. package/lib/sync/processors/mutation.js.map +1 -1
  73. package/lib/sync/processors/subscription.d.ts +33 -0
  74. package/lib/sync/processors/subscription.js +29 -102
  75. package/lib/sync/processors/subscription.js.map +1 -1
  76. package/lib/sync/processors/sync.d.ts +28 -0
  77. package/lib/sync/processors/sync.js +26 -102
  78. package/lib/sync/processors/sync.js.map +1 -1
  79. package/lib/sync/utils.d.ts +42 -0
  80. package/lib/sync/utils.js +40 -103
  81. package/lib/sync/utils.js.map +1 -1
  82. package/lib/types.d.ts +554 -0
  83. package/lib/types.js +9 -39
  84. package/lib/types.js.map +1 -1
  85. package/lib/util.d.ts +189 -0
  86. package/lib/util.js +192 -188
  87. package/lib/util.js.map +1 -1
  88. package/lib-esm/authModeStrategies/multiAuthStrategy.js +2 -57
  89. package/lib-esm/authModeStrategies/multiAuthStrategy.js.map +1 -1
  90. package/lib-esm/datastore/datastore.d.ts +59 -8
  91. package/lib-esm/datastore/datastore.js +642 -234
  92. package/lib-esm/datastore/datastore.js.map +1 -1
  93. package/lib-esm/index.d.ts +3 -2
  94. package/lib-esm/index.js +2 -1
  95. package/lib-esm/index.js.map +1 -1
  96. package/lib-esm/predicates/index.d.ts +16 -2
  97. package/lib-esm/predicates/index.js +128 -7
  98. package/lib-esm/predicates/index.js.map +1 -1
  99. package/lib-esm/predicates/next.d.ts +301 -0
  100. package/lib-esm/predicates/next.js +812 -0
  101. package/lib-esm/predicates/next.js.map +1 -0
  102. package/lib-esm/predicates/sort.js +10 -4
  103. package/lib-esm/predicates/sort.js.map +1 -1
  104. package/lib-esm/storage/adapter/AsyncStorageAdapter.d.ts +2 -1
  105. package/lib-esm/storage/adapter/AsyncStorageAdapter.js +112 -350
  106. package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
  107. package/lib-esm/storage/adapter/AsyncStorageDatabase.js +7 -68
  108. package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
  109. package/lib-esm/storage/adapter/InMemoryStore.d.ts +1 -1
  110. package/lib-esm/storage/adapter/InMemoryStore.js +1 -52
  111. package/lib-esm/storage/adapter/InMemoryStore.js.map +1 -1
  112. package/lib-esm/storage/adapter/IndexedDBAdapter.d.ts +4 -2
  113. package/lib-esm/storage/adapter/IndexedDBAdapter.js +230 -367
  114. package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
  115. package/lib-esm/storage/adapter/getDefaultAdapter/index.js.map +1 -1
  116. package/lib-esm/storage/relationship.d.ts +140 -0
  117. package/lib-esm/storage/relationship.js +333 -0
  118. package/lib-esm/storage/relationship.js.map +1 -0
  119. package/lib-esm/storage/storage.d.ts +7 -6
  120. package/lib-esm/storage/storage.js +33 -101
  121. package/lib-esm/storage/storage.js.map +1 -1
  122. package/lib-esm/sync/datastoreConnectivity.js +1 -47
  123. package/lib-esm/sync/datastoreConnectivity.js.map +1 -1
  124. package/lib-esm/sync/index.js +4 -76
  125. package/lib-esm/sync/index.js.map +1 -1
  126. package/lib-esm/sync/merger.js +1 -67
  127. package/lib-esm/sync/merger.js.map +1 -1
  128. package/lib-esm/sync/outbox.js +1 -74
  129. package/lib-esm/sync/outbox.js.map +1 -1
  130. package/lib-esm/sync/processors/errorMaps.js +2 -32
  131. package/lib-esm/sync/processors/errorMaps.js.map +1 -1
  132. package/lib-esm/sync/processors/mutation.js +12 -93
  133. package/lib-esm/sync/processors/mutation.js.map +1 -1
  134. package/lib-esm/sync/processors/subscription.js +6 -69
  135. package/lib-esm/sync/processors/subscription.js.map +1 -1
  136. package/lib-esm/sync/processors/sync.js +2 -75
  137. package/lib-esm/sync/processors/sync.js.map +1 -1
  138. package/lib-esm/sync/utils.d.ts +1 -1
  139. package/lib-esm/sync/utils.js +32 -95
  140. package/lib-esm/sync/utils.js.map +1 -1
  141. package/lib-esm/types.d.ts +63 -10
  142. package/lib-esm/types.js +7 -38
  143. package/lib-esm/types.js.map +1 -1
  144. package/lib-esm/util.d.ts +39 -6
  145. package/lib-esm/util.js +171 -171
  146. package/lib-esm/util.js.map +1 -1
  147. package/package.json +21 -14
  148. package/src/authModeStrategies/multiAuthStrategy.ts +2 -2
  149. package/src/datastore/datastore.ts +699 -206
  150. package/src/index.ts +4 -0
  151. package/src/predicates/index.ts +143 -17
  152. package/src/predicates/next.ts +967 -0
  153. package/src/predicates/sort.ts +8 -2
  154. package/src/storage/adapter/AsyncStorageAdapter.ts +59 -181
  155. package/src/storage/adapter/AsyncStorageDatabase.ts +16 -15
  156. package/src/storage/adapter/InMemoryStore.ts +5 -2
  157. package/src/storage/adapter/IndexedDBAdapter.ts +169 -192
  158. package/src/storage/adapter/getDefaultAdapter/index.ts +2 -2
  159. package/src/storage/relationship.ts +272 -0
  160. package/src/storage/storage.ts +56 -37
  161. package/src/sync/datastoreConnectivity.ts +4 -4
  162. package/src/sync/index.ts +22 -28
  163. package/src/sync/merger.ts +1 -1
  164. package/src/sync/outbox.ts +6 -6
  165. package/src/sync/processors/errorMaps.ts +1 -1
  166. package/src/sync/processors/mutation.ts +23 -19
  167. package/src/sync/processors/subscription.ts +20 -16
  168. package/src/sync/processors/sync.ts +17 -17
  169. package/src/sync/utils.ts +42 -48
  170. package/src/types.ts +128 -16
  171. package/src/util.ts +108 -150
  172. package/build.js +0 -5
  173. package/dist/aws-amplify-datastore.js +0 -92787
  174. package/dist/aws-amplify-datastore.js.map +0 -1
  175. package/dist/aws-amplify-datastore.min.js +0 -66
  176. package/dist/aws-amplify-datastore.min.js.map +0 -1
  177. package/index.js +0 -7
  178. package/webpack.config.dev.js +0 -6
@@ -35,7 +35,7 @@ export class ModelSortPredicateCreator {
35
35
  const result = (sortDirection: SortDirection) => {
36
36
  ModelSortPredicateCreator.sortPredicateGroupsMap
37
37
  .get(receiver)
38
- .push({ field, sortDirection });
38
+ ?.push({ field, sortDirection });
39
39
 
40
40
  return receiver;
41
41
  };
@@ -66,7 +66,13 @@ export class ModelSortPredicateCreator {
66
66
  throw new Error('The predicate is not valid');
67
67
  }
68
68
 
69
- return ModelSortPredicateCreator.sortPredicateGroupsMap.get(predicate);
69
+ const predicateGroup =
70
+ ModelSortPredicateCreator.sortPredicateGroupsMap.get(predicate);
71
+ if (predicateGroup) {
72
+ return predicateGroup;
73
+ } else {
74
+ throw new Error('Predicate group not found');
75
+ }
70
76
  }
71
77
 
72
78
  // transforms cb-style predicate into Proxy
@@ -2,10 +2,7 @@ import { ConsoleLogger as Logger } from '@aws-amplify/core';
2
2
  import AsyncStorageDatabase from './AsyncStorageDatabase';
3
3
  import { Adapter } from './index';
4
4
  import { ModelInstanceCreator } from '../../datastore/datastore';
5
- import {
6
- ModelPredicateCreator,
7
- ModelSortPredicateCreator,
8
- } from '../../predicates';
5
+ import { ModelPredicateCreator } from '../../predicates';
9
6
  import {
10
7
  InternalSchema,
11
8
  isPredicateObj,
@@ -23,13 +20,13 @@ import {
23
20
  } from '../../types';
24
21
  import {
25
22
  DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR,
26
- exhaustiveCheck,
27
23
  getIndex,
28
24
  getIndexFromAssociation,
29
25
  isModelConstructor,
30
26
  traverseModel,
31
27
  validatePredicate,
32
- sortCompareFunction,
28
+ inMemoryPagination,
29
+ NAMESPACES,
33
30
  keysEqual,
34
31
  getStorename,
35
32
  getIndexKeys,
@@ -40,17 +37,21 @@ import {
40
37
  const logger = new Logger('DataStore');
41
38
 
42
39
  export class AsyncStorageAdapter implements Adapter {
43
- private schema: InternalSchema;
44
- private namespaceResolver: NamespaceResolver;
45
- private modelInstanceCreator: ModelInstanceCreator;
46
- private getModelConstructorByModelName: (
47
- namsespaceName: string,
40
+ // Non-null assertions (bang operators) added to most properties to make TS happy.
41
+ // For now, we can be reasonably sure they're available when they're needed, because
42
+ // the adapter is not used directly outside the library boundary.
43
+ // TODO: rejigger for DI?
44
+ private schema!: InternalSchema;
45
+ private namespaceResolver!: NamespaceResolver;
46
+ private modelInstanceCreator!: ModelInstanceCreator;
47
+ private getModelConstructorByModelName!: (
48
+ namsespaceName: NAMESPACES,
48
49
  modelName: string
49
50
  ) => PersistentModelConstructor<any>;
50
- private db: AsyncStorageDatabase;
51
- private initPromise: Promise<void>;
52
- private resolve: (value?: any) => void;
53
- private reject: (value?: any) => void;
51
+ private db!: AsyncStorageDatabase;
52
+ private initPromise!: Promise<void>;
53
+ private resolve!: (value?: any) => void;
54
+ private reject!: (value?: any) => void;
54
55
 
55
56
  private getStorenameForModel(
56
57
  modelConstructor: PersistentModelConstructor<any>
@@ -88,7 +89,7 @@ export class AsyncStorageAdapter implements Adapter {
88
89
  namespaceResolver: NamespaceResolver,
89
90
  modelInstanceCreator: ModelInstanceCreator,
90
91
  getModelConstructorByModelName: (
91
- namsespaceName: string,
92
+ namsespaceName: NAMESPACES,
92
93
  modelName: string
93
94
  ) => PersistentModelConstructor<any>
94
95
  ) {
@@ -131,7 +132,7 @@ export class AsyncStorageAdapter implements Adapter {
131
132
  model,
132
133
  this.schema.namespaces[namespaceName],
133
134
  this.modelInstanceCreator,
134
- this.getModelConstructorByModelName
135
+ this.getModelConstructorByModelName as any
135
136
  );
136
137
 
137
138
  const set = new Set<string>();
@@ -152,7 +153,7 @@ export class AsyncStorageAdapter implements Adapter {
152
153
 
153
154
  if (condition && fromDB) {
154
155
  const predicates = ModelPredicateCreator.getPredicates(condition);
155
- const { predicates: predicateObjs, type } = predicates;
156
+ const { predicates: predicateObjs, type } = predicates!;
156
157
 
157
158
  const isValid = validatePredicate(fromDB, type, predicateObjs);
158
159
 
@@ -189,17 +190,16 @@ export class AsyncStorageAdapter implements Adapter {
189
190
  result.push([instance, opType]);
190
191
  }
191
192
  }
192
-
193
193
  return result;
194
194
  }
195
195
 
196
196
  private async load<T>(
197
- namespaceName: string,
197
+ namespaceName: NAMESPACES,
198
198
  srcModelName: string,
199
199
  records: T[]
200
200
  ): Promise<T[]> {
201
201
  const namespace = this.schema.namespaces[namespaceName];
202
- const relations = namespace.relationships[srcModelName].relationTypes;
202
+ const relations = namespace.relationships![srcModelName].relationTypes;
203
203
  const connectionStoreNames = relations.map(({ modelName }) => {
204
204
  return getStorename(namespaceName, modelName);
205
205
  });
@@ -214,111 +214,6 @@ export class AsyncStorageAdapter implements Adapter {
214
214
  );
215
215
  }
216
216
 
217
- for await (const relation of relations) {
218
- const { fieldName, modelName, targetName, targetNames, relationType } =
219
- relation;
220
- const storeName = getStorename(namespaceName, modelName);
221
- const modelConstructor = this.getModelConstructorByModelName(
222
- namespaceName,
223
- modelName
224
- );
225
-
226
- switch (relationType) {
227
- case 'HAS_ONE':
228
- for await (const recordItem of records) {
229
- // ASYNC CPK TODO: make this cleaner
230
- if (targetNames?.length) {
231
- let getByFields = [];
232
- let allPresent;
233
- // iterate through all targetnames to make sure they are all present in the recordItem
234
- allPresent = targetNames.every(targetName => {
235
- return recordItem[targetName] != null;
236
- });
237
-
238
- if (!allPresent) {
239
- break;
240
- }
241
-
242
- getByFields = targetNames as any;
243
-
244
- // keys are the key values
245
- const keys = getByFields
246
- .map(getByField => recordItem[getByField])
247
- .join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
248
-
249
- const connectionRecord = await this.db.get(keys, storeName);
250
-
251
- recordItem[fieldName] =
252
- connectionRecord &&
253
- this.modelInstanceCreator(modelConstructor, connectionRecord);
254
- } else {
255
- const getByfield = recordItem[targetName]
256
- ? targetName
257
- : fieldName;
258
- if (!recordItem[getByfield]) break;
259
-
260
- const key = recordItem[getByfield];
261
-
262
- const connectionRecord = await this.db.get(key, storeName);
263
-
264
- recordItem[fieldName] =
265
- connectionRecord &&
266
- this.modelInstanceCreator(modelConstructor, connectionRecord);
267
- }
268
- }
269
-
270
- break;
271
- case 'BELONGS_TO':
272
- for await (const recordItem of records) {
273
- // ASYNC CPK TODO: make this cleaner
274
- if (targetNames?.length) {
275
- let allPresent;
276
- // iterate through all targetnames to make sure they are all present in the recordItem
277
- allPresent = targetNames.every(targetName => {
278
- return recordItem[targetName] != null;
279
- });
280
-
281
- // If not present, there is not yet a connected record
282
- if (!allPresent) {
283
- break;
284
- }
285
-
286
- const keys = targetNames
287
- .map(targetName => recordItem[targetName])
288
- .join(DEFAULT_PRIMARY_KEY_VALUE_SEPARATOR);
289
-
290
- // Retrieve the connected record
291
- const connectionRecord = await this.db.get(keys, storeName);
292
-
293
- recordItem[fieldName] =
294
- connectionRecord &&
295
- this.modelInstanceCreator(modelConstructor, connectionRecord);
296
-
297
- targetNames?.map(targetName => {
298
- delete recordItem[targetName];
299
- });
300
- } else if (recordItem[targetName as any]) {
301
- const key = recordItem[targetName];
302
-
303
- const connectionRecord = await this.db.get(key, storeName);
304
-
305
- recordItem[fieldName] =
306
- connectionRecord &&
307
- this.modelInstanceCreator(modelConstructor, connectionRecord);
308
- delete recordItem[targetName];
309
- }
310
- }
311
-
312
- break;
313
- case 'HAS_MANY':
314
- // TODO: Lazy loading
315
- break;
316
- default:
317
- exhaustiveCheck(relationType);
318
- break;
319
- }
320
- }
321
-
322
217
  return records.map(record =>
323
218
  this.modelInstanceCreator(modelConstructor, record)
324
219
  );
@@ -330,7 +225,9 @@ export class AsyncStorageAdapter implements Adapter {
330
225
  pagination?: PaginationInput<T>
331
226
  ): Promise<T[]> {
332
227
  const storeName = this.getStorenameForModel(modelConstructor);
333
- const namespaceName = this.namespaceResolver(modelConstructor);
228
+ const namespaceName = this.namespaceResolver(
229
+ modelConstructor
230
+ ) as NAMESPACES;
334
231
 
335
232
  const predicates =
336
233
  predicate && ModelPredicateCreator.getPredicates(predicate);
@@ -344,7 +241,7 @@ export class AsyncStorageAdapter implements Adapter {
344
241
  const hasSort = pagination && pagination.sort;
345
242
  const hasPagination = pagination && pagination.limit;
346
243
 
347
- const records: T[] = await (async () => {
244
+ const records: T[] = (await (async () => {
348
245
  if (queryByKey) {
349
246
  const record = await this.getByKey(storeName, queryByKey);
350
247
  return record ? [record] : [];
@@ -361,7 +258,7 @@ export class AsyncStorageAdapter implements Adapter {
361
258
  }
362
259
 
363
260
  return this.getAll(storeName);
364
- })();
261
+ })()) as T[];
365
262
 
366
263
  return await this.load(namespaceName, modelConstructor.name, records);
367
264
  }
@@ -390,7 +287,7 @@ export class AsyncStorageAdapter implements Adapter {
390
287
  return;
391
288
  }
392
289
 
393
- const keyValues = [];
290
+ const keyValues = [] as any[];
394
291
 
395
292
  for (const key of keys) {
396
293
  const predicateObj = predicateObjs.find(
@@ -424,26 +321,7 @@ export class AsyncStorageAdapter implements Adapter {
424
321
  records: T[],
425
322
  pagination?: PaginationInput<T>
426
323
  ): T[] {
427
- if (pagination && records.length > 1) {
428
- if (pagination.sort) {
429
- const sortPredicates = ModelSortPredicateCreator.getPredicates(
430
- pagination.sort
431
- );
432
-
433
- if (sortPredicates.length) {
434
- const compareFn = sortCompareFunction(sortPredicates);
435
- records.sort(compareFn);
436
- }
437
- }
438
- const { page = 0, limit = 0 } = pagination;
439
- const start = Math.max(0, page * limit) || 0;
440
-
441
- const end = limit > 0 ? start + limit : records.length;
442
-
443
- return records.slice(start, end);
444
- }
445
-
446
- return records;
324
+ return inMemoryPagination(records, pagination);
447
325
  }
448
326
 
449
327
  async queryOne<T extends PersistentModel>(
@@ -463,14 +341,15 @@ export class AsyncStorageAdapter implements Adapter {
463
341
  const deleteQueue: { storeName: string; items: T[] }[] = [];
464
342
 
465
343
  if (isModelConstructor(modelOrModelConstructor)) {
466
- const modelConstructor = modelOrModelConstructor;
467
- const nameSpace = this.namespaceResolver(modelConstructor);
344
+ const modelConstructor =
345
+ modelOrModelConstructor as PersistentModelConstructor<T>;
346
+ const nameSpace = this.namespaceResolver(modelConstructor) as NAMESPACES;
468
347
 
469
348
  // models to be deleted.
470
- const models = await this.query(modelConstructor, condition);
349
+ const models = await this.query(modelConstructor, condition!);
471
350
  // TODO: refactor this to use a function like getRelations()
472
351
  const relations =
473
- this.schema.namespaces[nameSpace].relationships[modelConstructor.name]
352
+ this.schema.namespaces[nameSpace].relationships![modelConstructor.name]
474
353
  .relationTypes;
475
354
 
476
355
  if (condition !== undefined) {
@@ -509,11 +388,11 @@ export class AsyncStorageAdapter implements Adapter {
509
388
  return [models, deletedModels];
510
389
  }
511
390
  } else {
512
- const model = modelOrModelConstructor;
391
+ const model = modelOrModelConstructor as T;
513
392
 
514
393
  const modelConstructor = Object.getPrototypeOf(model)
515
394
  .constructor as PersistentModelConstructor<T>;
516
- const namespaceName = this.namespaceResolver(modelConstructor);
395
+ const nameSpace = this.namespaceResolver(modelConstructor) as NAMESPACES;
517
396
 
518
397
  const storeName = this.getStorenameForModel(modelConstructor);
519
398
 
@@ -530,7 +409,7 @@ export class AsyncStorageAdapter implements Adapter {
530
409
  }
531
410
 
532
411
  const predicates = ModelPredicateCreator.getPredicates(condition);
533
- const { predicates: predicateObjs, type } = predicates;
412
+ const { predicates: predicateObjs, type } = predicates!;
534
413
 
535
414
  const isValid = validatePredicate(fromDB, type, predicateObjs);
536
415
  if (!isValid) {
@@ -541,20 +420,19 @@ export class AsyncStorageAdapter implements Adapter {
541
420
  }
542
421
 
543
422
  const relations =
544
- this.schema.namespaces[namespaceName].relationships[
423
+ this.schema.namespaces[nameSpace].relationships![
545
424
  modelConstructor.name
546
425
  ].relationTypes;
547
-
548
426
  await this.deleteTraverse(
549
427
  relations,
550
428
  [model],
551
429
  modelConstructor.name,
552
- namespaceName,
430
+ nameSpace,
553
431
  deleteQueue
554
432
  );
555
433
  } else {
556
434
  const relations =
557
- this.schema.namespaces[namespaceName].relationships[
435
+ this.schema.namespaces[nameSpace].relationships![
558
436
  modelConstructor.name
559
437
  ].relationTypes;
560
438
 
@@ -562,7 +440,7 @@ export class AsyncStorageAdapter implements Adapter {
562
440
  relations,
563
441
  [model],
564
442
  modelConstructor.name,
565
- namespaceName,
443
+ nameSpace,
566
444
  deleteQueue
567
445
  );
568
446
  }
@@ -581,7 +459,7 @@ export class AsyncStorageAdapter implements Adapter {
581
459
  private async deleteItem<T extends PersistentModel>(
582
460
  deleteQueue?: { storeName: string; items: T[] | IDBValidKey[] }[]
583
461
  ) {
584
- for await (const deleteItem of deleteQueue) {
462
+ for await (const deleteItem of deleteQueue!) {
585
463
  const { storeName, items } = deleteItem;
586
464
 
587
465
  for await (const item of items) {
@@ -594,6 +472,7 @@ export class AsyncStorageAdapter implements Adapter {
594
472
  }
595
473
  }
596
474
  }
475
+
597
476
  /**
598
477
  * Populates the delete Queue with all the items to delete
599
478
  * @param relations
@@ -606,7 +485,7 @@ export class AsyncStorageAdapter implements Adapter {
606
485
  relations: RelationType[],
607
486
  models: T[],
608
487
  srcModel: string,
609
- nameSpace: string,
488
+ nameSpace: NAMESPACES,
610
489
  deleteQueue: { storeName: string; items: T[] }[]
611
490
  ): Promise<void> {
612
491
  for await (const rel of relations) {
@@ -621,7 +500,7 @@ export class AsyncStorageAdapter implements Adapter {
621
500
 
622
501
  const index: string | undefined =
623
502
  getIndex(
624
- this.schema.namespaces[nameSpace].relationships[modelName]
503
+ this.schema.namespaces[nameSpace].relationships![modelName]
625
504
  .relationTypes,
626
505
  srcModel
627
506
  ) ||
@@ -629,8 +508,8 @@ export class AsyncStorageAdapter implements Adapter {
629
508
  // i.e. for keyName connections, attempt to find one by the
630
509
  // associatedWith property
631
510
  getIndexFromAssociation(
632
- this.schema.namespaces[nameSpace].relationships[modelName].indexes,
633
- rel.associatedWith
511
+ this.schema.namespaces[nameSpace].relationships![modelName].indexes,
512
+ rel.associatedWith!
634
513
  );
635
514
 
636
515
  switch (relationType) {
@@ -664,9 +543,9 @@ export class AsyncStorageAdapter implements Adapter {
664
543
 
665
544
  if (hasConnectedModelFields && isUnidirectionalConnection) {
666
545
  // Values will be that of the child model
667
- values = targetNames.map(
668
- targetName => model[targetName]
669
- ) as any;
546
+ values = targetNames
547
+ .filter(targetName => model[targetName] ?? false)
548
+ .map(targetName => model[targetName]) as any;
670
549
  } else {
671
550
  // values will be that of the parent model
672
551
  values = keyValuesPath.split(
@@ -702,7 +581,7 @@ export class AsyncStorageAdapter implements Adapter {
702
581
  }
703
582
 
704
583
  await this.deleteTraverse<T>(
705
- this.schema.namespaces[nameSpace].relationships[modelName]
584
+ this.schema.namespaces[nameSpace].relationships![modelName]
706
585
  .relationTypes,
707
586
  recordToDelete,
708
587
  modelName,
@@ -711,10 +590,10 @@ export class AsyncStorageAdapter implements Adapter {
711
590
  );
712
591
  } else {
713
592
  const hasOneIndex = index || associatedWith;
714
- const hasOneCustomField = targetName in model;
593
+ const hasOneCustomField = targetName! in model;
715
594
  const keyValuesPath: string = this.getIndexKeyValuesPath(model);
716
595
  const value = hasOneCustomField
717
- ? model[targetName]
596
+ ? model[targetName!]
718
597
  : keyValuesPath;
719
598
 
720
599
  if (!value) break;
@@ -726,7 +605,7 @@ export class AsyncStorageAdapter implements Adapter {
726
605
  ) as T[];
727
606
 
728
607
  await this.deleteTraverse<T>(
729
- this.schema.namespaces[nameSpace].relationships[modelName]
608
+ this.schema.namespaces[nameSpace].relationships![modelName]
730
609
  .relationTypes,
731
610
  recordToDelete,
732
611
  modelName,
@@ -743,14 +622,14 @@ export class AsyncStorageAdapter implements Adapter {
743
622
 
744
623
  const allRecords = await this.db.getAll(storeName);
745
624
 
746
- const indices = index.split(IDENTIFIER_KEY_SEPARATOR);
625
+ const indices = index!.split(IDENTIFIER_KEY_SEPARATOR);
747
626
 
748
627
  const childrenArray = allRecords.filter(childItem =>
749
628
  indices.every(index => keyValues.includes(childItem[index]))
750
629
  ) as T[];
751
630
 
752
631
  await this.deleteTraverse<T>(
753
- this.schema.namespaces[nameSpace].relationships[modelName]
632
+ this.schema.namespaces[nameSpace].relationships![modelName]
754
633
  .relationTypes,
755
634
  childrenArray,
756
635
  modelName,
@@ -763,8 +642,7 @@ export class AsyncStorageAdapter implements Adapter {
763
642
  // Intentionally blank
764
643
  break;
765
644
  default:
766
- exhaustiveCheck(relationType);
767
- break;
645
+ throw new Error(`Invalid relationType ${relationType}`);
768
646
  }
769
647
  }
770
648
 
@@ -782,8 +660,8 @@ export class AsyncStorageAdapter implements Adapter {
782
660
  async clear(): Promise<void> {
783
661
  await this.db.clear();
784
662
 
785
- this.db = undefined;
786
- this.initPromise = undefined;
663
+ this.db = undefined!;
664
+ this.initPromise = undefined!;
787
665
  }
788
666
 
789
667
  async batchSave<T extends PersistentModel>(
@@ -812,7 +690,7 @@ export class AsyncStorageAdapter implements Adapter {
812
690
  const { instance } = connectedModels.find(({ instance }) => {
813
691
  const instanceKeyValuesPath = this.getIndexKeyValuesPath(instance);
814
692
  return keysEqual([instanceKeyValuesPath], [keyValuesPath]);
815
- });
693
+ })!;
816
694
 
817
695
  batch.push(instance);
818
696
  }
@@ -50,7 +50,7 @@ class AsyncStorageDatabase {
50
50
  monotonicFactoriesMap.set(storeName, monotonicUlidFactory());
51
51
  }
52
52
 
53
- return monotonicFactoriesMap.get(storeName);
53
+ return monotonicFactoriesMap.get(storeName)!;
54
54
  }
55
55
 
56
56
  async init(): Promise<void> {
@@ -58,7 +58,7 @@ class AsyncStorageDatabase {
58
58
 
59
59
  const allKeys: string[] = await this.storage.getAllKeys();
60
60
 
61
- const keysForCollectionEntries = [];
61
+ const keysForCollectionEntries: string[] = [];
62
62
 
63
63
  for (const key of allKeys) {
64
64
  const [dbName, storeName, recordType, ulidOrId, id] = key.split('::');
@@ -79,7 +79,7 @@ class AsyncStorageDatabase {
79
79
 
80
80
  const item = await this.storage.getItem(oldKey);
81
81
 
82
- await this.storage.setItem(newKey, item);
82
+ await this.storage.setItem(newKey, item!);
83
83
  await this.storage.removeItem(oldKey);
84
84
 
85
85
  ulid = newUlid;
@@ -87,7 +87,7 @@ class AsyncStorageDatabase {
87
87
  ulid = ulidOrId;
88
88
  }
89
89
 
90
- this.getCollectionIndex(storeName).set(id, ulid);
90
+ this.getCollectionIndex(storeName)!.set(id, ulid);
91
91
  } else if (recordType === COLLECTION) {
92
92
  keysForCollectionEntries.push(key);
93
93
  }
@@ -108,14 +108,14 @@ class AsyncStorageDatabase {
108
108
  const idxName = indexNameFromKeys(keys);
109
109
 
110
110
  const ulid =
111
- this.getCollectionIndex(storeName).get(idxName) ||
111
+ this.getCollectionIndex(storeName)?.get(idxName) ||
112
112
  this.getMonotonicFactory(storeName)();
113
113
 
114
114
  // Retrieve db key for item
115
115
  const itemKey = this.getKeyForItem(storeName, keyValuesPath, ulid);
116
116
 
117
117
  // Set key in collection index
118
- this.getCollectionIndex(storeName).set(keyValuesPath, ulid);
118
+ this.getCollectionIndex(storeName)?.set(keyValuesPath, ulid);
119
119
 
120
120
  // Save item in db
121
121
  await this.storage.setItem(itemKey, JSON.stringify(item));
@@ -131,11 +131,12 @@ class AsyncStorageDatabase {
131
131
  }
132
132
 
133
133
  const result: [T, OpType][] = [];
134
- const collection = this.getCollectionIndex(storeName);
134
+
135
+ const collection = this.getCollectionIndex(storeName)!;
135
136
 
136
137
  const keysToDelete = new Set<string>();
137
138
  const keysToSave = new Set<string>();
138
- const allItemsKeys = [];
139
+ const allItemsKeys: string[] = [];
139
140
  const itemsMap: Record<string, { ulid: string; model: T }> = {};
140
141
 
141
142
  /* Populate allItemKeys, keysToDelete, and keysToSave */
@@ -252,7 +253,7 @@ class AsyncStorageDatabase {
252
253
  keyValuePath: string,
253
254
  storeName: string
254
255
  ): Promise<T> {
255
- const ulid = this.getCollectionIndex(storeName).get(keyValuePath);
256
+ const ulid = this.getCollectionIndex(storeName)!.get(keyValuePath)!;
256
257
  const itemKey = this.getKeyForItem(storeName, keyValuePath, ulid);
257
258
  const recordAsString = await this.storage.getItem(itemKey);
258
259
  const record = recordAsString && JSON.parse(recordAsString);
@@ -260,19 +261,19 @@ class AsyncStorageDatabase {
260
261
  }
261
262
 
262
263
  async getOne(firstOrLast: QueryOne, storeName: string) {
263
- const collection = this.getCollectionIndex(storeName);
264
+ const collection = this.getCollectionIndex(storeName)!;
264
265
 
265
266
  const [itemId, ulid] =
266
267
  firstOrLast === QueryOne.FIRST
267
268
  ? (() => {
268
269
  let id: string, ulid: string;
269
270
  for ([id, ulid] of collection) break; // Get first element of the set
270
- return [id, ulid];
271
+ return [id!, ulid!];
271
272
  })()
272
273
  : (() => {
273
274
  let id: string, ulid: string;
274
275
  for ([id, ulid] of collection); // Get last element of the set
275
- return [id, ulid];
276
+ return [id!, ulid!];
276
277
  })();
277
278
  const itemKey = this.getKeyForItem(storeName, itemId, ulid);
278
279
 
@@ -291,7 +292,7 @@ class AsyncStorageDatabase {
291
292
  storeName: string,
292
293
  pagination?: PaginationInput<T>
293
294
  ): Promise<T[]> {
294
- const collection = this.getCollectionIndex(storeName);
295
+ const collection = this.getCollectionIndex(storeName)!;
295
296
 
296
297
  const { page = 0, limit = 0 } = pagination || {};
297
298
  const start = Math.max(0, page * limit) || 0;
@@ -322,9 +323,9 @@ class AsyncStorageDatabase {
322
323
  }
323
324
 
324
325
  async delete(key: string, storeName: string) {
325
- const ulid = this.getCollectionIndex(storeName).get(key);
326
+ const ulid = this.getCollectionIndex(storeName)!.get(key)!;
326
327
  const itemKey = this.getKeyForItem(storeName, key, ulid);
327
- this.getCollectionIndex(storeName).delete(key);
328
+ this.getCollectionIndex(storeName)!.delete(key);
328
329
  await this.storage.removeItem(itemKey);
329
330
  }
330
331
 
@@ -6,11 +6,14 @@ export class InMemoryStore {
6
6
  };
7
7
 
8
8
  multiGet = async (keys: string[]) => {
9
- return keys.reduce((res, k) => (res.push([k, this.db.get(k)]), res), []);
9
+ return keys.reduce(
10
+ (res, k) => (res.push([k, this.db.get(k)!]), res),
11
+ [] as [string, string][]
12
+ );
10
13
  };
11
14
 
12
15
  multiRemove = async (keys: string[], callback?) => {
13
- keys.forEach(k => this.db.delete(k));
16
+ keys.forEach((k) => this.db.delete(k));
14
17
 
15
18
  typeof callback === 'function' && callback();
16
19
  };