|
| 1 | +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + title: 'Batch DynamoDB Operations', |
| 5 | + description: |
| 6 | + 'Batch DynamoDB Operations', |
| 7 | + platforms: [ |
| 8 | + 'android', |
| 9 | + 'angular', |
| 10 | + 'flutter', |
| 11 | + 'javascript', |
| 12 | + 'nextjs', |
| 13 | + 'react', |
| 14 | + 'react-native', |
| 15 | + 'swift', |
| 16 | + 'vue' |
| 17 | + ] |
| 18 | +}; |
| 19 | + |
| 20 | +export const getStaticPaths = async () => { |
| 21 | + return getCustomStaticPath(meta.platforms); |
| 22 | +}; |
| 23 | + |
| 24 | +export function getStaticProps() { |
| 25 | + return { |
| 26 | + props: { |
| 27 | + meta |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +Batch DynamoDB operations allow you to add multiple items in single mutation. |
| 33 | + |
| 34 | +## Step 1 - Define a custom mutation |
| 35 | + |
| 36 | +```ts |
| 37 | +import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; |
| 38 | + |
| 39 | +const schema = a.schema({ |
| 40 | + // 1. Define your return type as a custom type or model |
| 41 | + Post: a.model({ |
| 42 | + id: a.id(), |
| 43 | + content: a.string(), |
| 44 | + likes: a.integer() |
| 45 | + }), |
| 46 | + |
| 47 | + // 2. Define your mutation with the return type and, optionally, arguments |
| 48 | + BatchCreatePost: a |
| 49 | + .mutation() |
| 50 | + // arguments that this query accepts |
| 51 | + .arguments({ |
| 52 | + content: a.string().array() |
| 53 | + }) |
| 54 | + .returns(a.ref("Post").array()) |
| 55 | + // only allow signed-in users to call this API |
| 56 | + .authorization(allow => [allow.authenticated()]) |
| 57 | + .handler( |
| 58 | + a.handler.custom({ |
| 59 | + dataSource: a.ref("Post"), |
| 60 | + entry: "./BatchCreatePostHandler.js", |
| 61 | + }) |
| 62 | + ) |
| 63 | +}); |
| 64 | + |
| 65 | +export type Schema = ClientSchema<typeof schema>; |
| 66 | + |
| 67 | +export const data = defineData({ |
| 68 | + schema |
| 69 | +}); |
| 70 | +``` |
| 71 | + |
| 72 | +## Step 2 - Configure custom business logic handler code |
| 73 | + |
| 74 | +After your query or mutation is defined, you need to author your custom business logic using a [custom resolver powered by AppSync JavaScript resolver](https://docs.aws.amazon.com/appsync/latest/devguide/tutorials-js.html). |
| 75 | + |
| 76 | +Custom resolvers work on a "request/response" basis. You choose a data source, map your request to the data source's input parameters, and then map the data source's response back to the query/mutation's return type. Custom resolvers provide the benefit of no cold starts, less infrastructure to manage, and no additional charge for Lambda function invocations. Review [Choosing between custom resolver and function](https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-overview-js.html#choosing-data-source). |
| 77 | + |
| 78 | +In your `amplify/data/resource.ts` file, define a custom handler using `a.handler.custom`. |
| 79 | + |
| 80 | +```ts title="amplify/data/resource.ts" |
| 81 | +import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; |
| 82 | + |
| 83 | +const schema = a.schema({ |
| 84 | + Post: a.model({ |
| 85 | + id: a.id(), |
| 86 | + content: a.string(), |
| 87 | + likes: a.integer() |
| 88 | + }), |
| 89 | + |
| 90 | + BatchCreatePost: a |
| 91 | + .mutation() |
| 92 | + .arguments({ |
| 93 | + contents: a.string().array() |
| 94 | + }) |
| 95 | + .returns(a.ref("Post").array()) |
| 96 | + .authorization(allow => [allow.authenticated()]) |
| 97 | + // 1. Add the custom handler |
| 98 | + .handler( |
| 99 | + a.handler.custom({ |
| 100 | + dataSource: a.ref("Post"), |
| 101 | + entry: "./BatchCreatePostHandler.js", |
| 102 | + }) |
| 103 | + ) |
| 104 | +}); |
| 105 | + |
| 106 | +export type Schema = ClientSchema<typeof schema>; |
| 107 | + |
| 108 | +export const data = defineData({ |
| 109 | + schema |
| 110 | +}); |
| 111 | +``` |
| 112 | + |
| 113 | +```ts title="amplify/data/BatchCreatePostHandler.js" |
| 114 | +import { util } from '@aws-appsync/utils'; |
| 115 | +export function request(ctx) { |
| 116 | + var now = util.time.nowISO8601(); |
| 117 | + |
| 118 | + return { |
| 119 | + operation: "BatchPutItem", |
| 120 | + tables: { |
| 121 | + [`Post-${ctx.stash.awsAppsyncApiId}-${ctx.stash.amplifyBranchName}`]: ctx.args.contents.map((content) => |
| 122 | + util.dynamodb.toMapValues({ |
| 123 | + content, |
| 124 | + id: util.autoId(), |
| 125 | + createdAt: now, |
| 126 | + updatedAt: now, |
| 127 | + }) |
| 128 | + ), |
| 129 | + }, |
| 130 | + }; |
| 131 | +} |
| 132 | + |
| 133 | +export function response(ctx) { |
| 134 | + if (ctx.error) { |
| 135 | + util.error(ctx.error.message, ctx.error.type); |
| 136 | + } |
| 137 | + return ctx.result.data[`Post-${ctx.stash.awsAppsyncApiId}-${ctx.stash.amplifyBranchName}`]; |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +## Step 3 - Invoke the custom query or mutation |
| 142 | + |
| 143 | +From your generated Data client, you can find all your custom queries and mutations under the `client.queries.` and `client.mutations.` APIs respectively. |
| 144 | + |
| 145 | +```ts |
| 146 | +const { data, errors } = await client.mutations.BatchCreatePost({ |
| 147 | + contents: ['Post 1', 'Post 2', 'Post 3'] |
| 148 | +}); |
| 149 | +``` |
0 commit comments