## 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. **Note:** this allows you to start persisting data locally to your device with DataStore, even without an AWS account. ## 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"; import android2 from "/src/fragments/lib-v1/datastore/android/getting-started/10_preReq.mdx"; import flutter3 from "/src/fragments/lib-v1/datastore/flutter/getting-started/10_preReq.mdx"; import ios4 from "/src/fragments/lib-v1/datastore/ios/getting-started/20_installLib.mdx"; import android5 from "/src/fragments/lib-v1/datastore/android/getting-started/20_installLib.mdx"; import flutter6 from "/src/fragments/lib-v1/datastore/flutter/getting-started/20_installLib.mdx"; ## Setup local development environment import ios8 from "/src/fragments/lib-v1/datastore/ios/getting-started/30_setupEnv.mdx"; import android9 from "/src/fragments/lib-v1/datastore/native_common/setup-env-cli.mdx"; import flutter10 from "/src/fragments/lib-v1/datastore/native_common/setup-env-cli.mdx"; ## 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"; import android13 from "/src/fragments/lib-v1/datastore/android/getting-started/40_codegen.mdx"; import flutter14 from "/src/fragments/lib-v1/datastore/flutter/getting-started/40_codegen.mdx"; ### Code generation: Amplify CLI import ios16 from "/src/fragments/lib-v1/datastore/native_common/codegen.mdx"; import android17 from "/src/fragments/lib-v1/datastore/android/getting-started/50_codegenCli.mdx"; import flutter18 from "/src/fragments/lib-v1/datastore/flutter/getting-started/50_codegenCli.mdx"; ## Initialize Amplify DataStore import ios20 from "/src/fragments/lib-v1/datastore/ios/getting-started/50_initDataStore.mdx"; import android21 from "/src/fragments/lib-v1/datastore/android/getting-started/60_initDataStore.mdx"; import flutter22 from "/src/fragments/lib-v1/datastore/flutter/getting-started/60_initDataStore.mdx"; ## 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"; import android25 from "/src/fragments/lib-v1/datastore/android/getting-started/70_saveSnippet.mdx"; import flutter26 from "/src/fragments/lib-v1/datastore/flutter/getting-started/80_saveSnippet.mdx"; ### 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"; import android29 from "/src/fragments/lib-v1/datastore/android/getting-started/80_querySnippet.mdx"; import flutter30 from "/src/fragments/lib-v1/datastore/flutter/getting-started/70_querySnippet.mdx"; ## 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)