
## 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 js0 from "/src/fragments/lib/datastore/js/getting-started/10_preReq.mdx";

<Fragments fragments={{js: js0}} />

import reactnative0 from "/src/fragments/lib/datastore/js/getting-started/10_preReq.mdx";

<Fragments fragments={{'react-native': reactnative0}} />

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

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

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

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

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

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

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

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

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

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

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

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

## Setup local development environment

import js7 from "/src/fragments/lib/datastore/native_common/setup-env.mdx";

<Fragments fragments={{js: js7}} />

import reactnative1 from "/src/fragments/lib/datastore/native_common/setup-env.mdx";

<Fragments fragments={{'react-native': reactnative1}} />

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

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

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

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

import flutter10 from "/src/fragments/lib/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.

import codegenPlatformIntegration from "/src/fragments/lib/datastore/native_common/codegen-platform-integration.mdx";

<Fragments fragments={{js: codegenPlatformIntegration}} />
<Fragments fragments={{'react-native': codegenPlatformIntegration}} />
<Fragments fragments={{ios: codegenPlatformIntegration}} />
<Fragments fragments={{android: codegenPlatformIntegration}} />

### Code generation: Amplify CLI

import js15 from "/src/fragments/lib/datastore/native_common/codegen.mdx";

<Fragments fragments={{js: js15}} />

import reactnative3 from "/src/fragments/lib/datastore/native_common/codegen.mdx";

<Fragments fragments={{'react-native': reactnative3}} />

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

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

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

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

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

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

## Initialize Amplify DataStore

import js19 from "/src/fragments/lib/datastore/js/getting-started/50_initDataStore.mdx";

<Fragments fragments={{js: js19}} />

import reactnative4 from "/src/fragments/lib/datastore/js/getting-started/50_initDataStore.mdx";

<Fragments fragments={{'react-native': reactnative4}} />

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

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

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

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

import flutter22 from "/src/fragments/lib/datastore/flutter/getting-started/50_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 js23 from "/src/fragments/lib/datastore/js/getting-started/60_saveSnippet.mdx";

<Fragments fragments={{js: js23}} />

import reactnative5 from "/src/fragments/lib/datastore/js/getting-started/60_saveSnippet.mdx";

<Fragments fragments={{'react-native': reactnative5}} />

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

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

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

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

import flutter26 from "/src/fragments/lib/datastore/flutter/getting-started/60_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 js27 from "/src/fragments/lib/datastore/js/getting-started/70_querySnippet.mdx";

<Fragments fragments={{js: js27}} />

import reactnative6 from "/src/fragments/lib/datastore/js/getting-started/70_querySnippet.mdx";

<Fragments fragments={{'react-native': reactnative6}} />

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

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

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

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

import flutter30 from "/src/fragments/lib/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/datastore/data-access#create-and-update)
- [Query data](/lib/datastore/data-access#query-data)
- [Model associations](/lib/datastore/relational)
- [Cloud synchronization](/lib/datastore/sync)
- [Clear local data](/lib/datastore/sync#clear-local-data)
