Skip to content

Commit d303dc2

Browse files
authored
Amplify Flutter Secondary Index Query Documentation (#8384)
added documentation for flutter querying by secondary indexes in the data category
1 parent b3919cb commit d303dc2

File tree

1 file changed

+82
-4
lines changed
  • src/pages/[platform]/build-a-backend/data/data-modeling/secondary-index

1 file changed

+82
-4
lines changed

src/pages/[platform]/build-a-backend/data/data-modeling/secondary-index/index.mdx

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getCustomStaticPath } from '@/utils/getCustomStaticPath';
2-
2+
// cspell:ignore ACCOUNTREPRESENTATIVEID
33
export const meta = {
44
title: 'Customize secondary indexes',
55
description:
@@ -48,7 +48,7 @@ export const schema = a.schema({
4848
});
4949
```
5050

51-
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
51+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android"]}>
5252

5353
The example client query below allows you to query for "Customer" records based on their `accountRepresentativeId`:
5454

@@ -104,6 +104,27 @@ let queriedCustomers = try await Amplify.API.query(
104104
```
105105

106106
</InlineFilter>
107+
108+
<InlineFilter filters={["flutter"]}>
109+
110+
The example client query below allows you to query for "Customer" records based on their `accountRepresentativeId`:
111+
112+
```dart title="lib/main.dart"
113+
import 'package:amplify_api/amplify_api.dart';
114+
import 'package:amplify_flutter/amplify_flutter.dart';
115+
import 'models/ModelProvider.dart';
116+
117+
// highlight-start
118+
final request = ModelQueries.list(
119+
Customer.classType,
120+
where: Customer.ACCOUNTREPRESENTATIVEID.eq(YOUR_REP_ID),
121+
);
122+
// highlight-end
123+
124+
```
125+
126+
</InlineFilter>
127+
107128
<Accordion title="Review how this works under the hood with Amazon DynamoDB">
108129

109130
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 `.secondaryIndexes()` modifier to configure a secondary index.
@@ -133,7 +154,7 @@ export const schema = a.schema({
133154
});
134155
```
135156

136-
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
157+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android",]}>
137158

138159
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:
139160

@@ -185,6 +206,26 @@ let queriedCustomers = try await Amplify.API.query(
185206
```
186207
</InlineFilter>
187208

209+
<InlineFilter filters={["flutter"]}>
210+
211+
The example client query below allows you to query for "Customer" records based on their `name` AND their `accountRepresentativeId`:
212+
213+
```dart title="lib/main.dart"
214+
import 'package:amplify_api/amplify_api.dart';
215+
import 'package:amplify_flutter/amplify_flutter.dart';
216+
import 'models/ModelProvider.dart';
217+
218+
// highlight-start
219+
final request = ModelQueries.list(
220+
Customer.classType,
221+
where: Customer.ACCOUNTREPRESENTATIVEID.eq(YOUR_REP_ID) & Customer.NAME.beginsWith("Rene"),
222+
);
223+
// highlight-end
224+
225+
```
226+
227+
</InlineFilter>
228+
188229
## Customize the query field for secondary indexes
189230

190231
You can also customize the auto-generated query name under `client.models.<MODEL_NAME>.listBy...` by setting the `queryField()` modifier.
@@ -206,7 +247,7 @@ const schema = a.schema({
206247
});
207248
```
208249

209-
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android", "flutter"]}>
250+
<InlineFilter filters={["javascript", "angular", "react-native", "react", "nextjs", "vue", "android"]}>
210251

211252
In your client app code, you'll see query updated under the Data client:
212253

@@ -258,6 +299,43 @@ let queriedCustomers = try await Amplify.API.query(
258299
```
259300
</InlineFilter>
260301

302+
<InlineFilter filters={["flutter"]}>
303+
304+
In your client app code, you can use the updated query name.
305+
306+
```dart title="lib/main.dart"
307+
import 'package:amplify_api/amplify_api.dart';
308+
import 'package:amplify_flutter/amplify_flutter.dart';
309+
import 'models/ModelProvider.dart';
310+
311+
var accountRepresentativeId = "John";
312+
var operationName = "listByRep";
313+
var document = """
314+
query ListByRep {
315+
$operationName(accountRepresentativeId: "$accountRepresentativeId") {
316+
items {
317+
accountRepresentativeId
318+
createdAt
319+
id
320+
name
321+
phoneNumber
322+
updatedAt
323+
}
324+
nextToken
325+
}
326+
}
327+
""";
328+
329+
final request = GraphQLRequest(
330+
document: document,
331+
variables: {
332+
'accountRepresentativeId': accountRepresentativeId,
333+
'operationName': operationName,
334+
},
335+
);
336+
```
337+
</InlineFilter>
338+
261339

262340
## Customize the name of secondary indexes
263341

0 commit comments

Comments
 (0)