import js0 from "/src/fragments/lib/datastore/js/data-access/importing-datastore-snippet.mdx"; import reactnative0 from "/src/fragments/lib/datastore/js/data-access/importing-datastore-snippet.mdx"; ## Create and update To write data to the DataStore, pass an instance of a model to `Amplify.DataStore.save()`: import js1 from "/src/fragments/lib/datastore/js/data-access/save-snippet.mdx"; import reactnative1 from "/src/fragments/lib/datastore/js/data-access/save-snippet.mdx"; import ios2 from "/src/fragments/lib/datastore/ios/data-access/save-snippet.mdx"; import android3 from "/src/fragments/lib/datastore/android/data-access/save-snippet.mdx"; import flutter4 from "/src/fragments/lib/datastore/flutter/data-access/save-snippet.mdx"; The `save` method creates a new record, or in the event that one already exists in the local store, it updates the record. import js5 from "/src/fragments/lib/datastore/js/data-access/update-snippet.mdx"; import ios6 from "/src/fragments/lib/datastore/ios/data-access/update-snippet.mdx"; import android7 from "/src/fragments/lib/datastore/android/data-access/update-snippet.mdx"; import flutter8 from "/src/fragments/lib/datastore/flutter/data-access/update-snippet.mdx"; **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](/lib/datastore/sync#distributed-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. import js_observe_update from "/src/fragments/lib/datastore/js/data-access/observe-update-snippet.mdx"; import reactnative2 from "/src/fragments/lib/datastore/react-native/data-access/observe-update-snippet.mdx"; import ios_observe_update from "/src/fragments/lib/datastore/ios/data-access/observe-update-snippet.mdx"; import android_observe_update from "/src/fragments/lib/datastore/android/data-access/observe-update-snippet.mdx"; import flutter_observe_update from "/src/fragments/lib/datastore/flutter/data-access/observe-update-snippet.mdx"; ## Delete To delete an item, simply pass in an instance. import js9 from "/src/fragments/lib/datastore/js/data-access/delete-snippet.mdx"; import reactnative3 from "/src/fragments/lib/datastore/js/data-access/delete-snippet.mdx"; import ios10 from "/src/fragments/lib/datastore/ios/data-access/delete-snippet.mdx"; import android11 from "/src/fragments/lib/datastore/android/data-access/delete-snippet.mdx"; import flutter12 from "/src/fragments/lib/datastore/flutter/data-access/delete-snippet.mdx"; ## Query Data Queries are performed against the _local store_. When cloud synchronization is enabled, the local store is updated in the background by the DataStore Sync Engine. For more advanced filtering, such as matching arbitrary field values on an object, you can supply a query predicate. import js13 from "/src/fragments/lib/datastore/js/data-access/query-basic-snippet.mdx"; import reactnative4 from "/src/fragments/lib/datastore/js/data-access/query-basic-snippet.mdx"; import ios14 from "/src/fragments/lib/datastore/ios/data-access/query-basic-snippet.mdx"; import android15 from "/src/fragments/lib/datastore/android/data-access/query-basic-snippet.mdx"; import flutter16 from "/src/fragments/lib/datastore/flutter/data-access/query-basic-snippet.mdx"; import js17 from "/src/fragments/lib/datastore/js/data-access/query-single-item-snippet.mdx"; ### Predicates Predicates are filters that can be used to match items in the DataStore. When applied to a query(), they constrain the returned results. When applied to a save(), they act as a pre-requisite for updating the data. You can match against fields in your schema by using the following predicates: import jsPredicates from "/src/fragments/lib/datastore/js/data-access/query-predicate-types.mdx"; import iOSPredicates from "/src/fragments/lib/datastore/ios/data-access/query-predicate-types.mdx"; import androidPredicates from "/src/fragments/lib/datastore/android/data-access/query-predicate-types.mdx"; import flutterPredicates from "/src/fragments/lib/datastore/flutter/data-access/query-predicate-types.mdx"; For example if you wanted a list of all `Post` Models that have a `rating` greater than 4: import js18 from "/src/fragments/lib/datastore/js/data-access/query-predicate-snippet.mdx"; import reactnative5 from "/src/fragments/lib/datastore/js/data-access/query-predicate-snippet.mdx"; import ios19 from "/src/fragments/lib/datastore/ios/data-access/query-predicate-snippet.mdx"; import android20 from "/src/fragments/lib/datastore/android/data-access/query-predicate-snippet.mdx"; import flutter21 from "/src/fragments/lib/datastore/flutter/data-access/query-predicate-snippet.mdx"; Multiple conditions can also be used, like the ones defined in [GraphQL Transform condition statements](/cli/graphql/data-modeling). For example, fetch all posts that have a rating greater than `4` and are `ACTIVE`: import js22 from "/src/fragments/lib/datastore/js/data-access/query-predicate-multiple-snippet.mdx"; import reactnative6 from "/src/fragments/lib/datastore/js/data-access/query-predicate-multiple-snippet.mdx"; import ios23 from "/src/fragments/lib/datastore/ios/data-access/query-predicate-multiple-snippet.mdx"; import android24 from "/src/fragments/lib/datastore/android/data-access/query-predicate-multiple-snippet.mdx"; import flutter25 from "/src/fragments/lib/datastore/flutter/data-access/query-predicate-multiple-snippet.mdx"; Alternatively, the `or` logical operator can also be used: import js26 from "/src/fragments/lib/datastore/js/data-access/query-predicate-or-snippet.mdx"; import reactnative7 from "/src/fragments/lib/datastore/js/data-access/query-predicate-or-snippet.mdx"; import ios27 from "/src/fragments/lib/datastore/ios/data-access/query-predicate-or-snippet.mdx"; import android28 from "/src/fragments/lib/datastore/android/data-access/query-predicate-or-snippet.mdx"; import flutter29 from "/src/fragments/lib/datastore/flutter/data-access/query-predicate-or-snippet.mdx"; import js30 from "/src/fragments/lib/datastore/native_common/sort.mdx"; import ios31 from "/src/fragments/lib/datastore/native_common/sort.mdx"; import android32 from "/src/fragments/lib/datastore/native_common/sort.mdx"; import flutter33 from "/src/fragments/lib/datastore/native_common/sort.mdx"; ### Pagination Query results can also be paginated by passing in a `page` number (starting at 0) and an optional `limit` (defaults to 100). This will return a list of the first 100 items: import js34 from "/src/fragments/lib/datastore/js/data-access/query-pagination-snippet.mdx"; import reactnative8 from "/src/fragments/lib/datastore/js/data-access/query-pagination-snippet.mdx"; import ios35 from "/src/fragments/lib/datastore/ios/data-access/query-pagination-snippet.mdx"; import android36 from "/src/fragments/lib/datastore/android/data-access/query-pagination-snippet.mdx"; import flutter37 from "/src/fragments/lib/datastore/flutter/data-access/query-pagination-snippet.mdx"; ### Query, then observe changes To both query _and_ observe subsequent changes to a Model, consider using [`observeQuery`](/lib/datastore/real-time#displaying-data-during-the-sync-process-with-observequery).