aws-amplify / amplify-flutter

A declarative library with an easy-to-use interface for building Flutter applications on AWS.
https://docs.amplify.aws
Apache License 2.0
1.31k stars 245 forks source link

Is there a good way to selectively sync based on userID after auth? #4491

Closed osehmathias closed 7 months ago

osehmathias commented 7 months ago

Description

Multiple owners is not supported with Amplify DataStore, so I am trying to find a way to selectively sync based on the userID once a user has logged in.

Are there any good ways to do this?

I am storing the ownerIDs as a Cognito attribute and ideally could use that to selectively sync.

https://github.com/aws-amplify/amplify-flutter/issues/3388

Categories

Steps to Reproduce

No response

Screenshots

No response

Platforms

Flutter Version

3.19.1

Amplify Flutter Version

1.6.2

Deployment Method

Amplify CLI

Schema

type Client
  @model
  @auth(
    rules: [
      { allow: private, provider: iam }
      { allow: owner, ownerField: "owner" }
      { allow: groups, groups: ["participant"] }
      { allow: groups, groups: ["clinician"] }
      { allow: groups, groups: ["practicemanager"] }
      { allow: groups, groups: ["admin"] }
    ]
  ) {
  id: ID!
  sub: ID!
...
}
Equartey commented 7 months ago

Hi @osehmathias, could you please provide more context about your use case?

osehmathias commented 7 months ago

Hi @Equartey - thank you for the response!

The application allows clinicians and clients to interact with the data a client has created. You can also invite participants.

Participants are clients that have access to another client's account. This is used for family members and so forth.

Instead of using the owner field to grant participant access, I am using a group and an attribute on their Cognito object which indicates the clientID the participant has access to. This ID is used for querying.

When a participant is logging in, it's extremely slow because it's syncing the entire DB.

The ideal solution would be to use the owner field with multiple owners. However, this is not available in Amplify Flutter. https://github.com/aws-amplify/amplify-flutter/issues/3388

Another solution could be to use the clientID attribute on the participant's Cognito object to selectively sync. However, it seems like once the Amplify DataStore configuration object has been set when the app first loads, it can't be modified.

I am curious if anyone knows a way to selectively sync with a variable that becomes available when a user has logged in.

Equartey commented 7 months ago

Hi @osehmathias, appreciate the extra context.

If you're looking to change the selective sync during runtime, checkout this guide:

https://docs.amplify.aws/flutter/build-a-backend/more-features/datastore/sync-to-cloud/#reevaluate-expressions-at-runtime

osehmathias commented 7 months ago

I did try that by setting the ID to StoredValues and re-evaluating. Strangely, it didn't filter. However, I will try another method of setting the ID to selectively sync.

Happy to consider this closed.