Name:
interface
Value:
Extend your Amplify Gen 2 app with AWS Blocks — self-contained backend capabilities you compose into your existing backend.

Page updated Apr 29, 2024

NewDeploy web apps with Git or your AI coding agent using the latest AWS Amplify. Add managed databases and authentication as you grow. Explore the documentation.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade.

Example application

These docs cover Amplify Gen 1. For new web apps, we recommend the latest AWS Amplify, with a simplified CLI and native integrations for databases, storage, and AI models. Get started with the latest AWS Amplify.

Example application

import React, { useEffect } from "react";
import logo from "./logo.svg";
import "./App.css";
import { Amplify } from 'aws-amplify'
import { DataStore, Predicates } from "aws-amplify/datastore";
import { Post, PostStatus } from "./models";
// Use next two lines only if syncing with the cloud
import amplifyconfig from "./amplifyconfiguration";
Amplify.configure(amplifyconfig);
async function onCreate() {
await DataStore.save(
new Post({
title: `New title ${Date.now()}`,
rating: Math.floor(Math.random() * (8 - 1) + 1),
status: PostStatus.ACTIVE,
})
);
}
async function onDeleteAll() {
await DataStore.delete(Post, Predicates.ALL);
}
async function onQuery() {
const posts = await DataStore.query(Post, (c) => c.rating.gt(4));
console.log(posts);
}
function App() {
useEffect(() => {
const subscription = DataStore.observe(Post).subscribe((msg) => {
console.log(msg.model, msg.opType, msg.element);
});
return () => subscription.unsubscribe();
}, []);
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<div>
<input type="button" value="NEW" onClick={onCreate} />
<input type="button" value="DELETE ALL" onClick={onDeleteAll} />
<input type="button" value="QUERY rating > 4" onClick={onQuery} />
</div>
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}
export default App;
import { Component, OnInit, OnDestroy } from '@angular/core';
import { DataStore, Predicates } from "aws-amplify/datastore";
import { Post, PostStatus } from "./models";
@Component({
selector: 'app-root',
template: `
<div class="app-header">
<button type="button" (click)="onCreate()">NEW</button>
<button type="button" (click)="onDeleteAll()">DELETE ALL</button>
<button type="button" (click)="onQuery()">QUERY rating > 4</button>
</div>
`})
export class AppComponent implements OnInit, OnDestroy {
subscription;
ngOnInit() {
//Subscribe to changes
this.subscription = DataStore.observe<Post>(Post).subscribe((msg) => {
console.log(msg.model, msg.opType, msg.element);
});
}
ngOnDestroy() {
if (!this.subscription) return;
this.subscription.unsubscribe();
}
public async loadPosts() {
let posts = await DataStore.query(Post, (c) => c.rating.gt(4));
console.log(posts);
}
public onCreate() {
DataStore.save(
new Post({
title: `New title ${Date.now()}`,
rating: Math.floor(Math.random() * (8 - 1) + 1),
status: PostStatus.ACTIVE,
})
);
}
public onDeleteAll() {
DataStore.delete(Post, Predicates.ALL);
}
}
<template>
<div id="app">
<div class="app-body">
<button type="button" @click="onCreate" >NEW</button>
<button type="button" @click="onDeleteAll" >DELETE ALL</button>
<button type="button" @click="onQuery" >QUERY rating > 4</button>
</div>
</div>
</template>
<script>
import { DataStore, Predicates } from "aws-amplify/datastore";
import { Post, PostStatus } from "./models";
export default {
name: "app",
data() {
return {
subscription: undefined,
};
},
created() {
//Subscribe to changes
this.subscription = DataStore.observe(Post).subscribe(msg => {
console.log(msg.model, msg.opType, msg.element);
});
},
destroyed() {
if (!this.subscription) return;
this.subscription.unsubscribe();
},
methods: {
async onQuery() {
let posts = await DataStore.query(Post, (c) => c.rating.gt(4));
console.log(posts);
},
onCreate() {
DataStore.save(
new Post({
title: `New title ${Date.now()}`,
rating: Math.floor(Math.random() * (8 - 1) + 1),
status: PostStatus.ACTIVE,
})
);
},
deleteAll() {
DataStore.delete(Post, Predicates.ALL);
}
}
};
</script>

API Reference

For the complete API documentation for DataStore, visit our API Reference