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