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

Page updated May 1, 2024

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 = a
4 .schema({
5 Customer: a
6 .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 types
12 location: a.customType({
13 // fields can be required or optional
14 lat: a.float().required(),
15 long: a.float().required(),
16 }),
17 // fields can be enums
18 engagementStage: a.enum(["PROSPECT", "INTERESTED", "PURCHASED"]),
19 // Use custom identifiers. By default, it uses an `id: a.id()` field
20 })
21 .identifier(["customerId"]),
22 Collection: a
23 .model({
24 customers: a.hasMany("Customer", "customerId"), // setup relationships between types
25 tags: a.string().array(), // fields can be arrays
26 representativeId: a.id().required(),
27 // customize secondary indexes to optimize your query performance
28 })
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});