aws-amplify / aws-amplify/amplify-data

Add support for custom index on hasMany / expose a list query on the secondary index created by hasMany

Open
#439 5 comments 0 reactions 1 assignee Claimed by @chrisbonifacio View on GitHub
data-schema feature-request Gen 2 transferred
Dominant language
TypeScript
Stars
18
Forks
23
Avg merge
26m
Merged PRs (30d)
1

Description

### Environment information

```plain text
System:
OS: macOS 14.0
CPU: (8) arm64 Apple M1 Pro
Memory: 128.48 MB / 16.00 GB
Shell: /bin/zsh
Binaries:
Node: 22.3.0 - ~/.nvm/versions/node/v22.3.0/bin/node
Yarn: undefined - undefined
npm: 10.8.1 - ~/.nvm/versions/node/v22.3.0/bin/npm
pnpm: 8.9.0 - ~/.npm-packages/bin/pnpm
NPM Packages:
@aws-amplify/auth-construct: Not Found
@aws-amplify/backend: 1.0.4
@aws-amplify/backend-auth: Not Found
@aws-amplify/backend-cli: 1.2.2
@aws-amplify/backend-data: Not Found
@aws-amplify/backend-deployer: Not Found
@aws-amplify/backend-function: Not Found
@aws-amplify/backend-output-schemas: Not Found
@aws-amplify/backend-output-storage: Not Found
@aws-amplify/backend-secret: Not Found
@aws-amplify/backend-storage: Not Found
@aws-amplify/cli-core: Not Found
@aws-amplify/client-config: Not Found
@aws-amplify/deployed-backend-client: Not Found
@aws-amplify/form-generator: Not Found
@aws-amplify/model-generator: Not Found
@aws-amplify/platform-core: Not Found
@aws-amplify/plugin-types: Not Found
@aws-amplify/sandbox: Not Found
@aws-amplify/schema-generator: Not Found
aws-amplify: 6.4.4
aws-cdk: 2.151.0
aws-cdk-lib: 2.151.0
typescript: 5.5.3
AWS environment variables:
AWS_STS_REGIONAL_ENDPOINTS = regional
AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
AWS_SDK_LOAD_CONFIG = 1
No CDK environment variables
npm notice
npm notice New patch version of npm available! 10.8.1 -> 10.8.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.2
npm notice To update run: npm install -g npm@10.8.2
npm notice
npm notice
npm notice New patch version of npm available! 10.8.1 -> 10.8.2
npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.8.2
npm notice To update run: npm install -g npm@10.8.2
npm notice
```

### Description

In this version, hasMany will create a secondary index for internal queries but:
- the secondary index dose not expose a list query in AppSync and subsequently in the client.
- we can't assign a user-created secondary index to hasMany (as in v1)

I find this important for the case where we need to query the secondary index to find children by "parentId" given that I have the parent id.

Let's assume we have this:
```
const schema = a.schema({
Todo: a
.model({
content: a.string(),
description: a.string(),
comments: a.hasMany("Comment", "parentId"),
})
.authorization((allow) => [allow.guest()]),

Comment: a
.model({
content: a.string(),
parentId: a.string().required(),
todo: a.belongsTo("Todo", "parentId"),
})
.authorization((allow) => [allow.guest()]),
});
```

if i need to load the todo and the comments, i can build an efficient query with selectors:

```
const { data: todos, errors } = await client.models.Todo.list({
selectionSet: [
"id",
"content",
"description",
"comments.*",
],
});
```

but i can't have a proper way to get the Comments by todoId.

The only thing I found was **lazy loading the Comments** by passing the todo:

``` const { data: comments } = await todo.comments() ```

This will work in POCs and small projects, but **it uses a filter instead of secondary index** to get data so it will scan the table each time.

If we crete a manual secondary index, we double the index storage and we also count towards the max number of indexes per table.

If we can create a secondary index and declare it for usage in hasMany, all the above would be solved.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.