Skip to content
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

DataStore doesn't trigger update on item updated in a lambda trigger #14247

Open
3 tasks done
rossicler-hostalky opened this issue Feb 25, 2025 · 1 comment
Open
3 tasks done
Labels
DataStore Related to DataStore category pending-maintainer-response Issue is pending a response from the Amplify team. pending-triage Issue is pending triage

Comments

@rossicler-hostalky
Copy link

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

GraphQL API

Amplify Version

v6

Amplify Categories

api, function

Backend

Amplify CLI

Environment information

# Put output below this line

System:
    OS: macOS 15.3.1
    CPU: (8) arm64 Apple M1
    Memory: 111.84 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.20.4 - ~/.nvm/versions/node/v18.20.4/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v18.20.4/bin/yarn
    npm: 10.7.0 - ~/.nvm/versions/node/v18.20.4/bin/npm
    Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman
  Browsers:
    Safari: 18.3
  npmPackages:
    @aws-amplify/datastore-storage-adapter: 2.0.40 => 2.0.40 
    @aws-amplify/react-native: ^1.1.7 => 1.1.7 
    @azure/core-asynciterator-polyfill: ^1.0.2 => 1.0.2 
    @babel/core: ^7.25.2 => 7.26.9 
    @expo/vector-icons: ^14.0.2 => 14.0.4 
    @react-native-async-storage/async-storage: 1.23.1 => 1.23.1 
    @react-native-community/netinfo: 11.4.1 => 11.4.1 
    @react-navigation/bottom-tabs: ^7.2.0 => 7.2.0 
    @react-navigation/native: ^7.0.14 => 7.0.14 
    @types/jest: ^29.5.12 => 29.5.14 
    @types/lodash: ^4.17.15 => 4.17.15 
    @types/react: ~18.3.12 => 18.3.18 
    @types/react-test-renderer: ^18.3.0 => 18.3.1 
    aws-amplify: ^6.12.3 => 6.13.1 
    aws-amplify/adapter-core:  undefined ()
    aws-amplify/adapter-core/internals:  undefined ()
    aws-amplify/analytics:  undefined ()
    aws-amplify/analytics/kinesis:  undefined ()
    aws-amplify/analytics/kinesis-firehose:  undefined ()
    aws-amplify/analytics/personalize:  undefined ()
    aws-amplify/analytics/pinpoint:  undefined ()
    aws-amplify/api:  undefined ()
    aws-amplify/api/internals:  undefined ()
    aws-amplify/api/server:  undefined ()
    aws-amplify/auth:  undefined ()
    aws-amplify/auth/cognito:  undefined ()
    aws-amplify/auth/cognito/server:  undefined ()
    aws-amplify/auth/enable-oauth-listener:  undefined ()
    aws-amplify/auth/server:  undefined ()
    aws-amplify/data:  undefined ()
    aws-amplify/data/server:  undefined ()
    aws-amplify/datastore:  undefined ()
    aws-amplify/in-app-messaging:  undefined ()
    aws-amplify/in-app-messaging/pinpoint:  undefined ()
    aws-amplify/push-notifications:  undefined ()
    aws-amplify/push-notifications/pinpoint:  undefined ()
    aws-amplify/storage:  undefined ()
    aws-amplify/storage/s3:  undefined ()
    aws-amplify/storage/s3/server:  undefined ()
    aws-amplify/storage/server:  undefined ()
    aws-amplify/utils:  undefined ()
    expo: ~52.0.28 => 52.0.36 
    expo-blur: ~14.0.3 => 14.0.3 
    expo-constants: ~17.0.5 => 17.0.7 
    expo-font: ~13.0.3 => 13.0.4 
    expo-haptics: ~14.0.1 => 14.0.1 
    expo-linking: ~7.0.5 => 7.0.5 
    expo-router: ~4.0.17 => 4.0.17 
    expo-splash-screen: ~0.29.21 => 0.29.22 
    expo-sqlite: ~15.1.2 => 15.1.2 
    expo-status-bar: ~2.0.1 => 2.0.1 
    expo-symbols: ~0.2.1 => 0.2.2 
    expo-system-ui: ~4.0.7 => 4.0.8 
    expo-web-browser: ~14.0.2 => 14.0.2 
    jest: ^29.2.1 => 29.7.0 
    jest-expo: ~52.0.3 => 52.0.4 
    lodash: ^4.17.21 => 4.17.21 
    nativewind: ^4.1.23 => 4.1.23 
    react: 18.3.1 => 18.3.1 
    react-dom: 18.3.1 => 18.3.1 
    react-native: 0.76.6 => 0.76.6 
    react-native-gesture-handler: ~2.20.2 => 2.20.2 
    react-native-get-random-values: ~1.11.0 => 1.11.0 
    react-native-reanimated: ~3.16.1 => 3.16.7 
    react-native-safe-area-context: 4.12.0 => 4.12.0 
    react-native-screens: ~4.4.0 => 4.4.0 
    react-native-sqlite-storage: ^6.0.1 => 6.0.1 
    react-native-web: ~0.19.13 => 0.19.13 
    react-native-webview: 13.12.5 => 13.12.5 
    react-test-renderer: 18.3.1 => 18.3.1 
    tailwindcss: ^3.4.17 => 3.4.17 
    typescript: ^5.3.3 => 5.7.3 
  npmGlobalPackages:
    @aws-amplify/cli: 12.14.1
    atlas-app-services-cli: 1.6.1
    corepack: 0.28.0
    eas-cli: 14.4.1
    ios-deploy: 1.12.2
    npm: 10.7.0
    pod-install: 0.3.4
    prettier: 3.3.3
    yarn: 1.22.22

