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

using owner in an identifier #3105

Open
mainvtm opened this issue Jan 4, 2025 · 3 comments
Open

using owner in an identifier #3105

mainvtm opened this issue Jan 4, 2025 · 3 comments
Labels
Gen 2 pending-maintainer-response Issue is pending a response from the Amplify team. question Further information is requested transferred

Comments

@mainvtm
Copy link

mainvtm commented Jan 4, 2025

Environment information

System:
  OS: Windows 10 10.0.19045
  CPU: (8) x64 Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
  Memory: 17.07 GB / 31.96 GB
Binaries:
  Node: 22.12.0 - C:\Program Files\nodejs\node.EXE
  Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
  npm: 9.6.4 - C:\Program Files\nodejs\npm.CMD
  pnpm: undefined - undefined
NPM Packages:
  @aws-amplify/auth-construct: 1.3.2
  @aws-amplify/backend: 1.5.1
  @aws-amplify/backend-auth: 1.2.0
  @aws-amplify/backend-cli: 1.3.0
  @aws-amplify/backend-data: 1.1.5
  @aws-amplify/backend-deployer: 1.1.5
  @aws-amplify/backend-function: 1.7.1
  @aws-amplify/backend-output-schemas: 1.4.0
  @aws-amplify/backend-output-storage: 1.1.2
  @aws-amplify/backend-secret: 1.1.4
  @aws-amplify/backend-storage: 1.2.1
  @aws-amplify/cli-core: 1.1.3
  @aws-amplify/client-config: 1.5.0
  @aws-amplify/deployed-backend-client: 1.4.2
  @aws-amplify/form-generator: 1.0.3
  @aws-amplify/model-generator: 1.0.8
  @aws-amplify/platform-core: 1.1.0
  @aws-amplify/plugin-types: 1.3.0
  @aws-amplify/sandbox: 1.2.3
  @aws-amplify/schema-generator: 1.2.4
  aws-amplify: 6.6.6
  aws-cdk: 2.163.1
  aws-cdk-lib: 2.163.1
  typescript: 5.6.3
AWS environment variables:
  AWS_NODEJS_CONNECTION_REUSE_ENABLED = 1
  AWS_SDK_LOAD_CONFIG = 1
  AWS_STS_REGIONAL_ENDPOINTS = regional
No CDK environment variables

Describe the bug

I'm trying to use identifier to ensure that each user only adds an email address once (email is unique per owner).

Reproduction steps

If I leave it as optional (as per the documentation), i can't use owner in the identifier

const schema = a.schema({
  Contact: a
    .model({
      firstName: a.string().required(),
      lastName: a.string().required(),
      email: a.string().required(),
      owner: a
        .string()
        .authorization((allow) => [allow.owner().to(['read'])]),
      address: a.string().required(),
    })
    .authorization((allow) => [
      allow.owner().to(['create', 'read', 'update', 'delete']),
    ])
    .identifier(['owner', 'email']), // error: Type '"owner"' is not assignable to type '"firstName" | "lastName" | "email" | "address"'
)

However if set owner as required, I cannot create rows as I wouild need to supply owner.

const schema = a.schema({
  Contact: a
    .model({
      firstName: a.string().required(),
      lastName: a.string().required(),
      email: a.string().required(),
      owner: a
        .string()
        .required()
        .authorization((allow) => [allow.owner().to(['read'])]),
      address: a.string().required(),
    })
    .authorization((allow) => [
      allow.owner().to(['create', 'read', 'update', 'delete']),
    ])
    .identifier(['owner', 'email']),
)

/*...*/

const contact: any = {
  firstName: "jack",
  lastName: "jones",
  email: "[email protected]",
  address: "lonely",
};

const response = await client.models.Contact.create(contact);
// error: "Variable 'input' has coerced Null value for NonNull type 'String!'

/*...*/

const contact: any = {
  owner: auth.user.userId,
  firstName: "jack",
  lastName: "jones",
  email: "[email protected]",
  address: "lonely",
};

const response = await client.models.Contact.create(contact);
//  Unauthorized on [owner]
@ykethan
Copy link
Member

ykethan commented Jan 6, 2025

Hey,👋 thanks for raising this! I'm going to transfer this over to our API repository for better assistance.

@ykethan ykethan transferred this issue from aws-amplify/amplify-backend Jan 6, 2025
@phani-srikar
Copy link
Contributor

Hi @mainvtm, we do not recommend using the owner field as part of the primary key partly for the reasons you've outlined in this issue. In order to meet your use-case ensure that each user only adds an email address once (email is unique per owner)., you can setup a field level Auth rule such that only the owner can create the email once and not be able to update it as shown below:

const schema = a.schema({
  Contact: a
    .model({
      firstName: a.string().required(),
      lastName: a.string().required(),
      email: a.string().required().authorization((allow) => [
      allow.owner().to(['create', 'read']),
    ]),
      owner: a
        .string()
        .authorization((allow) => [allow.owner().to(['read'])]),
      address: a.string().required(),
    })
    .authorization((allow) => [
      allow.owner().to(['create', 'read', 'update', 'delete']),
    ])
    .identifier(['email'])
)

@phani-srikar phani-srikar removed their assignment Jan 6, 2025
@AnilMaktala AnilMaktala added question Further information is requested pending-community-response Issue is pending a response from the author or community. and removed pending-triage labels Jan 7, 2025
@mainvtm
Copy link
Author

mainvtm commented Jan 22, 2025

Hi @phani-srikar

This doesn't solve the requirement of having unique owner-email pairs (different owners could not create a contact with the same email?). I achieved this with a secondary index, but this feels like it should be possible with the primary index.

 Contact: a
    .model({
      id: a.id().required(),
      owner: a.string().authorization((allow) => [allow.owner().to(['read', 'delete'])]),
      firstName: a.string().required(),
      lastName: a.string().required(),
      email: a.string().required(),
      address: a.string(),
    })
    .authorization(allow => [allow.owner()]),
    ])
    .secondaryIndexes((indexes) => [indexes('owner').sortKeys(['email'])]),

@github-actions github-actions bot added pending-maintainer-response Issue is pending a response from the Amplify team. and removed pending-community-response Issue is pending a response from the author or community. labels Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Gen 2 pending-maintainer-response Issue is pending a response from the Amplify team. question Further information is requested transferred
Projects
None yet
Development

No branches or pull requests

4 participants