Web development basic and advance tutorial, php basic tutorial, laravel basic tutorial, React Native tutorial

Wednesday, January 18, 2023

react aws amplify all query

0 comments

https://docs.amplify.aws/lib/datastore/data-access/q/platform/js/ 


DATASTORE

Manipulating data

Getting started

To get started, first import the DataStore API:

import { DataStore } from 'aws-amplify';

Create and update

To write data to the DataStore, pass an instance of a model to Amplify.DataStore.save():

1234567
await DataStore.save( new Post({ title: 'My First Post', rating: 10, status: PostStatus.INACTIVE }) );

The save method creates a new record, or in the event that one already exists in the local store, it updates the record.

12345678
async function updatePost(id, newTitle) { const original = await DataStore.query(Post, id); await DataStore.save( Post.copyOf(original, updated => { updated.title = newTitle }) ); }

Models in DataStore are immutable. To update a record you must use the copyOf function to apply updates to the item's fields rather than mutating the instance directly.

Avoid working with stale data!

Model instances which store values, such as those from the results of a DataStore.Query operation, can become stale and outdated when their properties are updated. This can be the result of a manual change or even a side effect of real time data being received by the application. In order to ensure you are performing mutations on the latest committed state to the system, either perform a query directly before the DataStore.save() operation or observe the model to keep the state updated at all times and perform mutations on that latest state referencing the model instance. The preceding example demonstrates one approach. The following example demonstrates the observeQuery approach.




No comments:

Post a Comment