Create, update, and delete application data
In this guide, you will learn how to create, update, and delete your data using Amplify Libraries' Data client.
Before you begin, you will need:
Create an item
You can create an item by first generating the Data client with your backend Data schema. Then you can add an item:
import { generateClient } from 'aws-amplify/data';import { type Schema } from '../amplify/data/resource'
const client = generateClient<Schema>();
const { errors, data: newTodo } = await client.models.Todo.create({ content: "My new todo", isDone: true,})
Update an item
To update the item, use the update
function:
import { generateClient } from 'aws-amplify/data';import { type Schema } from '../amplify/data/resource';
const client = generateClient<Schema>();
const todo = { id: 'some_id', content: 'Updated content',};
const { data: updatedTodo, errors } = await client.models.Todo.update(todo);
Delete an item
You can then delete the Todo by using the delete mutation. To specify which item to delete, you only need to provide the id
of that item:
import { generateClient } from 'aws-amplify/data';import { type Schema } from '../amplify/data/resource'
const client = generateClient<Schema>();
const toBeDeletedTodo = { id: '123123213'}
const { data: deletedTodo, errors } = await client.models.Todo.delete(toBeDeletedTodo)
Cancel create, update, and delete requests
You can cancel any mutation API request by calling .cancel
on the mutation request promise that's returned by .create(...)
, .update(...)
, or .delete(...)
.
const promise = client.models.Todo.create({ content: 'New Todo ' });// ^ Note: we're not awaiting the request, we're returning the promise
try { await promise;} catch (error) { console.log(error); // If the error is because the request was cancelled you can confirm here. if (client.isCancelError(error)) { console.log(error.message); // "my message for cancellation" // handle user cancellation logic }}
//...
// To cancel the above requestclient.cancel(promise, 'my message for cancellation');
You need to ensure that the promise returned from .create()
, .update()
, and .delete()
has not been modified. Typically, async functions wrap the promise being returned into another promise. For example, the following will not work:
async function makeAPICall() { return client.models.Todo.create({ content: 'New Todo' });}const promise = makeAPICall();
// The following will NOT cancel the request.client.cancel(promise, 'my error message');
Conclusion
Congratulations! You have finished the Create, update, and delete application data guide. In this guide, you created, updated, and deleted your app data.
Next steps
Our recommended next steps include using the API to query data and subscribe to real-time events to look for mutations in your data. Some resources that will help with this work include: