#✅ - How to add both a Lambda function and a DynamoDB table in data/resource.ts?

4 messages · Page 1 of 1 (latest)

orchid lagoon
#

I want to define both a Lambda function and a DynamoDB table in an Amplify application. I wrote the following code in data/resource.ts, but when running the ampx sandbox command, an error is displayed. Is there something wrong with my approach?

data/resource.ts

import { type ClientSchema, a, defineData } from '@aws-amplify/backend';
import { getRss } from '../functions/get-rss/resource';

const schema = a.schema({
  getRss: a
    .query()
    .arguments({
      name: a.string(),
    })
    .returns(a.string())
    .authorization((allow) => [allow.guest()])
    .handler(a.handler.function(getRss)),
  Rss: a
    .model({
      title: a.string(),
      link: a.string(),
      ai_summary: a.string(),
      is_read: a.boolean(),
      is_deleted: a.boolean(),
      published_at: a.string(),
      updated_at: a.string(),
      fetched_at: a.string(),
    })
    .authorization((allow) => [allow.authenticated()]),
});

export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: 'iam',
  },
});

Error:

Failed to instantiate data construct
Caused By: Object type extension 'Query' cannot redeclare field getRss

Resolution: See the underlying error message for more details.

I saw that this error is resolved when I removed Rss schema in a.schema({}).

keen brook
#

I believe getRss is invisibly created for you when you have a model named Rss in your schema. You can change your getRss schema name to something else (maybe fetchRss?) Or, if you don't acutally need the function you can just use the getRss provided by the model.

orchid lagoon
#

Renaming getRss to fetchRss worked fine! Thank you!

getRss is invisibly created for you when you have a model named Rss
I didn't know this...

vocal wharfBOT
#

✅ - How to add both a Lambda function and a DynamoDB table in data/resource.ts?