import type { InputContext } from "@webiny/api-kernel-core"; import { AwsLambdaContext } from "@webiny/api-kernel-aws/abstractions/AwsLambdaContext.js"; import { SqlSchemaSetupInputHandler, SqlSchemaSetupService, SQL_SCHEMA_SETUP_EVENT_IDENTIFIER, type ISqlSchemaSetupEvent, type ISqlSchemaSetupResult } from "./abstractions.js"; /** * Handles the schema-setup event: reads the service manifests from the event and hands them to * `SqlSchemaSetupService`, which does the setup. It returns what the setup did, and throws when a * step fails, so the deploy fails with it. On Lambda it tells the service when the function will be * stopped, so the setup can fail with its own error before then. */ class SetupSqlSchemaHandlerImpl implements SqlSchemaSetupInputHandler.Interface { public constructor( private readonly service: SqlSchemaSetupService.Interface, private readonly lambdaContext: AwsLambdaContext.Interface ) {} public async execute(ctx: InputContext): Promise { const serviceManifests = ctx.input[SQL_SCHEMA_SETUP_EVENT_IDENTIFIER].serviceManifests ?? []; if (!Array.isArray(serviceManifests)) { throw new Error("`serviceManifests` must be a list of { name, manifest }."); } const deadline = this.lambdaContext.isSet() ? Date.now() + this.lambdaContext.get().getRemainingTimeInMillis() : undefined; const done = await this.service.setup({ serviceManifests, deadline }); return { ok: true, done }; } } export const SetupSqlSchemaHandler = SqlSchemaSetupInputHandler.createImplementation({ implementation: SetupSqlSchemaHandlerImpl, dependencies: [SqlSchemaSetupService, AwsLambdaContext] });