Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Choose your framework/language

Page updated Jul 12, 2024

Customize your data model

Data modeling capabilities

Every data model is defined as part of a data schema (a.schema()). You can enhance your data model with various fields, customize their identifiers, apply authorization rules, or model relationships. Every data model (a.model()) automatically provides create, read, update, and delete API operations as well as real-time subscription events. Below is a quick tour of the many functionalities you can add to your data model:

import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
const schema = a
.schema({
Customer: a
.model({
customerId: a.id().required(),
// fields can be of various scalar types,
// such as string, boolean, float, integers etc.
name: a.string(),
// fields can be of custom types
location: a.customType({
// fields can be required or optional
lat: a.float().required(),
long: a.float().required(),
}),
// fields can be enums
engagementStage: a.enum(["PROSPECT", "INTERESTED", "PURCHASED"]),
collectionId: a.id(),
collection: a.belongsTo("Collection", "collectionId")
// Use custom identifiers. By default, it uses an `id: a.id()` field
})
.identifier(["customerId"]),
Collection: a
.model({
customers: a.hasMany("Customer", "collectionId"), // setup relationships between types
tags: a.string().array(), // fields can be arrays
representativeId: a.id().required(),
// customize secondary indexes to optimize your query performance
})
.secondaryIndexes((index) => [index("representativeId")]),
})
.authorization((allow) => [allow.publicApiKey()]);
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "apiKey",
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});

Gen 1 schema support

If you are coming from Gen 1, you can continue to use the GraphQL Schema Definition Language (SDL) for defining your schema. However, we strongly recommend you use the TypeScript-first schema builder experience in your project as it provides type safety and is the recommended way of working with Amplify going forward.

Note: Some features available in Gen 1 GraphQL SDL are not available in Gen 2. See the feature matrix for features supported in Gen 2.

amplify/data/resource.ts
import { defineData } from '@aws-amplify/backend';
const schema = /* GraphQL */`
type Todo @model @auth(rules: [{ allow: owner }]) {
content: String
isDone: Boolean
}
`;
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: "apiKey",
apiKeyAuthorizationMode: {
expiresInDays: 30,
},
},
});