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
@@ -15,6 +15,7 @@ import { EntityManagerFactory } from 'typeorm/entity-manager/EntityManagerFactor
import { FeatureFlagMap } from 'src/engine/core-modules/feature-flag/interfaces/feature-flag-map.interface';
import { WorkspaceInternalContext } from 'src/engine/twenty-orm/interfaces/workspace-internal-context.interface';
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type';
import {
PermissionsException,
PermissionsExceptionCode,
@@ -58,20 +59,29 @@ export class WorkspaceDataSource extends DataSource {
target: EntityTarget<Entity>,
shouldBypassPermissionChecks = false,
roleId?: string,
authContext?: AuthContext,
): WorkspaceRepository<Entity> {
if (shouldBypassPermissionChecks === true) {
return this.manager.getRepository(target, {
shouldBypassPermissionChecks: true,
});
return this.manager.getRepository(
target,
{
shouldBypassPermissionChecks: true,
},
authContext,
);
}
if (roleId) {
return this.manager.getRepository(target, {
roleId,
});
return this.manager.getRepository(
target,
{
roleId,
},
authContext,
);
}
return this.manager.getRepository(target);
return this.manager.getRepository(target, undefined, authContext);
}
override createEntityManager(