Manipulating data
Getting started
To get started, first import the DataStore
API:
import { DataStore } from 'aws-amplify/datastore';
Create and update
To write data to the DataStore, pass an instance of a model to Amplify.DataStore.save()
:
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.
// Example showing how to observe the model and keep state updated before// performing a save. This uses the useEffect React hook, but you can// substitute for a similar mechanism in your application lifecycle with// other frameworks.
const App = () => { const [post, setPost] = useState();
useEffect(() => { /** * This keeps `post` fresh. */ const sub = DataStore.observeQuery(Post, (c) => c.id.eq('5a3b284c-8afc-436a-9027-c8c32bfc8ed2') ).subscribe(({ items }) => { setPost(items[0]); });
return () => { sub.unsubscribe(); }; }, []);
/** * Create a new Post */ async function onCreate() { const _post = await DataStore.save( new Post({ title: `New title ${Date.now()}`, rating: Math.floor(Math.random() * (8 - 1) + 1), status: PostStatus.ACTIVE }) );
setPost(_post); }
return ( <SafeAreaView> <StatusBar /> <ScrollView contentInsetAdjustmentBehavior="automatic"> <View> <Text>{post?.title}</Text> <Button onPress={onCreate} title={'New Post'} /> <TextInput disabled={!post} value={post?.name ?? ''} onChangeText={(text) => { /** * Each keypress updates the post in local React state. */ setPost( Post.copyOf(post, (draft) => { draft.title = text; }) ); }} /> <Button disabled={!post} title={'Save'} onPress={async () => { /** * This post is already up-to-date because `observeQuery` updated it. */ if (!post) { return; } const savedPost = await DataStore.save(post); console.log('Post saved: ', savedPost); }} /> </View> </ScrollView> </SafeAreaView> );};
Delete
To delete an item, simply pass in an instance.
const toDelete = await DataStore.query(Post, '1234567');if (toDelete) { DataStore.delete(toDelete);}
const toDelete = await DataStore.query(Post, '1234567');DataStore.delete(toDelete);
You can also pass predicate operators to delete multiple items. For example, the following will delete all draft posts:
await DataStore.delete(Post, (post) => post.status.eq(PostStatus.INACTIVE));
Additionally, you can perform a conditional delete. For instance, only delete if a post is in draft status by passing in an instance of a model:
const toDelete = await DataStore.query(Post, '123');if (toDelete) { DataStore.delete(toDelete, (post) => post.status.eq(PostStatus.INACTIVE));}
const todelete = await DataStore.query(Post, '123');DataStore.delete(todelete, (post) => post.status.eq(PostStatus.INACTIVE));
Also, to delete all items for a model you can use Predicates.ALL
:
await DataStore.delete(Post, Predicates.ALL);
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.
Querying for all items
To query for all items, pass in the model name as the argument.
const posts = await DataStore.query(Post);
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:
For example if you wanted a list of all Post
Models that have a rating
greater than 4:
const posts = await DataStore.query(Post, (c) => c.rating.gt(4));
Multiple conditions can also be used, like the ones defined in GraphQL Transform condition statements. For example, fetch all posts that have a rating greater than 4
and are ACTIVE
:
When using multiple conditions, you can wrap the predicates with the and
operator. For example with multiple conditions:
const posts = await DataStore.query(Post, (c) => c.and(c => [ c.rating.gt(4), c.status.eq(PostStatus.ACTIVE)]));
Alternatively, the or
logical operator can also be used:
If you wanted this to be an or
statement you would wrap your combined predicates with c => c.or(...)
const posts = await DataStore.query(Post, (c) => c.or(c => [ c.rating.gt(4), c.status.eq(PostStatus.ACTIVE) ]));
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:
const posts = await DataStore.query(Post, Predicates.ALL, { page: 0, limit: 100});