-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique Constraints #286
Comments
Does anyone know if this is currently being developed by the amplify team? |
I've only started out with Amplify, but it seems like a really basic requirement. There are plenty of times you would need to make sure a field(s) is unique. Having a directive like @unique seems to make sense. |
This is the best I could find specific to what you're asking for. https://medium.com/@fullstackpho/ensuring-usernames-are-unique-in-your-aws-amplify-app-6fff963274 |
A real shame this hasn't been picked up, the medium link posted above works by running a Lambda that validates the data before (or just after?) it is inserted into the database. Uniqueness should be handled by the data store so it is guaranteed to be enforced. |
@WillDent I tried similar methods but the method I suggested works better. I have modified my VTL template to use the method and it works perfectly (and is not that diffficult to do). It just takes a long time to do for every template and field combination when we could easily add directive to the schema that changes the vtl template generation slightly |
+1 |
Even key field does not ensure uniqueness. I have following and duplicate email accepted even though I have indicated that as key. Worse part is that the backend corresponding dynamo table does not seem to take the duplicate record. So now, my client side sqlite is out of sync with dynamoDB. So I have to write code to check if the record exists..Sounds ugly unless someone can enlighten me please.. type AddUserToT |
+1 |
7 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
i notice this issue has the |
+1 |
did the behavior of according to the dynamodb docs on comparison operators and functions, @jcbdev do you know if your technique of maintaining a |
This seems like major missing feature IMO. For future readers: username: String! @primaryKey You can also do something like this (e.g. Discord usernames Bob#0001): discordUsername: String! @primaryKey(sortKeyFields: ["discordNumber"])
discordNumber: Int! |
@acusti It did when I was using it but yes i think its because the column is the primary key. The trick was to create a uniqueconstraints table where all the unique constraints are written to and not to write the unique constraints to the same table your data is in (the dynamodb put requests can go to different tables in the transaction). But I do not use amplify anymore. I rewrote the platform on cdk and have never looked back. I found aws amplify terrible at scaling and just couldn't get to work on any project that wasn't a toy project. Too many limitations, compromises and assumptions that I spent more time maintaining and working around amplify in the project than developing new features. |
Hi all. Just stumbled across this and think I might have found the answer. If you look at the generated schemas in AppSync, you'll see the schemas for the condition expression objects as well. For example, the schema to create a condition on a
The first item in that schema
and the input variables would look like:
If an item already exists with the name |
@lewisdonovan , isn't the condition specified on the client side? In other words, a client can cheat your system by removing that part of the request. |
Yes, absolutely. But AppSync can be run on the back-end too. You could create a custom mutation, pass |
@lewisdonovan I'm not sure the condition method works, just given it a spin and it still lets me create/update the user. |
Is your feature request related to a problem? Please describe.
I have lots of scenarios where I require unique values on non-primary key fields. Especially for public apps where you might want to not allow two people with the same phone number or email address and so forth
Describe the solution you'd like
It would be possible to add a directive such as
@unique
to a field or@unique(fields: ["email", "phone"])
to the typeFirst of all this would cause resources to be added:
when this is added you can then change the VTL generated to do the following:
** another "Put" item is added the Items but for TableName - "UniqueConstraints-APIID-env" and the id(pk) of "table#field#value"
** attribute_not_exist(id) is added the condition for this item
This would then only succeed if the field is successful. You would also need the update and delete templates to follow the equivalent logic
If the "value" is a complex type you could potentially use a hash of the value
Describe alternatives you've considered
I have used custom lambda resolvers and custom resolvers to implement this myself but it tedious for something I have to do over and over again
The text was updated successfully, but these errors were encountered: