
## DataStore with Amplify

Amplify DataStore provides a programming model for leveraging shared and distributed data without writing additional code for offline and online scenarios, which makes working with distributed, cross-user data just as simple as working with local-only data.

<Callout>

**Note:** this allows you to start persisting data locally to your device with DataStore, even without an AWS account.

</Callout>

## Goal
To setup and configure your application with Amplify DataStore and use it to persist data locally on a device.

## Prerequisites

import ios1 from "/src/fragments/lib-v1/datastore/ios/getting-started/10_preReq.mdx";

<Fragments fragments={{ios: ios1}} />

import android2 from "/src/fragments/lib-v1/datastore/android/getting-started/10_preReq.mdx";

<Fragments fragments={{android: android2}} />

import flutter3 from "/src/fragments/lib-v1/datastore/flutter/getting-started/10_preReq.mdx";

<Fragments fragments={{flutter: flutter3}} />

import ios4 from "/src/fragments/lib-v1/datastore/ios/getting-started/20_installLib.mdx";

<Fragments fragments={{ios: ios4}} />

import android5 from "/src/fragments/lib-v1/datastore/android/getting-started/20_installLib.mdx";

<Fragments fragments={{android: android5}} />

import flutter6 from "/src/fragments/lib-v1/datastore/flutter/getting-started/20_installLib.mdx";

<Fragments fragments={{flutter: flutter6}} />

## Setup local development environment

import ios8 from "/src/fragments/lib-v1/datastore/ios/getting-started/30_setupEnv.mdx";

<Fragments fragments={{ios: ios8}} />

import android9 from "/src/fragments/lib-v1/datastore/native_common/setup-env-cli.mdx";

<Fragments fragments={{android: android9}} />

import flutter10 from "/src/fragments/lib-v1/datastore/native_common/setup-env-cli.mdx";

<Fragments fragments={{flutter: flutter10}} />

## Idiomatic persistence

DataStore relies on platform standard data structures to represent the data schema in an idiomatic way. The persistence language is composed by data types that satisfies the `Model` interface and operations defined by common verbs such as `save`, `query` and `delete`.

### Data schema

The first step to create an app backed by a persistent datastore is to **define a schema**. DataStore uses GraphQL schema files as the definition of the application data model. The schema contains data types and relationships that represent the app's functionality.

### Sample schema

For the next steps, let's start with a schema for a small blog application. Currently, it has only a single model. New types and constructs will be added to this base schema as more concepts are presented.

Open the `schema.graphql` file located by default at `amplify/backend/{api_name}/` and **define a model** `Post` as follows.

```graphql
type Post @model {
  id: ID!
  title: String!
  status: PostStatus!
  rating: Int
  content: String
}

enum PostStatus {
  ACTIVE
  INACTIVE
}
```

Now you will to convert the platform-agnostic `schema.graphql` into platform-specific data structures. DataStore relies on code generation to guarantee schemas are correctly converted to platform code.

Like the initial setup, models can be generated either using the IDE integration or Amplify CLI directly.

### Code generation: Platform integration

import ios12 from "/src/fragments/lib-v1/datastore/ios/getting-started/40_codegen.mdx";

<Fragments fragments={{ios: ios12}} />

import android13 from "/src/fragments/lib-v1/datastore/android/getting-started/40_codegen.mdx";

<Fragments fragments={{android: android13}} />

import flutter14 from "/src/fragments/lib-v1/datastore/flutter/getting-started/40_codegen.mdx";

<Fragments fragments={{flutter: flutter14}} />

### Code generation: Amplify CLI

import ios16 from "/src/fragments/lib-v1/datastore/native_common/codegen.mdx";

<Fragments fragments={{ios: ios16}} />

import android17 from "/src/fragments/lib-v1/datastore/android/getting-started/50_codegenCli.mdx";

<Fragments fragments={{android: android17}} />

import flutter18 from "/src/fragments/lib-v1/datastore/flutter/getting-started/50_codegenCli.mdx";

<Fragments fragments={{flutter: flutter18}} />

## Initialize Amplify DataStore

import ios20 from "/src/fragments/lib-v1/datastore/ios/getting-started/50_initDataStore.mdx";

<Fragments fragments={{ios: ios20}} />

import android21 from "/src/fragments/lib-v1/datastore/android/getting-started/60_initDataStore.mdx";

<Fragments fragments={{android: android21}} />

import flutter22 from "/src/fragments/lib-v1/datastore/flutter/getting-started/60_initDataStore.mdx";

<Fragments fragments={{flutter: flutter22}} />

## Persistence operations

Now the application is ready to execute persistence operations. The data will be persisted to a local database, enabling offline-first use cases by default.

Even though a GraphQL API is already added to your project, the cloud synchronization will only be enabled when the API plugin is initialized and the backend provisioned. See the [Next steps](#next-steps) for more info.

### Writing to the database

To write to the database, create an instance of the `Post` model and save it.

import ios24 from "/src/fragments/lib-v1/datastore/ios/getting-started/60_saveSnippet.mdx";

<Fragments fragments={{ios: ios24}} />

import android25 from "/src/fragments/lib-v1/datastore/android/getting-started/70_saveSnippet.mdx";

<Fragments fragments={{android: android25}} />

import flutter26 from "/src/fragments/lib-v1/datastore/flutter/getting-started/80_saveSnippet.mdx";

<Fragments fragments={{flutter: flutter26}} />

### Reading from the database

To read from the database, the simplest approach is to query for all records of a given model type.

import ios28 from "/src/fragments/lib-v1/datastore/ios/getting-started/70_querySnippet.mdx";

<Fragments fragments={{ios: ios28}} />

import android29 from "/src/fragments/lib-v1/datastore/android/getting-started/80_querySnippet.mdx";

<Fragments fragments={{android: android29}} />

import flutter30 from "/src/fragments/lib-v1/datastore/flutter/getting-started/70_querySnippet.mdx";

<Fragments fragments={{flutter: flutter30}} />

## Next steps

Congratulations! You’ve created and retrieved data from the local database. Check out the following links to see other Amplify DataStore use cases and advanced concepts:

- [Write data](/lib-v1/datastore/data-access#create-and-update)
- [Query data](/lib-v1/datastore/data-access#query-data)
- [Model associations](/lib-v1/datastore/relational)
- [Cloud synchronization](/lib-v1/datastore/sync)
- [Clear local data](/lib-v1/datastore/sync#clear-local-data)
