Migrate serverless function service to v2 (#17285)

# Introduction

In this PR we're migrating the serverless function service that was
using the SF repo directly to the v2 build and runner.
The whole serverless engine now deals with flat entities only

## Resolvers
Refactored the resolvers ( serverlessFunction, route, database and cron
trigger) :
- return types to `dto`
- Standardized the flat to dto transpilation within the resolvers
- Find and findMany passing by the cached data

## Services
Refactored the services ( serverlessFunction, route, database and cron
trigger) :
- return type to be `flat`
- always calling v2 and computing cache

## New additional caches
- application variables ( cf
https://github.com/twentyhq/core-team-issues/issues/2116 )
- serverless function layer

## What to test:
- CRUD ( database trigger  , route trigger, cron trigger, serverless
function through workflows  )
- Duplicating a workflow with a serverless function code node  

## Concerns
We need to implement the cron that will hard delete soft deleted s3
serverless functions, not in this PR though ( cf
https://github.com/twentyhq/twenty/pull/17285#discussion_r2709168570 and
https://github.com/twentyhq/core-team-issues/issues/2118 )
This commit is contained in:
Paul Rastoin
2026-01-20 19:32:22 +01:00
committed by GitHub
parent ccf2eae684
commit e35b4b86cb
50 changed files with 1415 additions and 505 deletions
@@ -8,7 +8,7 @@ import { WorkspaceMigrationRunnerActionHandler } from 'src/engine/workspace-mana
import { FileStorageService } from 'src/engine/core-modules/file-storage/file-storage.service';
import { getBaseTypescriptProjectFiles } from 'src/engine/core-modules/serverless/drivers/utils/get-base-typescript-project-files';
import { getServerlessFolder } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { getServerlessFolderOrThrow } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { ServerlessFunctionEntity } from 'src/engine/metadata-modules/serverless-function/serverless-function.entity';
import { CreateServerlessFunctionAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/serverless-function/types/workspace-migration-serverless-function-action.type';
import { WorkspaceMigrationActionRunnerArgs } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/types/workspace-migration-action-runner-args.type';
@@ -38,8 +38,8 @@ export class CreateServerlessFunctionActionHandlerService extends WorkspaceMigra
workspaceId,
});
const draftFileFolder = getServerlessFolder({
serverlessFunction,
const draftFileFolder = getServerlessFolderOrThrow({
flatServerlessFunction: serverlessFunction,
version: 'draft',
});
@@ -66,8 +66,8 @@ export class CreateServerlessFunctionActionHandlerService extends WorkspaceMigra
const { action } = context;
await this.fileStorageService.delete({
folderPath: getServerlessFolder({
serverlessFunction: action.flatEntity,
folderPath: getServerlessFolderOrThrow({
flatServerlessFunction: action.flatEntity,
}),
});
}
@@ -3,7 +3,7 @@ import { Injectable } from '@nestjs/common';
import { WorkspaceMigrationRunnerActionHandler } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-runner/interfaces/workspace-migration-runner-action-handler-service.interface';
import { FileStorageService } from 'src/engine/core-modules/file-storage/file-storage.service';
import { getServerlessFolder } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { getServerlessFolderOrThrow } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { findFlatEntityByIdInFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps-or-throw.util';
import { ServerlessFunctionEntity } from 'src/engine/metadata-modules/serverless-function/serverless-function.entity';
import { DeleteServerlessFunctionAction } from 'src/engine/workspace-manager/workspace-migration/workspace-migration-builder/builders/serverless-function/types/workspace-migration-serverless-function-action.type';
@@ -43,13 +43,13 @@ export class DeleteServerlessFunctionActionHandlerService extends WorkspaceMigra
// TODO: Should implement a cron task or a job to delete the files after a certain period of time
await this.fileStorageService.move({
from: {
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
folderPath: getServerlessFolderOrThrow({
flatServerlessFunction: existingServerlessFunction,
}),
},
to: {
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
folderPath: getServerlessFolderOrThrow({
flatServerlessFunction: existingServerlessFunction,
toDelete: true,
}),
},
@@ -70,14 +70,14 @@ export class DeleteServerlessFunctionActionHandlerService extends WorkspaceMigra
await this.fileStorageService.move({
from: {
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
folderPath: getServerlessFolderOrThrow({
flatServerlessFunction: existingServerlessFunction,
toDelete: true,
}),
},
to: {
folderPath: getServerlessFolder({
serverlessFunction: existingServerlessFunction,
folderPath: getServerlessFolderOrThrow({
flatServerlessFunction: existingServerlessFunction,
toDelete: false,
}),
},
@@ -7,7 +7,7 @@ import { WorkspaceMigrationRunnerActionHandler } from 'src/engine/workspace-mana
import { FileStorageService } from 'src/engine/core-modules/file-storage/file-storage.service';
import { ServerlessService } from 'src/engine/core-modules/serverless/serverless.service';
import { getServerlessFolder } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { getServerlessFolderOrThrow } from 'src/engine/core-modules/serverless/utils/serverless-get-folder.utils';
import { findFlatEntityByIdInFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps-or-throw.util';
import { ServerlessFunctionEntity } from 'src/engine/metadata-modules/serverless-function/serverless-function.entity';
import { FlatServerlessFunction } from 'src/engine/metadata-modules/serverless-function/types/flat-serverless-function.type';
@@ -43,7 +43,7 @@ export class UpdateServerlessFunctionActionHandlerService extends WorkspaceMigra
fromFlatEntityPropertiesUpdatesToPartialFlatEntity(action),
);
const serverlessFunction = findFlatEntityByIdInFlatEntityMapsOrThrow({
const flatServerlessFunction = findFlatEntityByIdInFlatEntityMapsOrThrow({
flatEntityId: entityId,
flatEntityMaps: context.allFlatEntityMaps.flatServerlessFunctionMaps,
});
@@ -51,37 +51,35 @@ export class UpdateServerlessFunctionActionHandlerService extends WorkspaceMigra
for (const update of action.updates) {
if (update.property === 'checksum' && isDefined(code)) {
await this.handleChecksumUpdate({
serverlessFunction,
flatServerlessFunction,
code,
});
}
if (update.property === 'deletedAt' && isDefined(update.to)) {
await this.handleDeletedAtUpdate({
serverlessFunction,
flatServerlessFunction,
});
}
}
}
async handleDeletedAtUpdate({
serverlessFunction,
flatServerlessFunction,
}: {
serverlessFunction: FlatServerlessFunction;
flatServerlessFunction: FlatServerlessFunction;
}) {
this.serverlessService.delete(
serverlessFunction as unknown as ServerlessFunctionEntity,
);
this.serverlessService.delete(flatServerlessFunction);
}
async handleChecksumUpdate({
serverlessFunction,
flatServerlessFunction,
code,
}: {
serverlessFunction: FlatServerlessFunction;
flatServerlessFunction: FlatServerlessFunction;
code: Sources;
}) {
const fileFolder = getServerlessFolder({
serverlessFunction,
const fileFolder = getServerlessFolderOrThrow({
flatServerlessFunction,
version: 'draft',
});