Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cbonif/edits-to-secondary-indexes-page #7179

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ export function getStaticProps(context) {

You can optimize your list queries based on "secondary indexes". For example, if you have a **Customer** model, you can query based on the customer's **id** identifier field by default but you can add a secondary index based on the **accountRepresentativeId** to get list customers for a given account representative.

A secondary index consists of a "hash key" and, optionally, a "sort key". Use the "hash key" to perform strict equality and the "sort key" for greater than (gt), greater than or equal to (ge), less than (lt), less than or equal to (le), equals (eq), begins with, and between operations.
A secondary index consists of a "hash key" and, optionally, a "sort key". Use the "hash key" to perform strict equality and the "sort key" for greater than (gt), greater than or equal to (ge), less than (lt), less than or equal to (le), equals (eq), begins with, and between operations.

```ts
export const schema = a.schema({
Customer: a.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required()
// highlight-start
}).secondaryIndexes(index => [
index('accountRepresentativeId')
])
// highlight-end
.authorization([a.allow.public()]),
Customer: a
.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
})
// highlight-next-line
.secondaryIndexes((index) => [index("accountRepresentativeId")])
.authorization([a.allow.public()]),
});
```

Expand All @@ -39,40 +38,41 @@ import { generateClient } from 'aws-amplify/data';

const client = generateClient<Schema>();

const {
data,
errors
const { data, errors } =
// highlight-start
} = await client.models.Customer.listByAccountRepresentativeId({
accountRepresentativeId: "YOUR_REP_ID"
});
// highlight-end
await client.models.Customer.listByAccountRepresentativeId({
accountRepresentativeId: "YOUR_REP_ID",
});
// highlight-end
```

<Accordion title="Review how this works under the hood with Amazon DynamoDB">

Amplify uses Amazon DynamoDB tables as the default data source for `a.model()`. For key-value databases, it is critical to model your access patterns with "secondary indexes". Use the `.index()` modifier to configure a secondary index.
Amplify uses Amazon DynamoDB tables as the default data source for `a.model()`. For key-value databases, it is critical to model your access patterns with "secondary indexes". Use the `.index()` modifier to configure a secondary index.

**Amazon DynamoDB** is a key-value and document database that delivers single-digit millisecond performance at any scale but making it work for your access patterns requires a bit of forethought. DynamoDB query operations may use at most two attributes to efficiently query data. The first query argument passed to a query (the hash key) must use strict equality and the second attribute (the sort key) may use gt, ge, lt, le, eq, beginsWith, and between. DynamoDB can effectively implement a wide variety of access patterns that are powerful enough for the majority of applications.

</Accordion>

## Add sort keys to secondary indexes

You can define "sort keys" to add a set of flexible filters to your query, such as "greater than" (gt), "greater than or equal to" (ge), "less than" (lt), "less than or equal to" (le), "equals" (eq), "begins with" (beginsWith), and "between" operations.
You can define "sort keys" to add a set of flexible filters to your query, such as "greater than" (gt), "greater than or equal to" (ge), "less than" (lt), "less than or equal to" (le), "equals" (eq), "begins with" (beginsWith), and "between" operations.

```ts title="amplify/data/resource.ts"
const schema = a.schema({
Customer: a.model({
name: a.string(),
phoneNumber: a.phone().required(),
accountRepresentativeId: a.id().required(),
}).secondaryIndexes(index => [
index('accountRepresentativeId')
// highlight-next-line
.sortKeys(["name"]),
])
}).authorization([a.allow.owner()]);
export const schema = a.schema({
Customer: a
.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
})
.secondaryIndexes((index) => [
index("accountRepresentativeId")
// highlight-next-line
.sortKeys(["name"]),
])
.authorization([a.allow.owner()]),
});
```

On the client side, you should find a new `listBy...` query that's named after hash key and sort keys. For example, in this case: `listByAccountRepresentativeIdAndName`. You can supply the filter as part of this new list query:
Expand All @@ -81,13 +81,14 @@ On the client side, you should find a new `listBy...` query that's named after h
const {
data,
errors
// highlight-next-line
} = await client.models.Customer.listByAccountRepresentativeIdAndName({
accountRepresentativeId: 'YOUR_REP_ID',
name: {
beginsWith: 'Rene',
},
});
} =
// highlight-next-line
await client.models.Customer.listByAccountRepresentativeIdAndName({
accountRepresentativeId: 'YOUR_REP_ID',
name: {
beginsWith: 'Rene',
},
});
```

## Customize the query field for secondary indexes
Expand All @@ -96,16 +97,19 @@ You can also customize the auto-generated query name under `client.models.<MODEL

```ts title="amplify/data/resource.ts"
const schema = a.schema({
Customer: a.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
}).secondaryIndexes(index => [
index('accountRepresentativeId')
// highlight-next-line
.queryField("listByRep"),
]),
}).authorization([a.allow.owner()]);
Customer: a
.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
})
.secondaryIndexes((index) => [
index("accountRepresentativeId")
// highlight-next-line
.queryField("listByRep"),
])
.authorization([a.allow.owner()]),
});
```

In your client app code, you'll see query updated under the Data client:
Expand All @@ -125,15 +129,19 @@ const {
To customize the underlying DynamoDB's index name, you can optionally provide the `name()` modifier.

```ts
const schema = a.schema({
Customer: a.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
}).secondaryIndexes(index => [
index('accountRepresentativeId')
// highlight-next-line
.name("MyCustomIndexName"),
])
}).authorization([a.allow.owner()]);
const schema = a
.schema({
Customer: a
.model({
name: a.string(),
phoneNumber: a.phone(),
accountRepresentativeId: a.id().required(),
})
.secondaryIndexes((index) => [
index("accountRepresentativeId")
// highlight-next-line
.name("MyCustomIndexName"),
]),
})
.authorization([a.allow.owner()]);
chrisbonifacio marked this conversation as resolved.
Show resolved Hide resolved
```
Loading