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,8 +1,7 @@
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
import { objectRecordChangedValues } from 'src/engine/core-modules/event-emitter/utils/object-record-changed-values';
import { ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
const mockObjectMetadata: ObjectMetadataInterface = {
const mockObjectMetadata: ObjectMetadataItemWithFieldMaps = {
id: '1',
icon: 'Icon123',
nameSingular: 'Object',
@@ -12,14 +11,16 @@ const mockObjectMetadata: ObjectMetadataInterface = {
description: 'Test object metadata',
targetTableName: 'test_table',
workspaceId: '1',
fields: [],
indexMetadatas: [],
fieldsById: {},
fieldIdByName: {},
isSystem: false,
isCustom: false,
isActive: true,
isRemote: false,
isAuditLogged: true,
isSearchable: true,
indexMetadatas: [],
fieldIdByJoinColumnName: {},
};
describe('objectRecordChangedValues', () => {
@@ -38,7 +39,6 @@ describe('objectRecordChangedValues', () => {
const result = objectRecordChangedValues(
oldRecord,
newRecord,
['name'],
mockObjectMetadata,
);
@@ -60,7 +60,6 @@ describe('objectRecordChangedValues', () => {
const result = objectRecordChangedValues(
oldRecord,
newRecord,
[],
mockObjectMetadata,
);
@@ -82,7 +81,6 @@ describe('objectRecordChangedValues', () => {
const result = objectRecordChangedValues(
oldRecord,
newRecord,
['name', 'value'],
mockObjectMetadata,
);
@@ -112,7 +110,6 @@ describe('objectRecordChangedValues', () => {
const result = objectRecordChangedValues(
oldRecord,
newRecord,
['name', 'config', 'status'],
mockObjectMetadata,
);
@@ -2,23 +2,25 @@ import deepEqual from 'deep-equal';
import { FieldMetadataType } from 'twenty-shared/types';
import { ObjectRecord } from 'src/engine/api/graphql/workspace-query-builder/interfaces/object-record.interface';
import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface';
import { ObjectMetadataItemWithFieldMaps } from 'src/engine/metadata-modules/types/object-metadata-item-with-field-maps';
export const objectRecordChangedValues = (
oldRecord: Partial<ObjectRecord>,
newRecord: Partial<ObjectRecord>,
updatedKeys: string[] | undefined,
objectMetadataItem: ObjectMetadataInterface,
objectMetadataItem: ObjectMetadataItemWithFieldMaps,
) => {
return Object.keys(newRecord).reduce(
(acc, key) => {
const field = objectMetadataItem.fields.find((f) => f.name === key);
const field =
objectMetadataItem.fieldsById[objectMetadataItem.fieldIdByName[key]];
const oldRecordValue = oldRecord[key];
const newRecordValue = newRecord[key];
if (
key === 'updatedAt' ||
!updatedKeys?.includes(key) ||
key === 'searchVector' ||
field?.type === FieldMetadataType.RELATION ||
deepEqual(oldRecordValue, newRecordValue)
) {