Describe the bug

I have a DynamoDB trigger that updates an item, but DataStore is not properly triggering the update on the item.

I'm updating an attribute value along with _lastChangedAt, updatedAt and _version, and even tho I have n observer setup, it doesn't trigger. This is what I have for the observer:

const subscription = DataStore.observe<T>(
  modelConstructor,
  identifier
).subscribe((msg) => {
  console.log("Received a message", msg);
});

If I restart the app, after a second or so DataStore updates correctly.

Follow-up question: Is manually updating these DataStore metadata (_lastChangedAt, etc) recommended? If not, what is the right way?

Expected behavior

Print the log "Received a message " whenever the trigger updates the item on DynamoDB.

Reproduction steps

  1. Initiate the observer on the client
  2. Run a DynamoDB trigger or function that updates an item on DynamoDB.
  3. Expect observer to get the updated item.

Code Snippet

// Put your code below this line.

Log output

// Put your logs below this line


aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

@github-actions github-actions bot added pending-triage Issue is pending triage pending-maintainer-response Issue is pending a response from the Amplify team. labels Feb 25, 2025
@joon-won joon-won added the DataStore Related to DataStore category label Feb 26, 2025
@rossicler-hostalky
Copy link
Author

Someone in the discord shared this documentation to help with my issue. But even after changing my function from updating directly in the database to using AppSync mutation to update, neither DataStore or AppSync Subscription is being triggered.

This is my client-side setup for testing:

const subTest = DataStore.observeQuery<T>(modelConstructor).subscribe(
  (snapshot) => console.log("[Test Object ObserverQuery]", snapshot)
);

const client = generateClient();
const subTest2 = client
  .graphql({ query: subcriptions.onUpdateRoom })
  .subscribe({
    next: (msg) => console.log("AppSync Subscription", msg),
    error: (err) => console.error("AppSync Subscription Error", err),
  });

My function looks like this:

...

const getRoomQuery = /* GraphQL */ `
  query GetRoom($id: ID!) {
    getRoom(id: $id) {
      id
      _version
    }
  }
`;

const updateRoomMutation = /* GraphQL */ `
  mutation UPDATE_ROOM($input: UpdateRoomInput!) {
    updateRoom(input: $input) {
      id
      lastMessageId
    }
  }
`;

const getRoomVersion = async (id) => {
  const res = await runGraphQLRequest(GRAPHQL_ENDPOINT, {
    query: getRoomQuery,
    variables: { id },
  });
  return res.data?.getRoom?._version;
};

/**
 * @type {import('@types/aws-lambda').APIGatewayProxyHandler}
 */
exports.handler = async (event) => {
  console.log(`EVENT: ${JSON.stringify(event)}`);
  for (const record of event.Records) {
    if (record.eventName !== "INSERT") continue;
    const doc = unmarshall(record.dynamodb.NewImage);

    const version = await getRoomVersion(doc.roomId);
    const variables = {
      input: {
        id: doc.roomId,
        lastMessageId: doc.id,
        _version: version,
      },
    };

    try {
      const res = await runGraphQLRequest(GRAPHQL_ENDPOINT, {
        query: updateRoomMutation,
        variables,
      });
      if (res.errors) {
        console.error("Errors: ", res.errors);
      } else {
        console.log("Success: ", JSON.stringify(res));
      }
    } catch (error) {
      console.error("Error:", error);
    }
  }
  return "Successfully processed DynamoDB record";
};

The function is running fine, I can see in DynamoDB that the value is updated. I can also see the Room updated if I restart the app (which forces DataStore to resync)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DataStore Related to DataStore category pending-maintainer-response Issue is pending a response from the Amplify team. pending-triage Issue is pending triage
Projects
None yet
Development

No branches or pull requests

2 participants