Skip to content
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

add createMany fields to fieldMetadataService to batch field creation #9957

Conversation

Weiko
Copy link
Member

@Weiko Weiko commented Jan 31, 2025

Context

Not exposed in the API yet, this new method allows us to reduce the time to create multiple fields at once, mostly during seeding. This allows us to batch transactions and avoid recomputing the cache everytime.

With this change, we recompute the cache 7 times instead of 35 during seeding. We could do the same for objects.

@Weiko Weiko changed the title add createmany fields add createMany fields to fieldMetadataService to batch field creation Jan 31, 2025
@FelixMalfait
Copy link
Member

Nice!

await queryRunner.startTransaction();

try {
const fieldMetadataRepository =
Copy link
Member Author

@Weiko Weiko Jan 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of things could be refactored there, this method has many responsibilities and we could at least create a few reusable methods. I will probably not do that in this PR and only move the logic from createOne 👀

@Weiko Weiko marked this pull request as ready for review February 3, 2025 09:11
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR optimizes field metadata creation by introducing batch processing capabilities in the FieldMetadataService.

  • Added new createMany method in packages/twenty-server/src/engine/metadata-modules/field-metadata/field-metadata.service.ts to handle bulk field creation
  • Refactored DataSeedWorkspaceCommand and SeederService to use batch creation instead of individual field creation
  • Reduced cache recomputation from 35 to 7 times during seeding by grouping field creations by object metadata
  • Implemented single transaction handling for multiple field creations to improve database performance
  • Added batch processing for view field creation while maintaining existing validation and error handling

3 file(s) reviewed, 3 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 797 to 800
const view = await workspaceQueryRunner?.query(
`SELECT id FROM ${dataSourceMetadata.schema}."view"
WHERE "objectMetadataId" = '${createdFieldMetadata.objectMetadataId}'`,
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: SQL injection vulnerability in template string. Should use parameterized queries instead of string interpolation.

Suggested change
const view = await workspaceQueryRunner?.query(
`SELECT id FROM ${dataSourceMetadata.schema}."view"
WHERE "objectMetadataId" = '${createdFieldMetadata.objectMetadataId}'`,
);
const view = await workspaceQueryRunner?.query(
'SELECT id FROM $1:name."view" WHERE "objectMetadataId" = $2',
[dataSourceMetadata.schema, createdFieldMetadata.objectMetadataId]
);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlikely, those 2 variables are treated above. However I simply moved the existing code while I could have used repository pattern instead

Copy link
Collaborator

@ijreilly ijreilly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also add a test if relevant?

@Weiko Weiko merged commit 40f43a4 into main Feb 4, 2025
32 checks passed
@Weiko Weiko deleted the c--add-create-many-field-metadata-method-to-batch-transaction-and-cache-invalidation branch February 4, 2025 10:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants