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

Support @hasMany configures a default secondary index in gen2 #2312

Closed
2 tasks
sevenkkk opened this issue Mar 1, 2024 · 7 comments
Closed
2 tasks

Support @hasMany configures a default secondary index in gen2 #2312

sevenkkk opened this issue Mar 1, 2024 · 7 comments
Assignees
Labels

Comments

@sevenkkk
Copy link

sevenkkk commented Mar 1, 2024

Describe the feature you'd like to request

This is the implementation of v6:

type Post @model {
  id: ID!
  title: String!
  comments: [Comment] @hasMany(indexName: "byPost", fields: ["id"])
}

type Comment @model {
  id: ID!
  postID: ID! @index(name: "byPost", sortKeyFields: ["content"])
  content: String!
}

Use a.hasMany() in gen2:

Post: a
   .model({
     title: a.string().required(),
     comments: a.hasMany('Comment'),
   }),
 Comment: a
   .model({
     content: a.string(),
     postID: a.id(),
   })
   .secondaryIndexes([
     a.index('postID').name('byPost').sortKeys(['content']).queryField('commentsByPostIDByContent'),
   ]),

I want to implement the default use of the default index for byPost

Describe the solution you'd like

Support associated attributes

Describe alternatives you've considered

no

Additional context

No response

Is this something that you'd be interested in working on?

  • 👋 I may be able to implement this feature request

Would this feature include a breaking change?

  • ⚠️ This feature might incur a breaking change
@AnilMaktala
Copy link
Member

Hey @sevenkkk, Thank you for your request. We will label this as a feature request for the team to review in more detail.

@AnilMaktala AnilMaktala added feature-request New feature or request and removed pending-triage labels Mar 1, 2024
@renebrandel
Copy link
Contributor

Hi @sevenkkk - we're actually rethinking relationships all up in our Gen 2 experience. There are a few edits that we want to apply as part of the workflow:

  • for hasOne/belongsTo relationships, we'll move the source of truth to a single table rather than distributed throughout two tables
  • all relationsihps (hasOne, hasMany, and manyToMany) will allow you to query from both sides of the relationship i.e. we'll enforce belongsTo as a requirement.
  • all updates to the relationship will happen through the related model (i.e. where the belongsTo is setup)

while we're making these updates, I wanted to clarify your use case more. Is it primarily the fact that you want to setup multiple relationships between the same models and/or want to customize the field / index in which the relationship info is stored?

@sevenkkk
Copy link
Author

sevenkkk commented Mar 5, 2024

Hi @renebrandel - I have a table that needs to describe the management relationship between fathers and sons, and here is the model I have defined:

GenreTag: a
    .model({
      code: a.string().required(),
      name: a.string().required(),
      order: a.string().required(),
      displayFields: a.string().array(),
      isEnabled: a.boolean().default(true),
      tags: a.hasMany('GenreTag'),
    })
    .secondaryIndexes([
      a.index('genreTagTagsId').sortKeys(['order']).queryField('genreTagsByParentIdByOrder'),
    ])
截屏2024-03-05 14 14 12

My requirement is to retrieve categories starting from the root and for each level, sort them based on the order field.
Client requests to retrieve categories, where ROOT represents the top level:

const res = await userPoolClient.models.GenreTag.genreTagsByParentIdByOrder({
			genreTagTagsId: 'ROOT',
		});
		if (res) {
			const tags = await res.data[0].tags();
		}

When calling tags(), I understand that it's using the gsi-GenreTag.tags index, which doesn't provide the desired sorting. Is it possible to use a custom index, such as genreTagTagsId, for sorting purposes?

@renebrandel
Copy link
Contributor

Hi @sevenkkk - we've got a task on our backlog to support sort direction for the sortKey fields. So yes, you are on the right track with using secondary indexes.

The sort direction on the sort keys is available in the underlying GraphQL API but not exposed to the client code client.models..... You can work around this limitation for now by generating the raw GraphQL queries / mutations; then calling client.graphql(...).

@sevenkkk
Copy link
Author

sevenkkk commented Mar 6, 2024

Hi~ @renebrandel -
Yes, the current solution is to call client.graphql(...) to control the direction and wait for the client to support sortKey.

Also, I think binding @hasmany to a custom index is another topic. For getting "Has Many" relationships you can follow a custom index, I'm not sure if this is planned (including client-side).
Or is there any other plan to solve my above problem (without using client.graphql(...))

@AnilMaktala
Copy link
Member

Hey @sevenkkk, The latest version now includes support for sorting in custom indexes. Please verify in the latest version and let us know.

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants