How can I generate multiple entities in a from a single request using Amplify?

Amplify allows the creation of entities through generated code. I’d like to harness this to create several entities at once.

When a user creates an entity, say Event,

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const Event = a
.model({
id: a.id().required(),
title: a.string(),
eventOwnerId: a.id().required(),
eventOwner: a.belongsTo("EventOwner", "eventOwnerId"),
...
})
.authorization((allow) => [allow.owner()]);
</code>
<code>const Event = a .model({ id: a.id().required(), title: a.string(), eventOwnerId: a.id().required(), eventOwner: a.belongsTo("EventOwner", "eventOwnerId"), ... }) .authorization((allow) => [allow.owner()]); </code>
const Event = a
  .model({
    id: a.id().required(),
    title: a.string(),
    eventOwnerId: a.id().required(),
    eventOwner: a.belongsTo("EventOwner", "eventOwnerId"),
    ...
  })
  .authorization((allow) => [allow.owner()]);

where

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const EventOwner = a
.model({
id: a.id().required(),
userId: a.id().required(), // should be associated with a user
eventId: a.id().required(),
events: a.hasMany("Event", "eventOwnerId"),
})
.authorization((allow) => [allow.owner()]);,
</code>
<code>const EventOwner = a .model({ id: a.id().required(), userId: a.id().required(), // should be associated with a user eventId: a.id().required(), events: a.hasMany("Event", "eventOwnerId"), }) .authorization((allow) => [allow.owner()]);, </code>
const EventOwner = a
  .model({
    id: a.id().required(),
    userId: a.id().required(), // should be associated with a user
    eventId: a.id().required(),
    events: a.hasMany("Event", "eventOwnerId"),
  })
  .authorization((allow) => [allow.owner()]);,

a given property on that event, say eventOwner should be automatically populated through the following process.

  • If an EventOwner is associated with the user who submitted the createEvent mutation, the created event will automatically be associated via that EventOwner via its eventOwnerId.
  • If an EventOwner is not associated with the user who submitted the createEvent mutation, the created event will automatically generate a EventOwner entity. And, that EventOwner should automatically be associated with the newly created Event.

I believe this should be accomplished through a lambda function. So, I believe should have

  • an amplify/functions/pre-event-creation/resource.ts with `import { defineFunction } from “@aws-amplify/backend”;
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export const preEventCreation = defineFunction({name: "pre-event-creation",});`
</code>
<code>export const preEventCreation = defineFunction({name: "pre-event-creation",});` </code>
export const preEventCreation = defineFunction({name: "pre-event-creation",});`
  • and an amplify/functions/pre-event-creation/handler.ts, which I suspect should look something like:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import type { PostConfirmationTriggerHandler } from "aws-lambda";
import { type Schema } from "../../data/schema";
import { preEventCreation } from "./resource";
import { env } from "$amplify/env/post-confirmation";
Amplify.configure(
{
API: {
GraphQL: {
endpoint: env.AMPLIFY_DATA_GRAPHQL_ENDPOINT,
region: env.AWS_REGION,
defaultAuthMode: "iam",
},
},
},
{
Auth: {
credentialsProvider: {
getCredentialsAndIdentityId: async () => ({
credentials: {
accessKeyId: env.AWS_ACCESS_KEY_ID,
secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
sessionToken: env.AWS_SESSION_TOKEN,
},
}),
clearCredentialsAndIdentityId: () => {
/* noop */
},
},
},
}
);
const client = generateClient<Schema>({
authMode: "iam",
});
export const handler: PostConfirmationTriggerHandler = async (event) => {
await client.graphql({
query: preEventCreation,
variables: {
input: {
email: event.request.userAttributes.email,
profileOwner: `${event.request.userAttributes.sub}::${event.userName}`,
},
},
});
return event;
};
</code>
<code>import { Amplify } from "aws-amplify"; import { generateClient } from "aws-amplify/data"; import type { PostConfirmationTriggerHandler } from "aws-lambda"; import { type Schema } from "../../data/schema"; import { preEventCreation } from "./resource"; import { env } from "$amplify/env/post-confirmation"; Amplify.configure( { API: { GraphQL: { endpoint: env.AMPLIFY_DATA_GRAPHQL_ENDPOINT, region: env.AWS_REGION, defaultAuthMode: "iam", }, }, }, { Auth: { credentialsProvider: { getCredentialsAndIdentityId: async () => ({ credentials: { accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY, sessionToken: env.AWS_SESSION_TOKEN, }, }), clearCredentialsAndIdentityId: () => { /* noop */ }, }, }, } ); const client = generateClient<Schema>({ authMode: "iam", }); export const handler: PostConfirmationTriggerHandler = async (event) => { await client.graphql({ query: preEventCreation, variables: { input: { email: event.request.userAttributes.email, profileOwner: `${event.request.userAttributes.sub}::${event.userName}`, }, }, }); return event; }; </code>
import { Amplify } from "aws-amplify";
import { generateClient } from "aws-amplify/data";
import type { PostConfirmationTriggerHandler } from "aws-lambda";
import { type Schema } from "../../data/schema";
import { preEventCreation } from "./resource";
import { env } from "$amplify/env/post-confirmation";

Amplify.configure(
  {
    API: {
      GraphQL: {
        endpoint: env.AMPLIFY_DATA_GRAPHQL_ENDPOINT,
        region: env.AWS_REGION,
        defaultAuthMode: "iam",
      },
    },
  },
  {
    Auth: {
      credentialsProvider: {
        getCredentialsAndIdentityId: async () => ({
          credentials: {
            accessKeyId: env.AWS_ACCESS_KEY_ID,
            secretAccessKey: env.AWS_SECRET_ACCESS_KEY,
            sessionToken: env.AWS_SESSION_TOKEN,
          },
        }),
        clearCredentialsAndIdentityId: () => {
          /* noop */
        },
      },
    },
  }
);

const client = generateClient<Schema>({
  authMode: "iam",
});

export const handler: PostConfirmationTriggerHandler = async (event) => {
  await client.graphql({
    query: preEventCreation,
    variables: {
      input: {
        email: event.request.userAttributes.email,
        profileOwner: `${event.request.userAttributes.sub}::${event.userName}`,
      },
    },
  });

  return event;
};

Specifically, I’d like to know what to add to the handler. Thanks in advance!

I’ve tried variations on the PostConfirmationTriggerHandler from the amplify docs, but haven’t made much headway.

New contributor

user25441048 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật