## 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 js0 from "/src/fragments/lib/datastore/js/getting-started/10_preReq.mdx"; import reactnative0 from "/src/fragments/lib/datastore/js/getting-started/10_preReq.mdx"; import ios1 from "/src/fragments/lib/datastore/ios/getting-started/10_preReq.mdx"; import android2 from "/src/fragments/lib/datastore/android/getting-started/10_preReq.mdx"; import flutter3 from "/src/fragments/lib/datastore/flutter/getting-started/10_preReq.mdx"; import ios4 from "/src/fragments/lib/datastore/ios/getting-started/20_installLib.mdx"; import android5 from "/src/fragments/lib/datastore/android/getting-started/20_installLib.mdx"; import flutter6 from "/src/fragments/lib/datastore/flutter/getting-started/20_installLib.mdx"; ## Setup local development environment import js7 from "/src/fragments/lib/datastore/native_common/setup-env.mdx"; import reactnative1 from "/src/fragments/lib/datastore/native_common/setup-env.mdx"; import ios8 from "/src/fragments/lib/datastore/ios/getting-started/30_setupEnv.mdx"; import android9 from "/src/fragments/lib/datastore/native_common/setup-env-cli.mdx"; import flutter10 from "/src/fragments/lib/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. import codegenPlatformIntegration from "/src/fragments/lib/datastore/native_common/codegen-platform-integration.mdx"; ### Code generation: Amplify CLI import js15 from "/src/fragments/lib/datastore/native_common/codegen.mdx"; import reactnative3 from "/src/fragments/lib/datastore/native_common/codegen.mdx"; import ios16 from "/src/fragments/lib/datastore/native_common/codegen.mdx"; import android17 from "/src/fragments/lib/datastore/android/getting-started/50_codegenCli.mdx"; import flutter18 from "/src/fragments/lib/datastore/flutter/getting-started/50_codegenCli.mdx"; ## Initialize Amplify DataStore import js19 from "/src/fragments/lib/datastore/js/getting-started/50_initDataStore.mdx"; import reactnative4 from "/src/fragments/lib/datastore/js/getting-started/50_initDataStore.mdx"; import ios20 from "/src/fragments/lib/datastore/ios/getting-started/50_initDataStore.mdx"; import android21 from "/src/fragments/lib/datastore/android/getting-started/60_initDataStore.mdx"; import flutter22 from "/src/fragments/lib/datastore/flutter/getting-started/50_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 js23 from "/src/fragments/lib/datastore/js/getting-started/60_saveSnippet.mdx"; import reactnative5 from "/src/fragments/lib/datastore/js/getting-started/60_saveSnippet.mdx"; import ios24 from "/src/fragments/lib/datastore/ios/getting-started/60_saveSnippet.mdx"; import android25 from "/src/fragments/lib/datastore/android/getting-started/70_saveSnippet.mdx"; import flutter26 from "/src/fragments/lib/datastore/flutter/getting-started/60_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 js27 from "/src/fragments/lib/datastore/js/getting-started/70_querySnippet.mdx"; import reactnative6 from "/src/fragments/lib/datastore/js/getting-started/70_querySnippet.mdx"; import ios28 from "/src/fragments/lib/datastore/ios/getting-started/70_querySnippet.mdx"; import android29 from "/src/fragments/lib/datastore/android/getting-started/80_querySnippet.mdx"; import flutter30 from "/src/fragments/lib/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/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)