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:
@ -0,0 +1,24 @@
|
||||
import {
|
||||
DataSource,
|
||||
EntityManager,
|
||||
EntityTarget,
|
||||
ObjectLiteral,
|
||||
QueryRunner,
|
||||
} from 'typeorm';
|
||||
|
||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||
import { WorkspaceEntityManager } from 'src/engine/twenty-orm/entity-manager/entity.manager';
|
||||
|
||||
export class WorkspaceDataSource extends DataSource {
|
||||
readonly manager: WorkspaceEntityManager;
|
||||
|
||||
override getRepository<Entity extends ObjectLiteral>(
|
||||
target: EntityTarget<Entity>,
|
||||
): WorkspaceRepository<Entity> {
|
||||
return this.manager.getRepository(target);
|
||||
}
|
||||
|
||||
override createEntityManager(queryRunner?: QueryRunner): EntityManager {
|
||||
return new WorkspaceEntityManager(this, queryRunner);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user