Why does the DataStore.observeQuery function not trigger on a DB INSERT event

160 Views Asked by At

AWS Amplify DataStore.observeQuery will not trigger on n INSERT event if i add createdAt.between into the query filter.

Using aws-amplify v5.3.3

This is my code:

const subscription = DataStore.observeQuery(Order, (order) =>
  order.and((order) => [

    order.createdAt.between(start, end), // observer now does not get called when i add new data to the dynamoDB

    order.orderRestaurantId.eq(restaurant?.id),
    order.or((o) => [
      o.status.eq(OrderStatus.ACCEPTED),
      o.status.eq(OrderStatus.COOKING),
      o.status.eq(OrderStatus.NEW),
      o.status.eq(OrderStatus.READY_FOR_PICKUP),
    ]),
  ])
).subscribe((snapshot) => {
  const { items, isSynced } = snapshot;
  console.log(
    `[Snapshot] Orders fetched: ${items.length}, isSynced: ${isSynced}`
  );
})

If i remove the createdAt.between filter then INSERT events do not trigger the observer. But UPDATE events will trigger the observer, without the newly INSERTED data

After i refresh the page it fetches all data also the newly created data.

Solution:

My bad!!! The issue appears to be that i was setting the upper bound (e.g. end) of my filter to the time that the subscription was created, meaning that any items that were created after the subscription has been instantiated will not fulfill the criteria of my filter. After updating the upper bound, the filter works as expected.

For my case i use 30 days as upper bound now, guess that should be fine ...

const start = moment().utc().subtract(7, "day").toISOString(); const end = moment().utc().add("30", "day").toISOString();

0

There are 0 best solutions below