-
Notifications
You must be signed in to change notification settings - Fork 4k
Connect/Disconnect - Add Disconnect logic + Migration to query builders (insert/update) #13271
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
Changes from all commits
e7a1b86
d3bfe09
b430071
b432267
d5758d4
f068451
37a08b0
fb0281b
49802ed
3d8d7f5
ed8d446
afb3eb6
04da4af
3e903f9
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,14 @@ | ||
import { | ||
ConnectObject, | ||
DisconnectObject, | ||
} from 'src/engine/twenty-orm/entity-manager/types/query-deep-partial-entity-with-relation-connect.type'; | ||
|
||
export type RelationConnectQueryFieldsByEntityIndex = { | ||
[entityIndex: string]: { [key: string]: ConnectObject }; | ||
}; | ||
|
||
export type RelationDisconnectQueryFieldsByEntityIndex = { | ||
[entityIndex: string]: { | ||
[key: string]: DisconnectObject; | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,24 +40,16 @@ import { | |
PermissionsExceptionCode, | ||
} from 'src/engine/metadata-modules/permissions/permissions.exception'; | ||
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource'; | ||
import { QueryDeepPartialEntityWithRelationConnect } from 'src/engine/twenty-orm/entity-manager/types/query-deep-partial-entity-with-relation-connect.type'; | ||
import { RelationConnectQueryConfig } from 'src/engine/twenty-orm/entity-manager/types/relation-connect-query-config.type'; | ||
import { | ||
TwentyORMException, | ||
TwentyORMExceptionCode, | ||
} from 'src/engine/twenty-orm/exceptions/twenty-orm.exception'; | ||
import { QueryDeepPartialEntityWithNestedRelationFields } from 'src/engine/twenty-orm/entity-manager/types/query-deep-partial-entity-with-relation-connect.type'; | ||
import { | ||
OperationType, | ||
validateOperationIsPermittedOrThrow, | ||
} from 'src/engine/twenty-orm/repository/permissions.utils'; | ||
import { WorkspaceSelectQueryBuilder } from 'src/engine/twenty-orm/repository/workspace-select-query-builder'; | ||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository'; | ||
import { computeRelationConnectQueryConfigs } from 'src/engine/twenty-orm/utils/compute-relation-connect-query-configs.util'; | ||
import { createSqlWhereTupleInClause } from 'src/engine/twenty-orm/utils/create-sql-where-tuple-in-clause.utils'; | ||
import { formatData } from 'src/engine/twenty-orm/utils/format-data.util'; | ||
import { formatResult } from 'src/engine/twenty-orm/utils/format-result.util'; | ||
import { getObjectMetadataFromEntityTarget } from 'src/engine/twenty-orm/utils/get-object-metadata-from-entity-target.util'; | ||
import { getRecordToConnectFields } from 'src/engine/twenty-orm/utils/get-record-to-connect-fields.util'; | ||
|
||
type PermissionOptions = { | ||
shouldBypassPermissionChecks?: boolean; | ||
|
@@ -172,19 +164,11 @@ export class WorkspaceEntityManager extends EntityManager { | |
override async insert<Entity extends ObjectLiteral>( | ||
target: EntityTarget<Entity>, | ||
entity: | ||
| QueryDeepPartialEntityWithRelationConnect<Entity> | ||
| QueryDeepPartialEntityWithRelationConnect<Entity>[], | ||
| QueryDeepPartialEntityWithNestedRelationFields<Entity> | ||
| QueryDeepPartialEntityWithNestedRelationFields<Entity>[], | ||
selectedColumns: string[] = [], | ||
permissionOptions?: PermissionOptions, | ||
): Promise<InsertResult> { | ||
const entityArray = Array.isArray(entity) ? entity : [entity]; | ||
|
||
const connectedEntities = await this.processRelationConnect<Entity>( | ||
entityArray, | ||
target, | ||
permissionOptions, | ||
); | ||
|
||
return this.createQueryBuilder( | ||
undefined, | ||
undefined, | ||
|
@@ -193,7 +177,7 @@ export class WorkspaceEntityManager extends EntityManager { | |
) | ||
.insert() | ||
.into(target) | ||
.values(connectedEntities) | ||
.values(entity) | ||
.returning(selectedColumns) | ||
.execute(); | ||
} | ||
|
@@ -1484,118 +1468,4 @@ export class WorkspaceEntityManager extends EntityManager { | |
PermissionsExceptionCode.RAW_SQL_NOT_ALLOWED, | ||
); | ||
} | ||
|
||
private async processRelationConnect<Entity extends ObjectLiteral>( | ||
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. Do we want to implement the same logic for methods that don't rely on query builders? 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'll do it for the .save. Regarding the others, it does not seem useful as they are not create/update operations 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'll push it later, tomorrow |
||
entities: QueryDeepPartialEntityWithRelationConnect<Entity>[], | ||
target: EntityTarget<Entity>, | ||
permissionOptions?: PermissionOptions, | ||
): Promise<QueryDeepPartialEntity<Entity>[]> { | ||
const objectMetadata = getObjectMetadataFromEntityTarget( | ||
target, | ||
this.internalContext, | ||
); | ||
|
||
const objectMetadataMap = this.internalContext.objectMetadataMaps; | ||
|
||
const relationConnectQueryConfigs = computeRelationConnectQueryConfigs( | ||
entities, | ||
objectMetadata, | ||
objectMetadataMap, | ||
); | ||
|
||
if (!isDefined(relationConnectQueryConfigs)) return entities; | ||
|
||
const recordsToConnectWithConfig = await this.executeConnectQueries( | ||
relationConnectQueryConfigs, | ||
permissionOptions, | ||
); | ||
|
||
const updatedEntities = this.updateEntitiesWithRecordToConnectId<Entity>( | ||
entities, | ||
recordsToConnectWithConfig, | ||
); | ||
|
||
return updatedEntities; | ||
} | ||
|
||
private async executeConnectQueries( | ||
relationConnectQueryConfigs: Record<string, RelationConnectQueryConfig>, | ||
permissionOptions?: PermissionOptions, | ||
): Promise<[RelationConnectQueryConfig, Record<string, unknown>[]][]> { | ||
const AllRecordsToConnectWithConfig: [ | ||
RelationConnectQueryConfig, | ||
Record<string, unknown>[], | ||
][] = []; | ||
|
||
for (const connectQueryConfig of Object.values( | ||
relationConnectQueryConfigs, | ||
)) { | ||
const { clause, parameters } = createSqlWhereTupleInClause( | ||
connectQueryConfig.recordToConnectConditions, | ||
connectQueryConfig.targetObjectName, | ||
); | ||
|
||
const recordsToConnect = await this.createQueryBuilder( | ||
connectQueryConfig.targetObjectName, | ||
connectQueryConfig.targetObjectName, | ||
undefined, | ||
permissionOptions, | ||
) | ||
.select(getRecordToConnectFields(connectQueryConfig)) | ||
.where(clause, parameters) | ||
.getRawMany(); | ||
|
||
AllRecordsToConnectWithConfig.push([ | ||
connectQueryConfig, | ||
recordsToConnect, | ||
]); | ||
} | ||
|
||
return AllRecordsToConnectWithConfig; | ||
} | ||
|
||
private updateEntitiesWithRecordToConnectId<Entity extends ObjectLiteral>( | ||
entities: QueryDeepPartialEntityWithRelationConnect<Entity>[], | ||
recordsToConnectWithConfig: [ | ||
RelationConnectQueryConfig, | ||
Record<string, unknown>[], | ||
][], | ||
): QueryDeepPartialEntity<Entity>[] { | ||
return entities.map((entity, index) => { | ||
for (const [ | ||
connectQueryConfig, | ||
recordsToConnect, | ||
] of recordsToConnectWithConfig) { | ||
if ( | ||
isDefined( | ||
connectQueryConfig.recordToConnectConditionByEntityIndex[index], | ||
) | ||
) { | ||
const recordToConnect = recordsToConnect.filter((record) => | ||
connectQueryConfig.recordToConnectConditionByEntityIndex[ | ||
index | ||
].every(([field, value]) => record[field] === value), | ||
); | ||
|
||
if (recordToConnect.length !== 1) { | ||
const recordToConnectTotal = recordToConnect.length; | ||
const connectFieldName = connectQueryConfig.connectFieldName; | ||
|
||
throw new TwentyORMException( | ||
`Expected 1 record to connect to ${connectFieldName}, but found ${recordToConnectTotal}.`, | ||
TwentyORMExceptionCode.CONNECT_RECORD_NOT_FOUND, | ||
); | ||
} | ||
|
||
entity = { | ||
...entity, | ||
[connectQueryConfig.relationFieldName]: recordToConnect[0]['id'], | ||
[connectQueryConfig.connectFieldName]: null, | ||
}; | ||
} | ||
} | ||
|
||
return entity; | ||
}); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.