AWS Amplify JavaScript Library

repository·main·Indexed 27 days ago

https://github.com/aws-amplify/amplify-js

A JavaScript library for frontend and mobile developers to build cloud-enabled applications with a declarative interface for AWS services. Key features include Authentication (Amazon Cognito), Analytics (Amazon Pinpoint), REST and GraphQL APIs (API Gateway, AWS AppSync), DataStore, Storage (Amazon S3), Geo, PubSub, and Push Notifications. Includes support for React Native and a dedicated adapter for Next.js.

Tokens
47.4K
Snippets
79
Records
576
Agent score
91%

What's inside Amplify JS

  1. Overview of AWS Amplify JavaScript Library

    main

    AWS Amplify is a JavaScript library designed for frontend and mobile developers (including React Native) to build cloud-enabled applications. It provides a declarative interface for various cloud operations, primarily using Amazon Web Services (AWS) providers, though it is designed to be pluggable for custom backends.

    Key features include:

    • Authentication: Using Amazon Cognito.
    • Analytics: Using Amazon Pinpoint.
    • REST API: Using Amazon API Gateway (supports Sigv4 signing).
    • GraphQL API: Using AWS AppSync.
    • DataStore: A programming model for shared/distributed data with online/offline synchronization via AWS AppSync.
    • Storage: Managing content in Amazon S3 buckets.
    • Geo (Developer preview): Maps and location search via Amazon Location Service.
    • Push Notifications: Integration via Amazon Pinpoint.
    • Interactions: Conversational bots via Amazon Lex.
    • PubSub: Cloud-based messaging via AWS IoT.
    • Internationalization: Lightweight i18n solution.
    • Cache: Generic LRU cache with priority and expiration.
    • Predictions: Machine learning connectivity (NLP, computer vision, etc.) using services like Amazon Comprehend, Polly, Rekognition, Textract, and Translate.
  2. Supported DataStore Storage Adapters

    main

    DataStore uses different storage adapters depending on the environment (Web, Mobile, or SSR).

    • Web: Uses IndexedDB.
    • Mobile (React Native): Uses Async Storage (with an option to use SQLite).
    • SSR (Server-Side Rendering): Uses InMemoryStore.

    Note: The SQLite adapter is maintained in its own dedicated package: datastore-storage-adapter.

  3. Access Amplify JS v6 API Reference

    main
    The Amplify JS v6 API reference guide provides detailed documentation for exported functions, classes, and type definitions. The documentation is organized by the specific packages and export paths exposed by the library. For internal or reference types, look within the <Reference Types> directories.
  4. Understand DataStore Sync Types

    main

    DataStore uses two primary synchronization methods to keep local data in sync with AppSync:

    • Full Sync: Performed the first time DataStore is started on a new client. It fetches all records from AppSync.
    • Delta Sync: Performed during subsequent app reloads (browser refreshes or new sessions). It fetches only the records that have changed after a specific timestamp.

    Note on Sync Frequency: If the time elapsed since the last full sync exceeds the fullSyncInterval (which is configurable), DataStore will trigger a Full Sync instead of a Delta Sync.

  5. Understand DataStore local database namespaces

    main

    The DataStore local database is organized into several namespaces that categorize the data stored on the device:

    • datastore: Contains configuration and settings, such as schema versioning.
    • user: Contains the actual application data records derived from your user-defined schema.
    • sync: Contains internal metadata used by the Sync Engine to manage synchronization state.
    • storage: (Deprecated)

    Note: Any table name prepended with sync_ is an internal table used by the engine.

  6. How the DataStore Merger maintains consistency

    main

    The Merger class ensures data consistency by preventing incoming remote changes from overwriting pending local changes.

    Behavior:

    • When a change arrives via a mutation response or an AppSync subscription, the Merger checks the local Outbox (mutation queue) for that specific record ID.
    • If a pending mutation exists for that record: The incoming remote change is discarded and not saved to the local database. This prevents the client from temporarily undoing its own local changes.
    • If no pending mutation exists: The incoming model is persisted to the local store via Storage.save.

    Important Requirement: For the merge logic to function correctly, AppSync must be configured to send only updated fields in mutation responses.

  7. Upgrade guidance for AWS Amplify versions

    main

    AWS Amplify has undergone significant version changes.

    • v6.x.x: The current major version. If you are upgrading from v5, refer to the official migration guide.
    • v5.x.x: Actively supported.
    • v4.x.x and below: Deprecated. These versions entered Maintenance Mode and will end all support on April 13, 2026. During Maintenance Mode, they only receive critical bug fixes and security updates. It is strongly recommended to upgrade to v6 as soon as possible.
  8. Update Storage.list and Storage.put in Amplify 5.x.x

    main

    Several Storage methods changed in version 5.x.x:

    Storage.list

    The maxKeys parameter was renamed to pageSize, and the return type now contains a results list.

    - const photos = await Storage.list('photos/', { maxKeys: 100 });
    - const { key } = photos[0];
    + const photos = await Storage.list('photos/', { pageSize: 100 });
    + const { key } = photos.results[0];

    Storage.put (Resumable)

    When resumable: true is used, the returned key no longer includes the bucket name.

    - let uploadedObjectKey;
    - Storage.put(file.name, file, {
    -   resumable: true,
    -   // Necessary to parse the bucket name out to work with the key
    -   completeCallback: (obj) => uploadedObjectKey = obj.key.substring( obj.key.indexOf("/") + 1 )
    - }
    + let uploadedObjectKey;
    + Storage.put(file.name, file, {
    +   resumable: true,
    +   completeCallback: (obj) => uploadedObjectKey = obj.key
    + }
    - const photos = await Storage.list('photos/', { maxKeys: 100 });
    - const { key } = photos[0];
    
    + const photos = await Storage.list('photos/', { pageSize: 100 });
    + const { key } = photos.results[0];