feat: extend twenty orm (#5238)

This PR is a follow up of PR #5153.
This one introduce some changes on how we're querying composite fields.
We can do:

```typescript
export class CompanyService {
  constructor(
    @InjectWorkspaceRepository(CompanyObjectMetadata)
    private readonly companyObjectMetadataRepository: WorkspaceRepository<CompanyObjectMetadata>,
  ) {}

  async companies(): Promise<CompanyObjectMetadata[]> {
    // Old way
    // const companiesFilteredByLinkLabel = await this.companyObjectMetadataRepository.find({
    //   where: { xLinkLabel: 'MyLabel' },
    // });
    // Result will return xLinkLabel property

    // New way
    const companiesFilteredByLinkLabel = await this.companyObjectMetadataRepository.find({
      where: { xLink: { label:  'MyLabel' } },
    });
    // Result will return { xLink: { label: 'MyLabel' } } property instead of  { xLinkLabel: 'MyLabel' }

    return companiesFilteredByLinkLabel;
  }
}
```

Also we can now inject `TwentyORMManage` class to manually create a
repository based on a given `workspaceId` using
`getRepositoryForWorkspace` function that way:

```typescript
export class CompanyService {
  constructor(
    // TwentyORMModule should be initialized
    private readonly twentyORMManager,
  ) {}

  async companies(): Promise<CompanyObjectMetadata[]> {
    const repository = await this.twentyORMManager.getRepositoryForWorkspace(
      '8bb6e872-a71f-4341-82b5-6b56fa81cd77',
      CompanyObjectMetadata,
    );

    const companies = await repository.find();

    return companies;
  }
}
```
This commit is contained in:
Jérémy M
2024-05-06 14:12:11 +02:00
committed by GitHub
parent 154ae99ed3
commit b207d10312
15 changed files with 784 additions and 75 deletions

View File

@ -1,13 +1,19 @@
import { DataSource } from 'typeorm';
export class DataSourceStorage {
private static readonly dataSources: Map<string, DataSource> = new Map();
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
public static getDataSource(key: string): DataSource | undefined {
export class DataSourceStorage {
private static readonly dataSources: Map<string, WorkspaceDataSource> =
new Map();
public static getDataSource(key: string): WorkspaceDataSource | undefined {
return this.dataSources.get(key);
}
public static setDataSource(key: string, dataSource: DataSource): void {
public static setDataSource(
key: string,
dataSource: WorkspaceDataSource,
): void {
this.dataSources.set(key, dataSource);
}

View File

@ -0,0 +1,30 @@
import { Type } from '@nestjs/common';
import { EntitySchema } from 'typeorm';
export class ObjectLiteralStorage {
private static readonly objects: Map<EntitySchema, Type<any>> = new Map();
public static getObjectLiteral(target: EntitySchema): Type<any> | undefined {
return this.objects.get(target);
}
public static setObjectLiteral(
target: EntitySchema,
objectLiteral: Type<any>,
): void {
this.objects.set(target, objectLiteral);
}
public static getAllObjects(): Type<any>[] {
return Array.from(this.objects.values());
}
public static getAllEntitySchemas(): EntitySchema[] {
return Array.from(this.objects.keys());
}
public static clear(): void {
this.objects.clear();
}
}