Name:
interface
Value:
Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.
Maintenance ModeYou are viewing Amplify Gen 1 documentation. Amplify Gen 1 has entered maintenance mode and will reach end of life on May 1, 2027. New project should use Amplify Gen 2. For existing Gen 1 projects, a migration guide and tooling are available to help you upgrade. Switch to the latest Gen 2 docs →

バージョニングと競合解決

@versioned

@versioned ディレクティブは、タイプにオブジェクトバージョニングと競合解決を追加します。DataStore を活用する場合、競合検出および解決機能は AppSync 内で自動的に処理され、@versioned ディレクティブと互換性がないため、このディレクティブを使用しないでください。

@versioned は AppSync codegen を使用して生成されたクライアントコード(ステートメントとタイプ)でのみサポートされます。 @versionedamplify codegen models を使用して生成されたモデルではサポートされません。 オフラインアプリデータアクセスと組み込みの競合解決を提供するには、@versioned の代わりに Amplify DataStore を使用してください。

定義

directive @versioned(versionField: String = "version", versionInput: String = "expectedVersion") on OBJECT

使用方法

オブジェクトバージョニングと競合検出を有効にするには、@model で注釈されたタイプに @versioned を追加します。

type Post @model @versioned {
id: ID!
title: String!
version: Int! # <- 指定されない場合は自動的に追加されます。
}

Post を作成するとバージョンが自動的に 1 に設定されます

mutation Create {
createPost(input:{
title:"Conflict detection in the cloud!"
}) {
id
title
version # will be 1
}
}

Post を更新するには、オブジェクトの最後に保存されたバージョンである「expectedVersion」を渡す必要があります

注: オブジェクトを更新するとき、バージョン番号は自動的にインクリメントされます。

mutation Update($postId: ID!) {
updatePost(
input:{
id: $postId,
title: "Conflict detection in the cloud is great!",
expectedVersion: 1
}
) {
id
title
version # will be 2
}
}

Post を削除するには、オブジェクトの最後に保存されたバージョンである「expectedVersion」を渡す必要があります

mutation Delete($postId: ID!) {
deletePost(
input: {
id: $postId,
expectedVersion: 2
}
) {
id
title
version
}
}

expectedVersion が DynamoDB に保存されているバージョンと一致しない場合、更新および削除操作は失敗します。タイプのバージョンフィールドのデフォルト名、および @versioned ディレクティブの versionField および versionInput 引数を使用して入力フィールドの名前を変更することができます。

生成される内容

@versioned ディレクティブはリゾルバマッピングテンプレートを操作し、バージョン管理されたオブジェクトに version フィールドを保存します。