476bdf764c
# Introduction
In preparation of the workspace agnostic builder, we're migrating
`FlatEntityMaps` to be universal identifier oriented and based
As in the builder context there're won't be any ids at all
Please also note that the FlatEntity is a UniversalFlatEntity superset
From
```ts
import { type SyncableFlatEntity } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-from.type';
export type FlatEntityMaps<T extends SyncableFlatEntity> = {
byId: Partial<Record<string, T>>;
idByUniversalIdentifier: Partial<Record<string, string>>;
universalIdentifiersByApplicationId: Partial<Record<string, string[]>>;
};
```
To
```ts
export type FlatEntityMaps<
T extends SyncableFlatEntity | UniversalSyncableFlatEntity,
> = {
byUniversalIdentifier: Partial<Record<string, T>>;
universalIdentifierById: Partial<Record<string, string>>;
universalIdentifiersByApplicationId: Partial<Record<string, string[]>>; // this might make more sense to be migrated to universalIdentifiersByApplicationUniversalIdentifier but it's the main topic of this PR
};
```
## Low level maps tools
Had to refactor find | create | delete | replace | find-many | get-sub
tools ( through mutations and or throw equivalent )
53 lines
1.8 KiB
TypeScript
53 lines
1.8 KiB
TypeScript
import { type EntityTarget, type ObjectLiteral } from 'typeorm';
|
|
|
|
import { type WorkspaceInternalContext } from 'src/engine/twenty-orm/interfaces/workspace-internal-context.interface';
|
|
|
|
import { findFlatEntityByIdInFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps.util';
|
|
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
|
|
import {
|
|
TwentyORMException,
|
|
TwentyORMExceptionCode,
|
|
} from 'src/engine/twenty-orm/exceptions/twenty-orm.exception';
|
|
|
|
export const getObjectMetadataFromEntityTarget = <T extends ObjectLiteral>(
|
|
entityTarget: EntityTarget<T>,
|
|
internalContext: WorkspaceInternalContext,
|
|
): FlatObjectMetadata => {
|
|
if (typeof entityTarget !== 'string') {
|
|
throw new TwentyORMException(
|
|
'Entity target must be a string',
|
|
TwentyORMExceptionCode.MALFORMED_METADATA,
|
|
);
|
|
}
|
|
|
|
const objectMetadataName = entityTarget;
|
|
|
|
const objectMetadataId =
|
|
internalContext.objectIdByNameSingular[objectMetadataName];
|
|
|
|
if (!objectMetadataId) {
|
|
throw new TwentyORMException(
|
|
`Object metadata for object "${objectMetadataName}" is missing ` +
|
|
`in workspace "${internalContext.workspaceId}" ` +
|
|
`with object metadata collection length: ${
|
|
Object.keys(internalContext.objectIdByNameSingular).length
|
|
}`,
|
|
TwentyORMExceptionCode.MALFORMED_METADATA,
|
|
);
|
|
}
|
|
|
|
const objectMetadata = findFlatEntityByIdInFlatEntityMaps({
|
|
flatEntityId: objectMetadataId,
|
|
flatEntityMaps: internalContext.flatObjectMetadataMaps,
|
|
});
|
|
|
|
if (!objectMetadata) {
|
|
throw new TwentyORMException(
|
|
`Object metadata for object "${objectMetadataName}" (id: ${objectMetadataId}) is missing`,
|
|
TwentyORMExceptionCode.MALFORMED_METADATA,
|
|
);
|
|
}
|
|
|
|
return objectMetadata;
|
|
};
|