-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
add createMany fields to fieldMetadataService to batch field creation #9957
Conversation
Nice! |
await queryRunner.startTransaction(); | ||
|
||
try { | ||
const fieldMetadataRepository = |
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.
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 👀
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.
PR Summary
This PR optimizes field metadata creation by introducing batch processing capabilities in the FieldMetadataService.
- Added new
createMany
method inpackages/twenty-server/src/engine/metadata-modules/field-metadata/field-metadata.service.ts
to handle bulk field creation - Refactored
DataSeedWorkspaceCommand
andSeederService
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
packages/twenty-server/src/database/commands/data-seed-dev-workspace.command.ts
Show resolved
Hide resolved
const view = await workspaceQueryRunner?.query( | ||
`SELECT id FROM ${dataSourceMetadata.schema}."view" | ||
WHERE "objectMetadataId" = '${createdFieldMetadata.objectMetadataId}'`, | ||
); |
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.
logic: SQL injection vulnerability in template string. Should use parameterized queries instead of string interpolation.
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] | |
); |
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.
unlikely, those 2 variables are treated above. However I simply moved the existing code while I could have used repository pattern instead
packages/twenty-server/src/engine/metadata-modules/field-metadata/field-metadata.service.ts
Outdated
Show resolved
Hide resolved
…batch-transaction-and-cache-invalidation
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.
Could we also add a test if relevant?
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.