aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.44k stars 2.13k forks source link

Add sortkey to the join table created under the hood by @manyToMany directive #9914

Open jeff-ong opened 2 years ago

jeff-ong commented 2 years ago

Is this related to a new or existing framework?

No response

Is this related to a new or existing API?

GraphQL API

Is this related to another service?

DynamoDB, GraphQL

Describe the feature you'd like to request

Currently, the @manyToMany directive creates a join table under the hood but it is not letting user specify a sortKey on the join table index in schema.graphql.

type Post @model {
  id: ID!
  title: String!
  content: String
  tags: [Tag] @manyToMany(relationName: "PostTags")
}

type Tag @model {
  id: ID!
  label: String!
  posts: [Post] @manyToMany(relationName: "PostTags")
}

The schema above creates the PostTags table under the hood but is there currently a way to add a sort key to the index created in the join table.

Describe the solution you'd like

Allow user to customize the partition and sort key for join table in schema.graphql

Describe alternatives you've considered

Allow user to prevent schema.graphql from overriding manual changes in DynamoDB resources.

Additional context

No response

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

camin-mccluskey commented 2 years ago

+1 to this feature being more explicit. I'm not sure if this answer will solve your problem @jeff-ong -https://stackoverflow.com/questions/70550775/how-to-create-an-amplify-datastore-many-to-many-schema-in-version-3-7-3

I am attempting to utilise this to explicitly model the junction table created by @manyToMany directive but it's unclear whether this is the correct pattern as, in the answer, the junction table is referenced in the directive, as opposed to being implicitly created via the relationName argument

camin-mccluskey commented 2 years ago

In fact, it's in the documentation that you must continue to use @hasMany with @belongsTo

Note: if you want to store additional properties on the join table, or if you have an existing join table, you can continue to use two @hasMany <=> @belongsTo relationships to facilitate a many-to-many relationship.

Source: https://docs.amplify.aws/cli/migration/transformer-migration/#relationship-mappingconnection-changes

judygab commented 1 year ago

has anyone figured this out? @camin-mccluskey so hasMany from both sides is a way to go if not?

camin-mccluskey commented 1 year ago

@judygab I used @hasMany with @belongsTo but it's been a while since I worked on this (or any) Amplify project so I've somewhat lost context