Add db event emitter in twenty orm (#13167)

## Context
Add an eventEmitter instance to twenty datasources so we can emit DB
events.
Add input and output formatting to twenty orm (formatData, formatResult)
Those 2 elements simplified existing logic when we interact with the
ORM, input will be formatted by the ORM so we can directly use
field-like structure instead of column-like. The output will be
formatted, for builder queries it will be in `result.generatedMaps`
where `result.raw` preserves the previous column-like structure.

Important change: We now have an authContext that we can pass when we
get a repository, this will be used for the different events emitted in
the ORM. We also removed the caching for repositories as it was not
scaling well and not necessary imho

Note: An upcoming PR should handle the onDelete: cascade behavior where
we send DESTROY events in cascade when there is an onDelete: CASCADE on
the FK.

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
Weiko
2025-07-17 18:07:28 +02:00
committed by GitHub
parent 4a3139c9e0
commit 2deac9448e
79 changed files with 1061 additions and 2016 deletions
@@ -1,14 +1,9 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { isDefined } from 'twenty-shared/utils';
import { Repository } from 'typeorm';
import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
import { RecordPositionService } from 'src/engine/core-modules/record-position/services/record-position.service';
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
import { WorkspaceEventEmitter } from 'src/engine/workspace-event-emitter/workspace-event-emitter';
import {
WorkflowVersionStepException,
WorkflowVersionStepExceptionCode,
@@ -28,9 +23,6 @@ export class WorkflowVersionWorkspaceService {
constructor(
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly workflowVersionStepWorkspaceService: WorkflowVersionStepWorkspaceService,
@InjectRepository(ObjectMetadataEntity, 'core')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
private readonly recordPositionService: RecordPositionService,
) {}
@@ -96,11 +88,6 @@ export class WorkflowVersionWorkspaceService {
status: WorkflowVersionStatus.DRAFT,
position,
});
await this.emitWorkflowVersionCreationEvent({
workflowVersion: draftWorkflowVersion,
workspaceId,
});
}
assertWorkflowVersionIsDraft(draftWorkflowVersion);
@@ -125,41 +112,4 @@ export class WorkflowVersionWorkspaceService {
return draftWorkflowVersion.id;
}
private async emitWorkflowVersionCreationEvent({
workflowVersion,
workspaceId,
}: {
workflowVersion: WorkflowVersionWorkspaceEntity;
workspaceId: string;
}) {
const objectMetadata = await this.objectMetadataRepository.findOne({
where: {
nameSingular: 'workflowVersion',
workspaceId,
},
});
if (!objectMetadata) {
throw new WorkflowVersionStepException(
'Object metadata not found',
WorkflowVersionStepExceptionCode.FAILURE,
);
}
this.workspaceEventEmitter.emitDatabaseBatchEvent({
objectMetadataNameSingular: 'workflowVersion',
action: DatabaseEventAction.CREATED,
events: [
{
recordId: workflowVersion.id,
objectMetadata,
properties: {
after: workflowVersion,
},
},
],
workspaceId,
});
}
}