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:
@ -1,11 +1,10 @@
|
||||
import { Provider, Type } from '@nestjs/common';
|
||||
import { EntityClassOrSchema } from '@nestjs/typeorm/dist/interfaces/entity-class-or-schema.type';
|
||||
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { getWorkspaceRepositoryToken } from 'src/engine/twenty-orm/utils/get-workspace-repository-token.util';
|
||||
import { TWENTY_ORM_WORKSPACE_DATASOURCE } from 'src/engine/twenty-orm/twenty-orm.constants';
|
||||
import { EntitySchemaFactory } from 'src/engine/twenty-orm/factories/entity-schema.factory';
|
||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||
|
||||
/**
|
||||
* Create providers for the given entities.
|
||||
@ -16,11 +15,15 @@ export function createTwentyORMProviders(
|
||||
return (objects || []).map((object) => ({
|
||||
provide: getWorkspaceRepositoryToken(object),
|
||||
useFactory: (
|
||||
dataSource: DataSource,
|
||||
dataSource: WorkspaceDataSource | null,
|
||||
entitySchemaFactory: EntitySchemaFactory,
|
||||
) => {
|
||||
const entity = entitySchemaFactory.create(object as Type);
|
||||
|
||||
if (!dataSource) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return dataSource.getRepository(entity);
|
||||
},
|
||||
inject: [TWENTY_ORM_WORKSPACE_DATASOURCE, EntitySchemaFactory],
|
||||
|
||||
Reference in New Issue
Block a user