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:
@ -5,6 +5,7 @@ import { EntitySchema } from 'typeorm';
|
||||
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 { metadataArgsStorage } from 'src/engine/twenty-orm/storage/metadata-args.storage';
|
||||
import { ObjectLiteralStorage } from 'src/engine/twenty-orm/storage/object-literal.storage';
|
||||
|
||||
@Injectable()
|
||||
export class EntitySchemaFactory {
|
||||
@ -33,11 +34,15 @@ export class EntitySchemaFactory {
|
||||
relationMetadataArgsCollection,
|
||||
);
|
||||
|
||||
return new EntitySchema({
|
||||
const entitySchema = new EntitySchema({
|
||||
name: objectMetadataArgs.nameSingular,
|
||||
tableName: objectMetadataArgs.nameSingular,
|
||||
columns,
|
||||
relations,
|
||||
});
|
||||
|
||||
ObjectLiteralStorage.setObjectLiteral(entitySchema, target);
|
||||
|
||||
return entitySchema;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
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 = [
|
||||
@ -8,4 +9,5 @@ export const entitySchemaFactories = [
|
||||
EntitySchemaRelationFactory,
|
||||
EntitySchemaFactory,
|
||||
WorkspaceDatasourceFactory,
|
||||
ScopedWorkspaceDatasourceFactory,
|
||||
];
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -1,31 +1,22 @@
|
||||
import { Inject, Injectable, Scope } from '@nestjs/common';
|
||||
import { REQUEST } from '@nestjs/core';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { DataSource, EntitySchema } from 'typeorm';
|
||||
import { EntitySchema } from 'typeorm';
|
||||
|
||||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { DataSourceStorage } from 'src/engine/twenty-orm/storage/data-source.storage';
|
||||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
|
||||
|
||||
@Injectable({ scope: Scope.REQUEST })
|
||||
@Injectable()
|
||||
export class WorkspaceDatasourceFactory {
|
||||
constructor(
|
||||
@Inject(REQUEST) private readonly request: Request,
|
||||
private readonly dataSourceService: DataSourceService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
) {}
|
||||
|
||||
public async createWorkspaceDatasource(entities: EntitySchema[]) {
|
||||
const workspace: Workspace = this.request['req']['workspace'];
|
||||
|
||||
if (!workspace) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const storedWorkspaceDataSource = DataSourceStorage.getDataSource(
|
||||
workspace.id,
|
||||
);
|
||||
public async create(entities: EntitySchema[], workspaceId: string) {
|
||||
const storedWorkspaceDataSource =
|
||||
DataSourceStorage.getDataSource(workspaceId);
|
||||
|
||||
if (storedWorkspaceDataSource) {
|
||||
return storedWorkspaceDataSource;
|
||||
@ -33,10 +24,10 @@ export class WorkspaceDatasourceFactory {
|
||||
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
|
||||
workspace.id,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
const workspaceDataSource = new DataSource({
|
||||
const workspaceDataSource = new WorkspaceDataSource({
|
||||
url:
|
||||
dataSourceMetadata.url ??
|
||||
this.environmentService.get('PG_DATABASE_URL'),
|
||||
@ -51,7 +42,7 @@ export class WorkspaceDatasourceFactory {
|
||||
|
||||
await workspaceDataSource.initialize();
|
||||
|
||||
DataSourceStorage.setDataSource(workspace.id, workspaceDataSource);
|
||||
DataSourceStorage.setDataSource(workspaceId, workspaceDataSource);
|
||||
|
||||
return workspaceDataSource;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user