@aws-amplify/datastore 3.7.4-custom-pk.80 → 3.7.4-custom-pk.86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aws-amplify-datastore.js +356 -168
- package/dist/aws-amplify-datastore.js.map +1 -1
- package/dist/aws-amplify-datastore.min.js +3 -3
- package/dist/aws-amplify-datastore.min.js.map +1 -1
- package/lib/datastore/datastore.js +47 -10
- package/lib/datastore/datastore.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageAdapter.js +229 -135
- package/lib/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib/storage/adapter/AsyncStorageDatabase.js +3 -2
- package/lib/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib/util.d.ts +4 -0
- package/lib/util.js +9 -0
- package/lib/util.js.map +1 -1
- package/lib-esm/datastore/datastore.js +48 -11
- package/lib-esm/datastore/datastore.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js +229 -135
- package/lib-esm/storage/adapter/AsyncStorageAdapter.js.map +1 -1
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js +4 -3
- package/lib-esm/storage/adapter/AsyncStorageDatabase.js.map +1 -1
- package/lib-esm/storage/adapter/IndexedDBAdapter.js.map +1 -1
- package/lib-esm/util.d.ts +4 -0
- package/lib-esm/util.js +8 -0
- package/lib-esm/util.js.map +1 -1
- package/package.json +7 -7
- package/src/datastore/datastore.ts +60 -12
- package/src/storage/adapter/AsyncStorageAdapter.ts +162 -35
- package/src/storage/adapter/AsyncStorageDatabase.ts +4 -2
- package/src/storage/adapter/IndexedDBAdapter.ts +0 -1
- package/src/util.ts +15 -0
|
@@ -65,8 +65,9 @@ export class AsyncStorageAdapter implements Adapter {
|
|
|
65
65
|
|
|
66
66
|
// Returns primary keys for a model
|
|
67
67
|
private getIndexKeys(namespaceName: string, modelName: string): string[] {
|
|
68
|
-
const
|
|
69
|
-
|
|
68
|
+
const namespace = this.schema.namespaces[namespaceName];
|
|
69
|
+
|
|
70
|
+
const keyPath = namespace?.keys[modelName]?.primaryKey;
|
|
70
71
|
|
|
71
72
|
if (keyPath) {
|
|
72
73
|
return keyPath;
|
|
@@ -239,7 +240,8 @@ export class AsyncStorageAdapter implements Adapter {
|
|
|
239
240
|
}
|
|
240
241
|
|
|
241
242
|
for await (const relation of relations) {
|
|
242
|
-
const { fieldName, modelName, targetName, relationType } =
|
|
243
|
+
const { fieldName, modelName, targetName, targetNames, relationType } =
|
|
244
|
+
relation;
|
|
243
245
|
const storeName = this.getStorename(namespaceName, modelName);
|
|
244
246
|
const modelConstructor = this.getModelConstructorByModelName(
|
|
245
247
|
namespaceName,
|
|
@@ -249,22 +251,84 @@ export class AsyncStorageAdapter implements Adapter {
|
|
|
249
251
|
switch (relationType) {
|
|
250
252
|
case 'HAS_ONE':
|
|
251
253
|
for await (const recordItem of records) {
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
+
// ASYNC CPK TODO: make this cleaner
|
|
255
|
+
if (targetNames?.length) {
|
|
256
|
+
let getByFields = [];
|
|
257
|
+
let allPresent;
|
|
258
|
+
// iterate through all targetnames to make sure they are all present in the recordItem
|
|
259
|
+
allPresent = targetNames.every(targetName => {
|
|
260
|
+
return recordItem[targetName] != null;
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
if (!allPresent) {
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
getByFields = targetNames as any;
|
|
268
|
+
|
|
269
|
+
// keys are the key values
|
|
270
|
+
const keys = getByFields
|
|
271
|
+
.map(getByField => recordItem[getByField])
|
|
272
|
+
.join(DEFAULT_PRIMARY_KEY_SEPARATOR);
|
|
273
|
+
|
|
274
|
+
const connectionRecord = await this.db.get(
|
|
275
|
+
keys as any,
|
|
276
|
+
storeName
|
|
277
|
+
);
|
|
278
|
+
|
|
279
|
+
recordItem[fieldName] =
|
|
280
|
+
connectionRecord &&
|
|
281
|
+
this.modelInstanceCreator(modelConstructor, connectionRecord);
|
|
282
|
+
} else {
|
|
283
|
+
const getByfield = recordItem[targetName]
|
|
284
|
+
? targetName
|
|
285
|
+
: fieldName;
|
|
286
|
+
if (!recordItem[getByfield]) break;
|
|
254
287
|
|
|
255
|
-
|
|
288
|
+
const key = recordItem[getByfield];
|
|
256
289
|
|
|
257
|
-
|
|
290
|
+
const connectionRecord = await this.db.get(key, storeName);
|
|
258
291
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
292
|
+
recordItem[fieldName] =
|
|
293
|
+
connectionRecord &&
|
|
294
|
+
this.modelInstanceCreator(modelConstructor, connectionRecord);
|
|
295
|
+
}
|
|
262
296
|
}
|
|
263
297
|
|
|
264
298
|
break;
|
|
265
299
|
case 'BELONGS_TO':
|
|
266
300
|
for await (const recordItem of records) {
|
|
267
|
-
|
|
301
|
+
// ASYNC CPK TODO: make this cleaner
|
|
302
|
+
if (targetNames?.length) {
|
|
303
|
+
let allPresent;
|
|
304
|
+
// iterate through all targetnames to make sure they are all present in the recordItem
|
|
305
|
+
allPresent = targetNames.every(targetName => {
|
|
306
|
+
return recordItem[targetName] != null;
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// If not present, there is not yet a connected record
|
|
310
|
+
if (!allPresent) {
|
|
311
|
+
break;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const keys = targetNames
|
|
315
|
+
.map(targetName => recordItem[targetName])
|
|
316
|
+
.join(DEFAULT_PRIMARY_KEY_SEPARATOR);
|
|
317
|
+
|
|
318
|
+
// Retrieve the connected record
|
|
319
|
+
const connectionRecord = await this.db.get(
|
|
320
|
+
keys as any,
|
|
321
|
+
storeName
|
|
322
|
+
);
|
|
323
|
+
|
|
324
|
+
recordItem[fieldName] =
|
|
325
|
+
connectionRecord &&
|
|
326
|
+
this.modelInstanceCreator(modelConstructor, connectionRecord);
|
|
327
|
+
|
|
328
|
+
targetNames?.map(targetName => {
|
|
329
|
+
delete recordItem[targetName];
|
|
330
|
+
});
|
|
331
|
+
} else if (recordItem[targetName as any]) {
|
|
268
332
|
const key = recordItem[targetName];
|
|
269
333
|
|
|
270
334
|
const connectionRecord = await this.db.get(key, storeName);
|
|
@@ -600,36 +664,99 @@ export class AsyncStorageAdapter implements Adapter {
|
|
|
600
664
|
switch (relationType) {
|
|
601
665
|
case 'HAS_ONE':
|
|
602
666
|
for await (const model of models) {
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
667
|
+
if (targetNames && targetNames?.length) {
|
|
668
|
+
const hasOneIndex = index || associatedWith;
|
|
669
|
+
|
|
670
|
+
// iterate over targetNames array and see if each item is present in model object
|
|
671
|
+
// targetNames here being the keys for the CHILD model
|
|
672
|
+
const hasConnectedModelFields = targetNames.every(targetName =>
|
|
673
|
+
model.hasOwnProperty(targetName)
|
|
674
|
+
);
|
|
675
|
+
|
|
676
|
+
// PK / Composite key for the parent model
|
|
677
|
+
const keyValuesPath: string = this.getIndexKeyValuesPath(model);
|
|
678
|
+
|
|
679
|
+
let values;
|
|
680
|
+
|
|
681
|
+
if (hasConnectedModelFields) {
|
|
682
|
+
// Values will be that of the child model
|
|
683
|
+
values = targetNames.map(
|
|
684
|
+
targetName => model[targetName]
|
|
685
|
+
) as any;
|
|
686
|
+
} else {
|
|
687
|
+
// values will be that of the parent model
|
|
688
|
+
values = keyValuesPath;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
if (values.length === 0) break;
|
|
692
|
+
|
|
693
|
+
const allRecords = await this.db.getAll(storeName);
|
|
694
|
+
|
|
695
|
+
let recordToDelete;
|
|
696
|
+
|
|
697
|
+
// values === targetNames
|
|
698
|
+
if (hasConnectedModelFields) {
|
|
699
|
+
/**
|
|
700
|
+
* Retrieve record by finding the record where all
|
|
701
|
+
* targetNames are present on the connected model
|
|
702
|
+
*/
|
|
703
|
+
recordToDelete = allRecords.filter(childItem =>
|
|
704
|
+
values.every(value => childItem[value] != null)
|
|
705
|
+
) as T[];
|
|
706
|
+
} else {
|
|
707
|
+
// values === keyValuePath
|
|
708
|
+
recordToDelete = allRecords.filter(
|
|
709
|
+
childItem => childItem[hasOneIndex] === values
|
|
710
|
+
) as T[];
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
await this.deleteTraverse<T>(
|
|
714
|
+
this.schema.namespaces[nameSpace].relationships[modelName]
|
|
715
|
+
.relationTypes,
|
|
716
|
+
recordToDelete,
|
|
717
|
+
modelName,
|
|
718
|
+
nameSpace,
|
|
719
|
+
deleteQueue
|
|
720
|
+
);
|
|
721
|
+
} else {
|
|
722
|
+
const hasOneIndex = index || associatedWith;
|
|
723
|
+
const hasOneCustomField = targetName in model;
|
|
724
|
+
const keyValuesPath: string = this.getIndexKeyValuesPath(model);
|
|
725
|
+
const value = hasOneCustomField
|
|
726
|
+
? model[targetName]
|
|
727
|
+
: keyValuesPath;
|
|
728
|
+
|
|
729
|
+
if (!value) break;
|
|
730
|
+
|
|
731
|
+
const allRecords = await this.db.getAll(storeName);
|
|
732
|
+
|
|
733
|
+
const recordToDelete = allRecords.filter(
|
|
734
|
+
childItem => childItem[hasOneIndex] === value
|
|
735
|
+
) as T[];
|
|
736
|
+
|
|
737
|
+
await this.deleteTraverse<T>(
|
|
738
|
+
this.schema.namespaces[nameSpace].relationships[modelName]
|
|
739
|
+
.relationTypes,
|
|
740
|
+
recordToDelete,
|
|
741
|
+
modelName,
|
|
742
|
+
nameSpace,
|
|
743
|
+
deleteQueue
|
|
744
|
+
);
|
|
745
|
+
}
|
|
624
746
|
}
|
|
625
747
|
break;
|
|
626
748
|
case 'HAS_MANY':
|
|
627
749
|
for await (const model of models) {
|
|
628
|
-
|
|
750
|
+
// Key values for the parent model:
|
|
751
|
+
const keyValues: string[] = this.getIndexKeyValues(model);
|
|
629
752
|
|
|
630
753
|
const allRecords = await this.db.getAll(storeName);
|
|
631
|
-
|
|
632
|
-
|
|
754
|
+
|
|
755
|
+
// Use constant if we go with this approach
|
|
756
|
+
const indices = index.split('-');
|
|
757
|
+
|
|
758
|
+
const childrenArray = allRecords.filter(childItem =>
|
|
759
|
+
indices.every(index => keyValues.includes(childItem[index]))
|
|
633
760
|
) as T[];
|
|
634
761
|
|
|
635
762
|
await this.deleteTraverse<T>(
|
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
PersistentModel,
|
|
7
7
|
QueryOne,
|
|
8
8
|
} from '../../types';
|
|
9
|
-
import { monotonicUlidFactory } from '../../util';
|
|
9
|
+
import { indexNameFromKeys, monotonicUlidFactory } from '../../util';
|
|
10
10
|
import { createInMemoryStore } from './InMemoryStore';
|
|
11
11
|
|
|
12
12
|
const DB_NAME = '@AmplifyDatastore';
|
|
@@ -96,8 +96,10 @@ class AsyncStorageDatabase {
|
|
|
96
96
|
keys: string[],
|
|
97
97
|
keyValuesPath: string
|
|
98
98
|
) {
|
|
99
|
+
const idxName = indexNameFromKeys(keys);
|
|
100
|
+
|
|
99
101
|
const ulid =
|
|
100
|
-
this.getCollectionIndex(storeName).get(
|
|
102
|
+
this.getCollectionIndex(storeName).get(idxName) ||
|
|
101
103
|
this.getMonotonicFactory(storeName)();
|
|
102
104
|
|
|
103
105
|
// Retrieve db key for item
|
package/src/util.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { produce, applyPatches, Patch } from 'immer';
|
|
|
5
5
|
import { ModelInstanceCreator } from './datastore/datastore';
|
|
6
6
|
import {
|
|
7
7
|
AllOperators,
|
|
8
|
+
ProducerModelPredicate,
|
|
8
9
|
isPredicateGroup,
|
|
9
10
|
isPredicateObj,
|
|
10
11
|
ModelInstanceMetadata,
|
|
@@ -31,8 +32,13 @@ import {
|
|
|
31
32
|
IndexesType,
|
|
32
33
|
} from './types';
|
|
33
34
|
import { WordArray } from 'amazon-cognito-identity-js';
|
|
35
|
+
|
|
34
36
|
const ID = 'id';
|
|
35
37
|
|
|
38
|
+
export const errorMessages = {
|
|
39
|
+
idEmptyString: 'An index field cannot contain an empty string value',
|
|
40
|
+
};
|
|
41
|
+
|
|
36
42
|
export function extractKeyIfExists(
|
|
37
43
|
modelDefinition: SchemaModel
|
|
38
44
|
): ModelAttribute | undefined {
|
|
@@ -59,6 +65,15 @@ export function extractPrimaryKeyValues<T extends PersistentModel>(
|
|
|
59
65
|
return keyFields.map(key => model[key]);
|
|
60
66
|
}
|
|
61
67
|
|
|
68
|
+
export function extractPrimaryKeysAndValues<T extends PersistentModel>(
|
|
69
|
+
model: T,
|
|
70
|
+
keyFields: string[]
|
|
71
|
+
): any {
|
|
72
|
+
const primaryKeysAndValues = {};
|
|
73
|
+
keyFields.forEach(key => (primaryKeysAndValues[key] = model[key]));
|
|
74
|
+
return primaryKeysAndValues;
|
|
75
|
+
}
|
|
76
|
+
|
|
62
77
|
// IdentifierFields<ManagedIdentifier>
|
|
63
78
|
// Default behavior without explicit @primaryKey defined
|
|
64
79
|
export function isIdManaged(modelDefinition: SchemaModel): boolean {
|