aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.43k stars 2.13k forks source link

How to use static method of `ModelPredicateCreator` #5157

Closed waltermvp closed 4 years ago

waltermvp commented 4 years ago

Which Category is your question related to? Datastore, API

What AWS Services are you utilizing? AppSync, DynamoDB

Provide additional details e.g. code snippets

When attempting to use static method createForId of ModelPredicateCreator in datastore Predicates I get the following error:

Attempted import error: 'ModelPredicateCreator' is not exported from '@aws-amplify/datastore'.

This is how I tried to call the createForId constructor:

    const predicate = ModelPredicateCreator.createForId(EventModel, slug);

You can see the full class here

import { ModelPredicate, PersistentModel, PredicatesGroup, ProducerModelPredicate, SchemaModel } from '../types';
export declare function isPredicatesAll(predicate: ProducerModelPredicate<any>): boolean;
export declare class Predicates {
    static readonly ALL: ProducerModelPredicate<any>;
}
export declare class ModelPredicateCreator {
    private static predicateGroupsMap;
    private static createPredicateBuilder;
    static isValidPredicate<T extends PersistentModel>(predicate: any): predicate is ModelPredicate<T>;
    static getPredicates<T extends PersistentModel>(predicate: ModelPredicate<T>, throwOnInvalid?: boolean): PredicatesGroup<any>;
    static createFromExisting<T extends PersistentModel>(modelDefinition: SchemaModel, existing: ProducerModelPredicate<T>): ModelPredicate<T>;
    static createForId<T extends PersistentModel>(modelDefinition: SchemaModel, id: string): ModelPredicate<T>;
}
manueliglesias commented 4 years ago

Hi @waltermvp

You can create a predicate for the id field as a function and pass it to the DataStore instance

e.g.

const predicate = c => c.id('eq', '1bc3907d-7a2b-4703-8683-fef0cbc6a7f1');

const modelInstance = DataStore.query(Model, predicate);
waltermvp commented 4 years ago

@manueliglesias thank you

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.