Looking for how to use this in your app?See Frontend Libraries →
データモデルをカスタマイズする
データモデリング機能
すべてのデータモデルはデータスキーマ(a.schema())の一部として定義されます。様々なフィールド、カスタム識別子、認可ルール、またはリレーションシップを使用してデータモデルを拡張できます。すべてのデータモデル(a.model())は自動的に作成、読み取り、更新、削除のAPI操作とリアルタイムサブスクリプションイベントを提供します。以下は、データモデルに追加できる多くの機能の簡単なツアーです。
import { type ClientSchema, a, defineData } 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"]), collectionId: a.id(), collection: a.belongsTo("Collection", "collectionId") // Use custom identifiers. By default, it uses an `id: a.id()` field }) .identifier(["customerId"]), Collection: a .model({ customers: a.hasMany("Customer", "collectionId"), // 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.publicApiKey()]);
export type Schema = ClientSchema<typeof schema>;
export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "apiKey", apiKeyAuthorizationMode: { expiresInDays: 30, }, },});データモデルにフィールドを追加
組み込みフィールドタイプとカスタムフィールドタイプを設定します。
モデル関係の構築
モデル関係のタイプとモデル関係の構築について学習します。
データモデル識別子のカスタマイズ
単一フィールドまたは複合識別子を使用してモデルの主キーを定義します。
セカンダリインデックスのカスタマイズ
データモデルのセカンダリインデックスを定義して、クエリパフォーマンスを最適化します
操作の無効化
データモデルの操作を無効化する
Gen 1 スキーマサポート
Gen 1 から移行する場合、GraphQL スキーマ定義言語(SDL)を引き続き使用してスキーマを定義できます。ただし、プロジェクトで TypeScript ファーストのスキーマビルダーエクスペリエンスを使用することを強くお勧めします。これは型安全性を提供し、今後 Amplify で作業するための推奨方法です。
amplify/data/resource.ts
import { defineData } from '@aws-amplify/backend';
const schema = /* GraphQL */` type Todo @model @auth(rules: [{ allow: owner }]) { content: String isDone: Boolean }`;
export const data = defineData({ schema, authorizationModes: { defaultAuthorizationMode: "apiKey", apiKeyAuthorizationMode: { expiresInDays: 30, }, },});