diff --git a/packages/datastore/src/storage/storage.ts b/packages/datastore/src/storage/storage.ts index fa33c9112db..35156158d7e 100644 --- a/packages/datastore/src/storage/storage.ts +++ b/packages/datastore/src/storage/storage.ts @@ -25,6 +25,7 @@ import { validatePredicate, valuesEqual, } from '../util'; +import { getIdentifierValue } from '../sync/utils'; import { Adapter } from './adapter'; import getDefaultAdapter from './adapter/getDefaultAdapter'; @@ -175,7 +176,21 @@ class StorageClass implements StorageFacade { condition ); - const modelIds = new Set(models.map(({ id }) => id)); + const modelConstructor = isModelConstructor(modelOrModelConstructor) + ? modelOrModelConstructor + : (Object.getPrototypeOf(modelOrModelConstructor || {}) + .constructor as PersistentModelConstructor); + const namespaceName = this.namespaceResolver(modelConstructor); + + const modelDefinition = + this.schema.namespaces[namespaceName].models[modelConstructor.name]; + + const modelIds = new Set( + models.map(model => { + const modelId = getIdentifierValue(modelDefinition, model); + return modelId; + }) + ); if ( !isModelConstructor(modelOrModelConstructor) && @@ -191,7 +206,8 @@ class StorageClass implements StorageFacade { let theCondition: PredicatesGroup; if (!isModelConstructor(modelOrModelConstructor)) { - theCondition = modelIds.has(model.id) + const modelId = getIdentifierValue(modelDefinition, model); + theCondition = modelIds.has(modelId) ? ModelPredicateCreator.getPredicates(condition, false) : undefined; } diff --git a/packages/datastore/src/sync/index.ts b/packages/datastore/src/sync/index.ts index 9cbc1555b46..ffadb9c278d 100644 --- a/packages/datastore/src/sync/index.ts +++ b/packages/datastore/src/sync/index.ts @@ -356,7 +356,6 @@ export class SyncEngine { .observe(null, null, ownSymbol) .filter(({ model }) => { const modelDefinition = this.getModelDefinition(model); - return modelDefinition.syncable === true; }) .subscribe({ @@ -366,7 +365,11 @@ export class SyncEngine { const MutationEventConstructor = this.modelClasses[ 'MutationEvent' ] as PersistentModelConstructor; - const graphQLCondition = predicateToGraphQLCondition(condition); + const modelDefinition = this.getModelDefinition(model); + const graphQLCondition = predicateToGraphQLCondition( + condition, + modelDefinition + ); const mutationEvent = createMutationInstanceFromModelOperation( namespace.relationships, this.getModelDefinition(model), diff --git a/packages/datastore/src/sync/utils.ts b/packages/datastore/src/sync/utils.ts index 95b56ce4594..c4f53893c23 100644 --- a/packages/datastore/src/sync/utils.ts +++ b/packages/datastore/src/sync/utils.ts @@ -27,7 +27,6 @@ import { InternalSchema, AuthModeStrategy, extractPrimaryKeyFieldNames, - // isPrimaryKeyId, } from '../types'; import { exhaustiveCheck } from '../util'; import { MutationEvent } from './'; @@ -430,7 +429,8 @@ export function createMutationInstanceFromModelOperation< } export function predicateToGraphQLCondition( - predicate: PredicatesGroup + predicate: PredicatesGroup, + modelDefinition: SchemaModel ): GraphQLCondition { const result = {}; @@ -438,17 +438,27 @@ export function predicateToGraphQLCondition( return result; } + const keyFields = extractPrimaryKeyFieldNames(modelDefinition); + predicate.predicates.forEach(p => { if (isPredicateObj(p)) { const { field, operator, operand } = p; - if (field === 'id') { + // This is compatible with how the GQL Transform currently generates the Condition Input, + // i.e. any PK and SK fields are omitted and can't be used as conditions. + // However, I think this limits usability. + // What if we want to delete all records where SK > some value + // Or all records where PK = some value but SKs are different values + + // TODO: if the Transform gets updated ^ we'll need to modify this logic to only omit + // key fields from the predicate when ALL of the keyFields are present and using `eq` operators + if (typeof field === 'string' && keyFields.includes(field)) { return; } result[field] = { [operator]: operand }; } else { - result[p.type] = predicateToGraphQLCondition(p); + result[p.type] = predicateToGraphQLCondition(p, modelDefinition); } });