Customize your data model
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 { a } 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"]), // Use custom identifiers. By default, it uses an `id: a.id()` field }) .identifier(["customerId"]), Collection: a .model({ customers: a.hasMany("Customer", "customerId"), // 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.owner()]);
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "apiKey", apiKeyAuthorizationMode: { expiresInDays: 30, }, },});
Add fields to data model
Configure built-in and custom field types.
Modeling relationships
Learn about the types of model relationships and modeling relationships.
Customize data model identifiers
Define the primary key for a model using single-field or composite identifiers.
Customize secondary indexes
Define the secondary indexes for your data model to optimize query performance