The resource.ts
file is the central location for all back-end data configurations. The most important element is the schema
object which defines the back-end data models. Let's start with an example:
import {type ClientSchema, a, defineData} from "@aws-amplify/backend";
const schema = a.schema({
ToDo: a
.model({
content: a.string(),
})
.authorization((allow) => [allow.owner()]),
});
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({
schema,
authorizationModes: {
defaultAuthorizationMode: 'userPool'
}
});
a.schema
, a.model
, a.string()
, a.integer()
, a.boolean()
, etc.userPool
is one of many Authorization modes to choose from. This means requests must be authenticated via Cognito User Pool JWTs.ClientSchema<typeof schema>
, it auto-generates types for your data model that your frontend (and backend) can use for full type safety.a.schema({...})
and a.model({...})
do?Todo
with the provided fields.Todo
model.You can add more fields:
Todo: a.model({
content: a.string(),
isDone: a.boolean(),
dueDate: a.datetime(),
})
authorization((allow) => [allow.owner()])
mean?This sets a row-level security for your model.
allow.owner()
means: Only the record creator/owner can create, read, update, or delete it.allow.public()
.