本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 API中使用 Aurora PostgreSQL 搭配資料 AWS AppSync
AWS AppSync 提供資料來源,用於針對使用資料 啟用的 Amazon Aurora 叢集執行SQL陳述式API。您可以使用 AWS AppSync 解析器,針對API具有 GraphQL 查詢、突變和訂閱的資料執行SQL陳述式。
注意
此教學會使用 US-EAST-1
區域。
建立叢集
在將 Amazon RDS資料來源新增至 之前 AWS AppSync,請先在 Aurora Serverless 叢集API上啟用資料。您還必須使用 設定秘密 AWS Secrets Manager。若要建立 Aurora Serverless 叢集,您可以使用 AWS CLI:
aws rds create-db-cluster \ --db-cluster-identifier appsync-tutorial \ --engine aurora-postgresql --engine-version 13.11 \ --engine-mode serverless \ --master-username USERNAME \ --master-user-password COMPLEX_PASSWORD
這將傳回叢集ARN的 。您可以使用 命令來檢查叢集的狀態:
aws rds describe-db-clusters \ --db-cluster-identifier appsync-tutorial \ --query "DBClusters[0].Status"
透過 AWS Secrets Manager 主控台或 建立秘密, AWS CLI 並使用輸入檔案,例如使用 USERNAME
和上一個步驟COMPLEX_PASSWORD
中的 和 :
{ "username": "USERNAME", "password": "COMPLEX_PASSWORD" }
將此作為 參數傳遞至 CLI:
aws secretsmanager create-secret \ --name appsync-tutorial-rds-secret \ --secret-string file://creds.json
這將傳回 ARN 作為秘密。在主控台中建立資料來源時,請記下 Aurora Serverless 叢集ARN的 和秘密以供稍後使用。 AWS AppSync
啟用資料 API
叢集狀態變更為 後available
,API請依照 Amazon RDS 文件 啟用資料。API 必須先啟用資料,才能將其新增為 AWS AppSync 資料來源。您也可以API使用 啟用資料 AWS CLI:
aws rds modify-db-cluster \ --db-cluster-identifier appsync-tutorial \ --enable-http-endpoint \ --apply-immediately
建立資料庫和資料表
啟用您的資料 後API,請使用 中的 aws rds-data execute-statement
命令來驗證其是否有效 AWS CLI。這可確保在將 Aurora Serverless 叢集新增至 之前,您的叢集已正確設定 AWS AppSync API。首先,使用 --sql
參數建立TESTDB資料庫:
aws rds-data execute-statement \ --resource-arn "arn:aws:rds:us-east-1:123456789012:cluster:appsync-tutorial" \ --secret-arn "arn:aws:secretsmanager:us-east-1:123456789012:secret:appsync-tutorial-rds-secret" \ --sql "create DATABASE \"testdb\""
如果執行時沒有任何錯誤,請使用 create table
命令新增兩個資料表:
aws rds-data execute-statement \ --resource-arn "arn:aws:rds:us-east-1:123456789012:cluster:appsync-tutorial" \ --secret-arn "arn:aws:secretsmanager:us-east-1:123456789012:secret:appsync-tutorial-rds-secret" \ --database "testdb" \ --sql 'create table public.todos (id serial constraint todos_pk primary key, description text not null, due date not null, "createdAt" timestamp default now());' aws rds-data execute-statement \ --resource-arn "arn:aws:rds:us-east-1:123456789012:cluster:appsync-tutorial" \ --secret-arn "arn:aws:secretsmanager:us-east-1:123456789012:secret:appsync-tutorial-rds-secret" \ --database "testdb" \ --sql 'create table public.tasks (id serial constraint tasks_pk primary key, description varchar, "todoId" integer not null constraint tasks_todos_id_fk references public.todos);'
如果一切執行都沒有問題,您現在可以在 中將叢集新增為資料來源API。
建立 GraphQL 結構描述
現在,您的 Aurora Serverless Data API 正在執行已設定的資料表,我們將建立 GraphQL 結構描述。您可以手動執行此操作,但 AWS AppSync 可讓您使用API建立精靈從現有資料庫匯入資料表組態,以快速開始。
若要開始:
-
在 AWS AppSync 主控台中,選擇建立 API,然後以 Amazon Aurora 叢集 開始。
-
指定API名稱 等API詳細資訊,然後選擇您的資料庫以產生 API。
-
選擇您的資料庫。如有需要,請更新區域,然後選擇您的 Aurora 叢集和TESTDB資料庫。
-
選擇您的秘密,然後選擇匯入 。
-
找到資料表後,請更新類型名稱。
Todos
將 變更為Todo
,將Tasks
變更為Task
。 -
選擇預覽結構描述 來預覽產生的結構描述。您的結構描述看起來會像這樣:
type Todo { id: Int! description: String! due: AWSDate! createdAt: String } type Task { id: Int! todoId: Int! description: String }
-
對於角色,您可以 AWS AppSync 建立新的角色,或使用類似下列的政策來建立角色:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "rds-data:ExecuteStatement", ], "Resource": [ "arn:aws:rds:us-east-1:123456789012:cluster:appsync-tutorial", "arn:aws:rds:us-east-1:123456789012:cluster:appsync-tutorial:*" ] }, { "Effect": "Allow", "Action": [ "secretsmanager:GetSecretValue" ], "Resource": [ "arn:aws:secretsmanager:us-east-1:123456789012:secret:your:secret:arn:appsync-tutorial-rds-secret", "arn:aws:secretsmanager:us-east-1:123456789012:secret:your:secret:arn:appsync-tutorial-rds-secret:*" ] } ] }
請注意,此政策中有兩個陳述式,您授予角色存取權。第一個資源是您的 Aurora 叢集,第二個資源是您的 AWS Secrets Manager ARN。
選擇下一個 ,檢閱組態詳細資訊,然後選擇建立 API。您現在擁有完全運作的 API。您可以在API結構描述頁面上檢閱 的完整詳細資訊。
的解析程式 RDS
API 建立流程會自動建立解析程式,以與我們的類型互動。如果您查看結構描述頁面,您會找到必要的解析程式:
-
todo
透過Mutation.createTodo
欄位建立 。 -
todo
透過Mutation.updateTodo
欄位更新 。 -
todo
透過Mutation.deleteTodo
欄位刪除 。 -
todo
透過Query.getTodo
欄位取得單一 。 -
todos
透過Query.listTodos
欄位列出全部。
您會找到類型連接的類似欄位和解析程式Task
。我們來進一步了解一些解析程式。
突變。createTodo
從 AWS AppSync 主控台的結構描述編輯器右側,選擇 testdb
旁的 createTodo(...): Todo
。解析程式程式碼使用 rds
模組的 insert
函數,動態建立插入陳述式,將資料新增至todos
資料表。由於我們使用 Postgres,因此我們可以利用 returning
陳述式來取回插入的資料。
讓我們更新解析器以正確指定due
欄位的DATE
類型:
import { util } from '@aws-appsync/utils'; import { insert, createPgStatement, toJsonObject, typeHint } from '@aws-appsync/utils/rds'; export function request(ctx) { const { input } = ctx.args; // if a due date is provided, cast is as `DATE` if (input.due) { input.due = typeHint.DATE(input.due) } const insertStatement = insert({ table: 'todos', values: input, returning: '*', }); return createPgStatement(insertStatement) } export function response(ctx) { const { error, result } = ctx; if (error) { return util.appendError( error.message, error.type, result ) } return toJsonObject(result)[0][0] }
儲存解析器。類型提示會將輸入物件中的 due
正確標記為 DATE
類型。這可讓 Postgres 引擎正確解譯值。接下來,更新您的結構描述,id
從CreateTodo
輸入中移除 。由於 Postgres 資料庫可以傳回產生的 ID,因此我們可以依賴它建立,並以單一請求傳回結果:
input CreateTodoInput { due: AWSDate! createdAt: String description: String! }
進行變更並更新您的結構描述。前往查詢編輯器,將項目新增至資料庫:
mutation CreateTodo { createTodo(input: {description: "Hello World!", due: "2023-12-31"}) { id due description createdAt } }
您會取得結果:
{ "data": { "createTodo": { "id": 1, "due": "2023-12-31", "description": "Hello World!", "createdAt": "2023-11-14 20:47:11.875428" } } }
查詢。listTodos
從主控台的結構描述編輯器右側,選擇 testdb
旁的 listTodos(id: ID!): Todo
。請求處理常式會使用選取的公用程式函數,在執行時間動態建置請求。
export function request(ctx) { const { filter = {}, limit = 100, nextToken } = ctx.args; const offset = nextToken ? +util.base64Decode(nextToken) : 0; const statement = select({ table: 'todos', columns: '*', limit, offset, where: filter, }); return createPgStatement(statement) }
我們希望todos
根據due
日期進行篩選。讓我們更新解析器,將due
值轉換為 DATE
。更新匯入清單和請求處理常式:
import { util } from '@aws-appsync/utils'; import * as rds from '@aws-appsync/utils/rds'; export function request(ctx) { const { filter: where = {}, limit = 100, nextToken } = ctx.args; const offset = nextToken ? +util.base64Decode(nextToken) : 0; // if `due` is used in a filter, CAST the values to DATE. if (where.due) { Object.entries(where.due).forEach(([k, v]) => { if (k === 'between') { where.due[k] = v.map((d) => rds.typeHint.DATE(d)); } else { where.due[k] = rds.typeHint.DATE(v); } }); } const statement = rds.select({ table: 'todos', columns: '*', limit, offset, where, }); return rds.createPgStatement(statement); } export function response(ctx) { const { args: { limit = 100, nextToken }, error, result, } = ctx; if (error) { return util.appendError(error.message, error.type, result); } const offset = nextToken ? +util.base64Decode(nextToken) : 0; const items = rds.toJsonObject(result)[0]; const endOfResults = items?.length < limit; const token = endOfResults ? null : util.base64Encode(`${offset + limit}`); return { items, nextToken: token }; }
讓我們嘗試查詢。在查詢編輯器中:
query LIST { listTodos(limit: 10, filter: {due: {between: ["2021-01-01", "2025-01-02"]}}) { items { id due description } } }
突變。updateTodo
您也可以update
使用 Todo
。從查詢編輯器中,讓我們更新第一個 id
Todo
項目1
。
mutation UPDATE { updateTodo(input: {id: 1, description: "edits"}) { description due id } }
請注意,您必須指定要更新的項目id
的 。您也可以指定條件,以僅更新符合特定條件的項目。例如,只有在描述以 開頭時,我們才可能想要編輯項目edits
:
mutation UPDATE { updateTodo(input: {id: 1, description: "edits: make a change"}, condition: {description: {beginsWith: "edits"}}) { description due id } }
就像我們處理 create
和 list
操作的方式一樣,我們可以更新解析器,將 due
欄位轉換為 DATE
。將這些變更儲存至 updateTodo
:
import { util } from '@aws-appsync/utils'; import * as rds from '@aws-appsync/utils/rds'; export function request(ctx) { const { input: { id, ...values }, condition = {}, } = ctx.args; const where = { ...condition, id: { eq: id } }; // if `due` is used in a condition, CAST the values to DATE. if (condition.due) { Object.entries(condition.due).forEach(([k, v]) => { if (k === 'between') { condition.due[k] = v.map((d) => rds.typeHint.DATE(d)); } else { condition.due[k] = rds.typeHint.DATE(v); } }); } // if a due date is provided, cast is as `DATE` if (values.due) { values.due = rds.typeHint.DATE(values.due); } const updateStatement = rds.update({ table: 'todos', values, where, returning: '*', }); return rds.createPgStatement(updateStatement); } export function response(ctx) { const { error, result } = ctx; if (error) { return util.appendError(error.message, error.type, result); } return rds.toJsonObject(result)[0][0]; }
現在請嘗試使用 條件進行更新:
mutation UPDATE { updateTodo( input: { id: 1, description: "edits: make a change", due: "2023-12-12"}, condition: { description: {beginsWith: "edits"}, due: {ge: "2023-11-08"}}) { description due id } }
突變。deleteTodo
您可以Todo
具有 deleteTodo
突變的 delete
。這的運作方式與 updateTodo
突變類似,而且您必須指定您要刪除的項目id
的 :
mutation DELETE { deleteTodo(input: {id: 1}) { description due id } }
撰寫自訂查詢
我們已使用rds
模組公用程式來建立我們的SQL陳述式。我們也可以編寫自己的自訂靜態陳述式來與資料庫互動。首先,更新結構描述以從CreateTask
輸入中移除id
欄位。
input CreateTaskInput { todoId: Int! description: String }
接下來,建立幾個任務。任務與 有外部金鑰關係Todo
:
mutation TASKS { a: createTask(input: {todoId: 2, description: "my first sub task"}) { id } b:createTask(input: {todoId: 2, description: "another sub task"}) { id } c: createTask(input: {todoId: 2, description: "a final sub task"}) { id } }
在名為 的Query
類型中建立新的欄位getTodoAndTasks
:
getTodoAndTasks(id: Int!): Todo
將tasks
欄位新增至 Todo
類型:
type Todo { due: AWSDate! id: Int! createdAt: String description: String! tasks:TaskConnection }
儲存結構描述。從主控台的結構描述編輯器右側,選擇連接解析程式getTodosAndTasks(id: Int!): Todo
。選擇您的 Amazon RDS資料來源。使用下列程式碼更新您的解析程式:
import { sql, createPgStatement,toJsonObject } from '@aws-appsync/utils/rds'; export function request(ctx) { return createPgStatement( sql`SELECT * from todos where id = ${ctx.args.id}`, sql`SELECT * from tasks where "todoId" = ${ctx.args.id}`); } export function response(ctx) { const result = toJsonObject(ctx.result); const todo = result[0][0]; if (!todo) { return null; } todo.tasks = { items: result[1] }; return todo; }
在此程式碼中,我們使用sql
標籤範本來撰寫SQL陳述式,以便在執行時間安全地傳遞動態值至 。 一次最多createPgStatement
可能需要兩個SQL請求。我們使用它來傳送一個查詢給 todo
,另一個則傳送給我們的 tasks
。您可以針對該事項使用JOIN
陳述式或任何其他方法完成此操作。該想法能夠撰寫您自己的SQL陳述式來實作您的業務邏輯。若要在查詢編輯器中使用查詢,我們可以嘗試:
query TodoAndTasks { getTodosAndTasks(id: 2) { id due description tasks { items { id description } } } }
刪除叢集
重要
刪除叢集是永久性的。在執行此動作之前,請徹底檢閱您的專案。
若要刪除叢集:
$ aws rds delete-db-cluster \ --db-cluster-identifier appsync-tutorial \ --skip-final-snapshot