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:
1import { a } from "@aws-amplify/backend";2
3const schema = a4 .schema({5 Customer: a6 .model({7 customerId: a.id().required(),8 // fields can be of various scalar types,9 // such as string, boolean, float, integers etc.10 name: a.string(),11 // fields can be of custom types12 location: a.customType({13 // fields can be required or optional14 lat: a.float().required(),15 long: a.float().required(),16 }),17 // fields can be enums18 engagementStage: a.enum(["PROSPECT", "INTERESTED", "PURCHASED"]),19 // Use custom identifiers. By default, it uses an `id: a.id()` field20 })21 .identifier(["customerId"]),22 Collection: a23 .model({24 customers: a.hasMany("Customer", "customerId"), // setup relationships between types25 tags: a.string().array(), // fields can be arrays26 representativeId: a.id().required(),27 // customize secondary indexes to optimize your query performance28 })29 .secondaryIndexes((index) => [index("representativeId")]),30 })31 .authorization((allow) => [allow.owner()]);32
33export type Schema = ClientSchema<typeof schema>;34
35export const data = defineData({36 schema,37 authorizationModes: {38 defaultAuthorizationMode: "apiKey",39 apiKeyAuthorizationMode: {40 expiresInDays: 30,41 },42 },43});
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