forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extend twenty orm (twentyhq#5238)
This PR is a follow up of PR twentyhq#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; } } ```
- Loading branch information
Showing
15 changed files
with
783 additions
and
74 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
packages/twenty-server/src/engine/twenty-orm/datasource/workspace.datasource.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/twenty-server/src/engine/twenty-orm/entity-manager/entity.manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { EntityManager, EntityTarget, ObjectLiteral } from 'typeorm'; | ||
|
||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository'; | ||
|
||
export class WorkspaceEntityManager extends EntityManager { | ||
override getRepository<Entity extends ObjectLiteral>( | ||
target: EntityTarget<Entity>, | ||
): WorkspaceRepository<Entity> { | ||
// find already created repository instance and return it if found | ||
const repoFromMap = this.repositories.get(target); | ||
|
||
if (repoFromMap) return repoFromMap as WorkspaceRepository<Entity>; | ||
|
||
const newRepository = new WorkspaceRepository<Entity>( | ||
target, | ||
this, | ||
this.queryRunner, | ||
); | ||
|
||
this.repositories.set(target, newRepository); | ||
|
||
return newRepository; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/twenty-server/src/engine/twenty-orm/factories/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { EntitySchemaColumnFactory } from 'src/engine/twenty-orm/factories/entity-schema-column.factory'; | ||
import { EntitySchemaRelationFactory } from 'src/engine/twenty-orm/factories/entity-schema-relation.factory'; | ||
import { EntitySchemaFactory } from 'src/engine/twenty-orm/factories/entity-schema.factory'; | ||
import { ScopedWorkspaceDatasourceFactory } from 'src/engine/twenty-orm/factories/scoped-workspace-datasource.factory'; | ||
import { WorkspaceDatasourceFactory } from 'src/engine/twenty-orm/factories/workspace-datasource.factory'; | ||
|
||
export const entitySchemaFactories = [ | ||
EntitySchemaColumnFactory, | ||
EntitySchemaRelationFactory, | ||
EntitySchemaFactory, | ||
WorkspaceDatasourceFactory, | ||
ScopedWorkspaceDatasourceFactory, | ||
]; |
25 changes: 25 additions & 0 deletions
25
...ages/twenty-server/src/engine/twenty-orm/factories/scoped-workspace-datasource.factory.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Inject, Injectable, Scope } from '@nestjs/common'; | ||
import { REQUEST } from '@nestjs/core'; | ||
|
||
import { EntitySchema } from 'typeorm'; | ||
|
||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
import { WorkspaceDatasourceFactory } from 'src/engine/twenty-orm/factories/workspace-datasource.factory'; | ||
|
||
@Injectable({ scope: Scope.REQUEST }) | ||
export class ScopedWorkspaceDatasourceFactory { | ||
constructor( | ||
@Inject(REQUEST) private readonly request: Request, | ||
private readonly workspaceDataSourceFactory: WorkspaceDatasourceFactory, | ||
) {} | ||
|
||
public async create(entities: EntitySchema[]) { | ||
const workspace: Workspace | undefined = this.request['req']?.['workspace']; | ||
|
||
if (!workspace) { | ||
return null; | ||
} | ||
|
||
return this.workspaceDataSourceFactory.create(entities, workspace.id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.