amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

Custom primary key support

Open dnys1 opened this issue 3 years ago • 9 comments

Description

This is to track support of custom primary keys in model gen and model mutations.

  • [ ] Model gen
  • [ ] Model mutations

Categories

  • [ ] Analytics
  • [X] API
  • [ ] Auth
  • [X] DataStore
  • [ ] Storage

Steps to Reproduce

No response

Screenshots

No response

Platforms

  • [ ] iOS
  • [ ] Android

Environment

N/A

Dependencies

N/A

Device

N/A

OS

N/A

CLI Version

N/A

Additional Context

No response

dnys1 avatar Mar 03 '22 15:03 dnys1

Use case for using Custom PK.

We have a BLE device that has its own identifier which cannot be changed for security reasons. This along with some other information is sent to a Lambda function which then searches for the device "key" in DynamoDB and updates via AppSync.

I did not want to have to send the generated "ID" as this means id have to then store it on flash (Limited space) and add it to the network packet which uses 3g/4g increasing our overhead.

I believe AppSync requires the primary key field for updates hence me needing a custom one.

This is also how the subscriptions for updates work by key. I am not sure if I could have done the same via ID, Correct me if I am wrong.

Thinking on it I could have searched the DB for the ID by finding the key field and then updated... It just felt better to make a custom primary key.

tr-asimpson avatar Mar 31 '22 00:03 tr-asimpson

We have been confronted with almost the exact same use case as @tr-asimpson . We are working with BLE devices and have natural keys currently defined in DynamoDB. We really want to use AppSync, but if we do, we'll have to apply costly workarounds, and will impact our customer experience. Is it possible to share where this enhancement is on the roadmap or an ETA so that we can appropriately plan?

Thanks!

h2ouw8n4 avatar May 16 '22 12:05 h2ouw8n4

+1

veeramneni avatar May 20 '22 14:05 veeramneni

Even we are facing this issue. It would be helpful if this is implemented soon.

AmanMasipeddi avatar May 20 '22 14:05 AmanMasipeddi

Hi there

I can't get a unique record from dynamodb through AppSync using: Amplify.API.query(request: ModelQueries.get(Model.classType, 123, "abc")) As the parameter ID takes a single String.

Also, since I can't use two owner type auth rules (https://github.com/aws-amplify/amplify-ios/issues/1590, https://github.com/aws-amplify/amplify-cli/issues/10110) I need a way to retrieve data from other users. Is there a way to automatically sync down specific records from a model, anyone?

EDIT: Nevermind, I realized I can just replace ModelQueries.get with a GraphQL query...

Simon-PumpAndUp avatar Jun 17 '22 08:06 Simon-PumpAndUp

Hey there, what's the ETA on resolving this issue? It looks like a lot of work has already been completed and is merged. We just started prototyping a flutter mobile app client for our existing amplify web application that uses custom PKs and ran into this issue. Currently we're considering two options:

  1. A rather expensive and risky schema migration away from custom PKs, as the web app is already in production state
  2. Switching to another mobile app framework, e.g. React Native, where we hope amplify supports custom PKs

hanna-becker avatar Aug 08 '22 09:08 hanna-becker

Hello @hanna-becker sorry for the delayed response!

You are right! This feature is almost ready (library support has been released with version 0.6.6). We are currently working on some final tunings cross Amplify platforms. We expect this feature to be live very soon, thank you for your patience!

HuiSF avatar Aug 19 '22 17:08 HuiSF

Nice! We're very excited about this.

hanna-becker avatar Aug 19 '22 18:08 hanna-becker

Hi do you have any updates about this? Is it already possible to set a custom primary key with amplify?

8maxxam8 avatar Sep 19 '22 15:09 8maxxam8

Hi @hanna-becker @8maxxam8 with the release of @aws-amplify/cli 10.5, custom primary key feature becomes generally available. You can give it a try :)

For an existing project, you may need to manually flip a feature flag in order to enable custom primary key functionality with Amplify CLI and codegen.

Set respectPrimaryKeyAttributesOnConnectionField to true in your amplify/cli.json file:

{
  "features": {
    "graphqltransformer": {
      "respectPrimaryKeyAttributesOnConnectionField": true,
    }
  }
}

Then run amplify codegen models (please make sure you've installed Amplify CLI 10.5).

In addition, currently custom primary key feature is only available for DataStore plugin, model generated with a custom primary key will not work with API plugin GraphQL helper.

HuiSF avatar Nov 28 '22 22:11 HuiSF

"...custom primary key feature is only available for DataStore plugin, model generated with a custom primary key will not work with API plugin GraphQL helper."

@HuiSF, to clarify where CPK support is available, would it be possible to have something like

CPK Support Stable ver. Dev Preview ver.
GraphQL API ? ?
DataStore ? ?

vgribok avatar Jan 04 '23 18:01 vgribok

Hi @vgribok thanks for asking the clarification.

Current state:

CPK Support Stable ver. Dev Preview ver.
GraphQL API
DataStore

For the API plugin the GraphQL helper APIs need to be update to support ModelIdentifier which allows composite key instead of hardcoded id.

HuiSF avatar Jan 04 '23 18:01 HuiSF

Just noting PR merged for GraphQL API support here https://github.com/aws-amplify/amplify-flutter/pull/2606, should go out in next dev preview version.

ragingsquirrel3 avatar Feb 22 '23 21:02 ragingsquirrel3

GraphQL API model helper support for custom primary key was just released in dev preview with amplify_api 1.0.0-next.6. There are some examples of usage on https://docs.amplify.aws/lib/graphqlapi/query-data/q/platform/flutter/#query-item. We will leave this issue open until there is a stable release for this.

ragingsquirrel3 avatar Mar 07 '23 20:03 ragingsquirrel3

This issue has been addressed in v1.0.0 of Amplify Flutter, which is now stable. See the comment above for a link to the docs.

This release also includes web and desktop support for Auth, API, Analytics, and Storage. You can see the list of new features and bug fixes in the release notes, and see more details on how to migrate in the upgrade guide.

Jordan-Nelson avatar Apr 17 '23 19:04 Jordan-Nelson