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,9 +1,6 @@
import { Injectable, Logger } from '@nestjs/common';
import { DatabaseEventAction } from 'src/engine/api/graphql/graphql-query-runner/enums/database-event-action';
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 {
MessageChannelSyncStatus,
MessageChannelWorkspaceEntity,
@@ -18,7 +15,6 @@ export class ImapHandleErrorService {
constructor(
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
private readonly workspaceEventEmitter: WorkspaceEventEmitter,
) {}
async handleError(
@@ -38,42 +34,12 @@ export class ImapHandleErrorService {
'messageChannel',
);
const messageChannel = await messageChannelRepository.findOneOrFail({
where: { id: messageChannelId },
});
await messageChannelRepository.update(
{ id: messageChannelId },
{
syncStatus: MessageChannelSyncStatus.FAILED_UNKNOWN,
},
);
const dataSource =
await this.twentyORMGlobalManager.getDataSourceForWorkspace({
workspaceId,
});
const messageChannelMetadata = await dataSource
.getRepository(ObjectMetadataEntity)
.findOneOrFail({
where: { nameSingular: 'messageChannel', workspaceId },
});
this.workspaceEventEmitter.emitDatabaseBatchEvent({
objectMetadataNameSingular: 'messageChannel',
action: DatabaseEventAction.UPDATED,
events: [
{
recordId: messageChannelId,
objectMetadata: messageChannelMetadata,
properties: {
before: { syncStatus: messageChannel.syncStatus },
after: { syncStatus: MessageChannelSyncStatus.FAILED_UNKNOWN },
},
},
],
workspaceId,
});
} catch (handleErrorError) {
this.logger.error(
`Error handling IMAP error: ${handleErrorError.message}`,