Store user-generated data
- How-to
- 4 minutes
If your app allows the user to save user-generated data that needs to be available across multiple user sessions, our document service is your best option. One example of such user-defined data is to-do items that the user enters in a Todo app. This guide explains how you can store, update, and delete data from the document service.
In the following examples, you'll see two code snippets for each type of operation.
- The React hook version uses the
@dynatrace-sdk/react-hooks
package for all the operations. You can use this in your React frontend code. - The Client SDK version uses the
@dynatrace-sdk/client-document
) package for all the operations. The example code shown uses this package in an app function. You can use the same code in Notebook, Dashboard, and Workflow as well.
Create a document
To create a document, use the useCreateDocument
hook. This hook provides an execute
function that you can use to create a new document. It also provides some additional fields with information about the loading status, errors, and the response. These fields are common to all the document hooks. The example below shows how to create a Todo list document:
- React Hook
- Client SDK
import React from 'react';
import { useCreateDocument } from '@dynatrace-sdk/react-hooks';
import { Button } from '@dynatrace/strato-components/buttons';
import { Page } from '@dynatrace/strato-components-preview/layouts';
const todos = [
{
title: 'Send email to John about vacation',
done: false,
},
{
title: 'Plan workshop for next week',
done: false,
},
];
export const App = () => {
const { execute, data, error, isLoading } = useCreateDocument();
const handleClick = () => {
execute({
body: {
name: 'My Todos',
type: 'TodoList',
content: new Blob([JSON.stringify(todos)], {
type: 'application/json',
}),
},
});
};
return (
<Page>
<Page.Main>
<Button onClick={handleClick}>Create Todos</Button>
</Page.Main>
</Page>
);
};
import { documentsClient } from '@dynatrace-sdk/client-document';
const todos = [
{ title: 'Send email to John about vacation', done: false },
{ title: 'Plan workshop for next week', done: false },
];
export default async function (payload: unknown = undefined) {
try {
const data = await documentsClient.createDocument({
body: {
name: 'My Todos',
type: 'TodoList',
content: new Blob([JSON.stringify(todos)], {
type: 'application/json',
}),
},
});
return data ? 'document created successfully' : 'document creation failed';
} catch (error) {
console.error('Error creating document:', error);
return 'document creation failed due to an error';
}
}
This operation requires the scope document:documents:write
. Read more about scopes in this guide.
List documents
The useListDocuments
hook lets you to get a filtered list of the available documents. Besides the common fields, this hook also provides a refetch
function to refetch the list of documents. The following code fetches the documents of type TodoList.
- React Hook
- Client SDK
import React from 'react';
import { useListDocuments } from '@dynatrace-sdk/react-hooks';
import { Button } from '@dynatrace/strato-components/buttons';
import { List, Text } from '@dynatrace/strato-components/typography';
import { Page } from '@dynatrace/strato-components-preview/layouts';
export const App = () => {
const { data, refetch } = useListDocuments({
filter: `type == 'TodoList'`,
});
const handleClick = () => {
refetch().then((list) => console.log(list.documents));
};
return (
<Page>
<Page.Main>
{data && (
<List>
{data.documents.map((doc) => (
<Text key={doc.id}>{doc.name}</Text>
))}
</List>
)}
<Button onClick={handleClick}>Refetch Todos</Button>
</Page.Main>
</Page>
);
};
import { documentsClient } from '@dynatrace-sdk/client-document';
export default async function (payload: unknown = undefined) {
try {
const list = await documentsClient.listDocuments({
filter: `type == 'TodoList'`,
});
return list?.documents ?? [];
} catch (error) {
console.error('Error fetching TodoList documents:', error);
return [];
}
}
This operation requires the scope document:documents:read
. Read more about scopes in this guide.