Skip to content

Commit

Permalink
run data snippets through prettier, naming updates (#7827)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefaidt authored Oct 15, 2024
1 parent fadf3ca commit d593c37
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -481,19 +481,18 @@ To delete your external DynamoDB table, you can navigate to the AppSync console
[Reference](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-getitem) - The `GetItem` request lets you tell the AWS AppSync DynamoDB function to make a `GetItem` request to DynamoDB, and enables you to specify:
- The key of the item in DynamoDB
- Whether to use a consistent read or not
**Example:**
```js
export function request(ctx) {
const {foo, bar} = ctx.args
const { foo, bar } = ctx.args;
return {
operation : "GetItem",
key : util.dynamodb.toMapValues({foo, bar}),
consistentRead : true
}
operation: 'GetItem',
key: util.dynamodb.toMapValues({ foo, bar }),
consistentRead: true
};
}
```
Expand All @@ -502,21 +501,20 @@ export function request(ctx) {
[PutItem](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-putitem) - The `PutItem` request mapping document lets you tell the AWS AppSync DynamoDB function to make a `PutItem` request to DynamoDB, and enables you to specify the following:
- The key of the item in DynamoDB
- The full contents of the item (composed of key and attributeValues)
- Conditions for the operation to succeed
**Example:**
```js
import { util } from '@aws-appsync/utils';

export function request(ctx) {
const { foo, bar, ...values} = ctx.args
const { foo, bar, ...values } = ctx.args;
return {
operation: 'PutItem',
key: util.dynamodb.toMapValues({foo, bar}),
attributeValues: util.dynamodb.toMapValues(values),
key: util.dynamodb.toMapValues({ foo, bar }),
attributeValues: util.dynamodb.toMapValues(values)
};
}
```
Expand All @@ -526,15 +524,14 @@ export function request(ctx) {
[UpdateItem](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-updateitem) - The `UpdateItem` request enables you to tell the AWS AppSync DynamoDB function to make a `UpdateItem` request to DynamoDB and allows you to specify the following:
- The key of the item in DynamoDB
- An update expression describing how to update the item in DynamoDB
- Conditions for the operation to succeed
**Example:**
```js
import { util } from '@aws-appsync/utils';

export function request(ctx) {
const { id } = ctx.args;
return {
Expand All @@ -543,8 +540,8 @@ export function request(ctx) {
update: {
expression: 'ADD #voteField :plusOne, version :plusOne',
expressionNames: { '#voteField': 'upvotes' },
expressionValues: { ':plusOne': { N: 1 } },
},
expressionValues: { ':plusOne': { N: 1 } }
}
};
}
```
Expand All @@ -554,17 +551,17 @@ export function request(ctx) {
[DeleteItem](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-deleteitem) - The `DeleteItem` request lets you tell the AWS AppSync DynamoDB function to make a `DeleteItem` request to DynamoDB, and enables you to specify the following:
- The key of the item in DynamoDB
- Conditions for the operation to succeed
**Example:**
```js
import { util } from '@aws-appsync/utils';

export function request(ctx) {
return {
operation: 'DeleteItem',
key: util.dynamodb.toMapValues({ id: ctx.args.id }),
key: util.dynamodb.toMapValues({ id: ctx.args.id })
};
}
```
Expand All @@ -574,17 +571,11 @@ export function request(ctx) {
[Query](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-query) - The Query request object lets you tell the AWS AppSync DynamoDB resolver to make a Query request to DynamoDB, and enables you to specify the following:
- Key expression
- Which index to use
- Any additional filter
- How many items to return
- Whether to use consistent reads
- query direction (forward or backward)
- Pagination token
**Example:**
Expand All @@ -598,9 +589,9 @@ export function request(ctx) {
operation: 'Query',
query: {
expression: 'ownerId = :ownerId',
expressionValues: util.dynamodb.toMapValues({ ':ownerId': owner }),
expressionValues: util.dynamodb.toMapValues({ ':ownerId': owner })
},
index: 'owner-index',
index: 'owner-index'
};
}
```
Expand All @@ -609,15 +600,10 @@ export function request(ctx) {
[Scan](https://docs.aws.amazon.com/appsync/latest/devguide/js-resolver-reference-dynamodb.html#js-aws-appsync-resolver-reference-dynamodb-scan) - The `Scan` request lets you tell the AWS AppSync DynamoDB function to make a `Scan` request to DynamoDB, and enables you to specify the following:
- A filter to exclude results
- Which index to use
- How many items to return
- Whether to use consistent reads
- Pagination token
- Parallel scans
**Example:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Before you begin, you will need:

You can create an item by first generating the Data client with your backend Data schema. Then you can add an item:

```js
```ts
import { generateClient } from 'aws-amplify/data';
import { type Schema } from '../amplify/data/resource'

Expand Down Expand Up @@ -238,7 +238,7 @@ Future<void> createTodo() async {

To update the item, use the `update` function:

```js
```ts
import { generateClient } from 'aws-amplify/data';
import { type Schema } from '../amplify/data/resource';

Expand Down Expand Up @@ -395,7 +395,7 @@ Future<void> deleteTodoById(Todo todoToDelete) async {
You can cancel any mutation API request by calling `.cancel` on the mutation request promise that's returned by `.create(...)`, `.update(...)`, or `.delete(...)`.

```ts
const promise = client.models.Todo.create({ content: 'New Todo ' });
const promise = client.models.Todo.create({ content: 'New Todo' });
// ^ Note: we're not awaiting the request, we're returning the promise

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,53 +34,53 @@ Amplify GraphQL API uses a variety of auto-generated, underlying AWS services an

In your Amplify app, you can access every underlying resource using CDK ["L2"](https://docs.aws.amazon.com/cdk/v2/guide/constructs.html#constructs_using) or ["L1"](https://docs.aws.amazon.com/cdk/v2/guide/constructs.html#constructs_l1_using) constructs. Access the generated resources as L2 constructs via the `.resources` property on the returned stack or access the generated resources as L1 constructs using the `.resources.cfnResources` property.

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';

const backend = defineBackend({
data
});

const dataResources = backend.data.resources;
const { cfnResources } = backend.data.resources;

Object.values(dataResources.cfnResources.amplifyDynamoDbTables).forEach((table) => {
for (const table of Object.values(cfnResources.amplifyDynamoDbTables)) {
table.pointInTimeRecoveryEnabled = true;
});
}
```

## Customize Amplify-generated AppSync GraphQL API resources

Apply all the customizations on `backend.data.resources.graphqlApi` or `backend.data.resources.cfnResources.cfnGraphqlApi`. For example, to enable X-Ray tracing for the AppSync GraphQL API:

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';

const backend = defineBackend({
data
});

const dataResources = backend.data.resources;
const { cfnResources } = backend.data.resources;

dataResources.cfnResources.cfnGraphqlApi.xrayEnabled = true;
cfnResources.cfnGraphqlApi.xrayEnabled = true;
```

## Customize Amplify-generated resources for data models

Pass in the model type name into `backend.data.resources.amplifyDynamoDbTables["MODEL_NAME"]` to modify the resources generated for that particular model type. For example, to enable time-to-live on the Todo `@model` type's DynamoDB table:

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';

const backend = defineBackend({
data
});

const dataResources = backend.data.resources;
const { cfnResources } = backend.data.resources;

dataResources.cfnResources.amplifyDynamoDbTables["Todo"].timeToLiveAttribute = {
cfnResources.amplifyDynamoDbTables["Todo"].timeToLiveAttribute = {
attributeName: "ttl",
enabled: true,
};
Expand All @@ -90,34 +90,35 @@ dataResources.cfnResources.amplifyDynamoDbTables["Todo"].timeToLiveAttribute = {

Set the [DynamoDB billing mode](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-billingmode) for the DynamoDB table as either "PROVISIONED" or "PAY_PER_REQUEST".

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';
import { BillingMode } from "aws-cdk-lib/aws-dynamodb";
import { data } from './data/resource';

const backend = defineBackend({
data
});
const dataResources = backend.data.resources;

dataResources.cfnResources.amplifyDynamoDbTables['Todo'].billingMode = BillingMode.PAY_PER_REQUEST;
const { cfnResources } = backend.data.resources;

cfnResources.amplifyDynamoDbTables['Todo'].billingMode = BillingMode.PAY_PER_REQUEST;
```

### Example - Configure provisioned throughput for a DynamoDB table

Override the default [ProvisionedThroughput](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-dynamodb-table.html#cfn-dynamodb-table-provisionedthroughput) provisioned for each model table and its Global Secondary Indexes (GSI). This override is only valid if the "DynamoDBBillingMode" is set to "PROVISIONED".

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';

const backend = defineBackend({
data
});

const dataResources = backend.data.resources;
const { cfnResources } = backend.data.resources;

dataResources.cfnResources.amplifyDynamoDbTables["Todo"].provisionedThroughput = {
cfnResources.amplifyDynamoDbTables["Todo"].provisionedThroughput = {
readCapacityUnits: 5,
writeCapacityUnits: 5,
};
Expand All @@ -127,15 +128,15 @@ dataResources.cfnResources.amplifyDynamoDbTables["Todo"].provisionedThroughput =

Enable/disable [DynamoDB point-in-time recovery](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-dynamodb-table-pointintimerecoveryspecification.html) for each model table.

```ts
```ts title="amplify/backend.ts"
import { defineBackend } from '@aws-amplify/backend';
import { data } from './data/resource';

const backend = defineBackend({
data
});

const dataResources = backend.data.resources;
const { cfnResources } = backend.data.resources;

dataResources.cfnResources.amplifyDynamoDbTables['Todo'].pointInTimeRecoveryEnabled = true;
cfnResources.amplifyDynamoDbTables['Todo'].pointInTimeRecoveryEnabled = true;
```

0 comments on commit d593c37

Please sign in to comment.