Skip to content

Automatically populating createdAt and updatedAt fields in RDS MySQL when using AWS Amplify with React Native

0

I’m using AWS Amplify for my React Native app. According to the Amplify documentation:

https://docs.amplify.aws/react-native/build-a-backend/data/connect-to-existing-data-sources/connect-postgres-mysql-database/

https://docs.amplify.aws/react-native/build-a-backend/data/mutate-data/

it states:

“When creating new DynamoDB-backed data models via a.model(), a set of implicit fields, such as id, createdAt, and updatedAt are added by default. When connecting to an existing SQL database, these fields are not implicitly added as they are not part of the underlying data source. If createdAt and updatedAt are valid columns in the underlying database table, then Amplify Data will automatically populate those fields with their respective values upon create and update mutations.”

I created a MySQL table in RDS and added createdAt and updatedAt columns (also tried with created_at and updated_at). However, when I run models.TableName.create() from my React Native app, these fields are not automatically populated.

My questions:

Is there any special configuration needed for Amplify Data to automatically populate these fields in an existing MySQL database?

Does Amplify require specific column names (createdAt vs created_at) or data types ( I tried TimeStamp )for this feature to work?

If not, what’s the recommended way to handle automatic timestamps when using Amplify Data with an existing SQL database?

Any guidance would be appreciated!

Thank you.

1 Answer
0

Based on the available information, AWS Amplify's capability to automatically populate timestamp fields like createdAt and updatedAt is primarily designed for DynamoDB-backed data models created with a.model().

When connecting to existing SQL databases like MySQL, Amplify has recently added support for using existing MySQL and PostgreSQL databases for GraphQL APIs through Amplify's Cloud Development Kit (CDK) construct. However, the automatic population of timestamp fields in these existing SQL databases isn't explicitly confirmed in the provided sources.

For your specific case with MySQL in RDS, here are some recommendations:

  1. Column naming: Try using the camelCase format (createdAt, updatedAt) to match Amplify's conventions, as this is the format used in DynamoDB models.

  2. Data types: Use appropriate timestamp data types in MySQL that are compatible with Amplify's expected formats (TIMESTAMP or DATETIME).

  3. Alternative approach: If automatic population doesn't work, you could:

  • Handle timestamps in your application code before sending mutations
  • Create database triggers in MySQL to automatically update these fields
  • Use the extensibility features of Amplify to customize the behavior

Since Amplify is built on AWS CDK, you might be able to extend its functionality to handle these timestamp fields according to your requirements.

If you continue to face issues, you might want to check the latest Amplify documentation or community forums, as this feature might have specific requirements or limitations when working with existing SQL databases that aren't fully detailed in the available information.
Sources
Community | AWS open source newsletter, #183
Building RAG-based applications with AWS Amplify AI Kit and Neon Postgres | Front-End Web & Mobile
AWS Amplify | Extensible Web & Mobile Apps | Amazon Web Services
Full Stack Development - Web and Mobile Apps - AWS Amplify

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.