-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: extend twenty orm #5238
feat: extend twenty orm #5238
Changes from all commits
642d383
0a2b51c
3a7929d
d4c3bca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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, | ||
]; |
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']; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this one is a bit dangerous since we don't provide the schema name here if I understood correctly. I guess ScopedWorkspaceDatasourceFactory should throw if the request object is not properly set and the token is not valid. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @magrinj I'd rather not return anything if possible? this datasource won't be usable without a proper schema defined anyway and you would have to use TwentyORMManager.getRepositoryForWorkspace right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit complicated on my opinion to return something There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tested multiple things, and the "best" things I can do is returning |
||
if (!workspace) { | ||
return null; | ||
} | ||
|
||
return this.workspaceDataSourceFactory.create(entities, workspace.id); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's avoid using abbreviations (repo) in the code base. Also let's keep the { } around one liner if content instructions per project convention