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

feat: twenty orm for standard and custom objects #6178

Merged
merged 29 commits into from
Jul 19, 2024

Conversation

magrinj
Copy link
Member

@magrinj magrinj commented Jul 9, 2024

Overview

This PR builds upon #5153, adding the ability to get a repository for custom objects. The entitySchema is now generated for both standard and custom objects based on metadata stored in the database instead of the decorated WorkspaceEntity in the code. This change ensures that standard objects with custom fields and relations can also support custom objects.

Implementation Details

Key Changes:

  • Dynamic Schema Generation: The entitySchema for standard and custom objects is now dynamically generated from the metadata stored in the database. This shift allows for greater flexibility and adaptability, particularly for standard objects with custom fields and relations.

  • Custom Object Repository Retrieval: A repository for a custom object can be retrieved using TwentyORMManager based on the object's name. Here's an example of how this can be achieved:

    const repository = await this.twentyORMManager.getRepository('custom');
    /*
     * `repository` variable will be typed as follows, ensuring that standard fields and relations are properly typed:
     * const repository: WorkspaceRepository<CustomWorkspaceEntity & {
     *    [key: string]: any;
     * }>
     */
    const res = await repository.find({});

Fix #6179

@magrinj magrinj marked this pull request as ready for review July 9, 2024 14:35
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

  • Dynamic Schema Generation: Refactored EntitySchemaColumnFactory, EntitySchemaRelationFactory, and EntitySchemaFactory to support dynamic schema generation from database metadata.
  • Custom Object Repository: Updated TwentyORMManager to retrieve repositories for custom objects using metadata names.
  • Storage Management: Introduced ObjectEntitiesStorage for managing entity schemas and metadata, removed ObjectLiteralStorage.
  • Dependency Updates: Added lodash.lowerfirst to package.json for handling object names.
  • Scoped Workspace Handling: Renamed and simplified ScopedWorkspaceDatasourceFactory to ScopedWorkspaceIdFactory.

12 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

@magrinj magrinj force-pushed the feat/twenty-orm-custom-object branch from 8785138 to d9d8480 Compare July 9, 2024 15:32
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

(updates since last review)

  • Updated workspace.repository.ts to use ObjectEntitiesStorage.getObjectMetadataEntity for dynamic schema generation.
  • Renamed methods in object-entities.storage.ts for clarity.
  • Added methods in object-entities.storage.ts to retrieve collections and entities by metadata name.

No major changes found since last review.

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

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

(updates since last review)

  • Updated packages/twenty-server/src/engine/twenty-orm/factories/entity-schema.factory.ts to support dynamic schema generation from metadata.
  • Added getObjectMetadataByName method in entity-schema.factory.ts for fetching metadata.
  • Modified packages/twenty-server/src/engine/twenty-orm/repository/workspace.repository.ts to use ObjectEntitiesStorage.getObjectMetadataEntity.
  • Renamed methods in packages/twenty-server/src/engine/twenty-orm/storage/object-entities.storage.ts for clarity.
  • Enhanced object-entities.storage.ts with new methods for retrieving metadata and entity schemas.

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

Comment on lines +39 to +40
if (!objectMetadata) {
throw new Error('Object metadata not found');
Copy link
Contributor

Choose a reason for hiding this comment

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

🧠 logic: Throws an error if objectMetadata is not found.

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

(updates since last review)

  • Modified packages/twenty-server/src/database/typeorm/typeorm.module.ts to support dynamic schema generation.
  • Introduced cacheVersion parameter in packages/twenty-server/src/engine/twenty-orm/factories/workspace-datasource.factory.ts.
  • Renamed ScopedWorkspaceIdFactory to ScopedWorkspaceContextFactory in packages/twenty-server/src/engine/twenty-orm/factories/index.ts.
  • Added logic to destroy outdated data sources in packages/twenty-server/src/engine/twenty-orm/storage/data-source.storage.ts.
  • Updated packages/twenty-server/src/engine/twenty-orm/twenty-orm.manager.ts for dynamic schema generation and cache version management.

10 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings

@@ -7,6 +7,16 @@ export class DataSourceStorage {
new Map();

public static getDataSource(key: string): WorkspaceDataSource | undefined {
// Destroy all data sources that belong to the workspace if the cache version change
for (const [dataSourceKey, dataSource] of this.dataSources.entries()) {
const workspaceId = key.split('-')?.[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

🪶 style: Consider adding a null check for key.split('-') to avoid potential runtime errors.

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

(updates since last review)

  • Added UpgradeTo0_22CommandModule in packages/twenty-server/src/database/commands/database-command.module.ts
  • Removed UpdateBooleanFieldsNullDefaultValuesAndNullValuesCommand in packages/twenty-server/src/database/commands/database-command.module.ts
  • Introduced FixObjectMetadataIdStandardIdCommand in packages/twenty-server/src/database/commands/upgrade-version/0-22/0-22-fix-object-metadata-id-standard-id.command.ts
  • Renamed and updated command in packages/twenty-server/src/database/commands/upgrade-version/0-22/0-22-update-boolean-fields-null-default-values-and-null-values.command.ts
  • Added UpgradeTo0_22Command and module in packages/twenty-server/src/database/commands/upgrade-version/0-22/0-22-upgrade-version.command.ts and 0-22-upgrade-version.module.ts

5 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

@charlesBochet charlesBochet self-assigned this Jul 10, 2024
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

(updates since last review)

  • Added Kubernetes and Terraform configurations for deploying TwentyCRM (packages/twenty-docker/k8s/).
  • Enhanced link handling logic in LinksFieldInput.tsx and LinksFieldMenuItem.tsx (packages/twenty-front/src/modules/object-record/record-field/meta-types/input/components/).
  • Renamed 'emailAliases' to 'handleAliases' across multiple files for consistency (packages/twenty-server/src/modules/connected-account/, packages/twenty-server/src/modules/messaging/).
  • Modified validation schema to allow empty strings as valid URLs (packages/twenty-front/src/utils/validation-schemas/absoluteUrlSchema.ts).
  • Updated dropdown rendering logic to use portals for better positioning (packages/twenty-front/src/modules/ui/layout/dropdown/components/Dropdown.tsx).

45 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

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

(updates since last review)

  • Switched from InjectWorkspaceRepository to TwentyORMManager for repository management (packages/twenty-server/src/engine/core-modules/calendar/timeline-calendar-event.service.ts)
  • Updated methods to retrieve repositories dynamically (packages/twenty-server/src/engine/core-modules/calendar/timeline-calendar-event.service.ts)
  • Introduced dynamic schema generation from database metadata (packages/twenty-server/src/engine/twenty-orm/twenty-orm.manager.ts)
  • Added custom object repository retrieval (packages/twenty-server/src/engine/twenty-orm/twenty-orm.manager.ts)
  • New method getRepositoryForWorkspace for specific workspace repositories (packages/twenty-server/src/engine/twenty-orm/twenty-orm.manager.ts)

2 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

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

(updates since last review)

  • Replaced direct repository injections with dynamic retrieval using TwentyORMManager (packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts)
  • Updated repository retrieval to use TwentyORMManager for dynamic schema generation (packages/twenty-server/src/modules/calendar/blocklist-manager/jobs/blocklist-item-delete-calendar-events.job.ts)
  • Refactored services to dynamically obtain repositories via TwentyORMManager (packages/twenty-server/src/modules/calendar/calendar-event-cleaner/services/calendar-event-cleaner.service.ts)
  • Enhanced flexibility by dynamically fetching repositories in various services (packages/twenty-server/src/modules/calendar/calendar-event-import-manager/services/calendar-events-import.service.ts)
  • Aligned repository management with new ORM capabilities for custom objects (packages/twenty-server/src/modules/messaging/message-cleaner/services/messaging-message-cleaner.service.ts)

18 file(s) reviewed, no comment(s)
Edit PR Review Bot Settings

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

(updates since last review)

  • Replaced WorkspaceSchemaStorageModule with WorkspaceCacheStorageModule for dynamic schema generation (packages/twenty-server/src/engine/api/graphql/core-graphql-api.module.ts)
  • Updated WorkspaceSchemaStorageService to WorkspaceCacheStorageService for dynamic schema generation (packages/twenty-server/src/engine/api/graphql/workspace-schema.factory.ts)
  • Introduced CacheManager class for caching dynamic schemas (packages/twenty-server/src/engine/twenty-orm/storage/cache-manager.storage.ts)
  • Refactored repository injections to use InjectWorkspaceRepository instead of TwentyORMManager (packages/twenty-server/src/modules/calendar/blocklist-manager/jobs/blocklist-item-delete-calendar-events.job.ts)
  • Added TestModule and TestResolver to demonstrate dynamic schema generation (packages/twenty-server/src/engine/core-modules/test-module/test.module.ts, packages/twenty-server/src/engine/core-modules/test-module/test.resolver.ts)

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

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

(updates since last review)

  • Added internalContext initialization in createEntityManager method (packages/twenty-server/src/engine/twenty-orm/datasource/workspace.datasource.ts)
  • Introduced internalContext in WorkspaceEntityManager constructor and updated getRepository method (packages/twenty-server/src/engine/twenty-orm/entity-manager/entity.manager.ts)
  • Added workspaceId parameter to create method for dynamic schema generation (packages/twenty-server/src/engine/twenty-orm/factories/entity-schema-column.factory.ts)
  • Removed getRelationType method and centralized relation details logic (packages/twenty-server/src/engine/twenty-orm/factories/entity-schema-relation.factory.ts)
  • Deleted data-source.storage.ts file, indicating a refactor in data source management (packages/twenty-server/src/engine/twenty-orm/storage/data-source.storage.ts)

18 file(s) reviewed, 5 comment(s)
Edit PR Review Bot Settings

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

(updates since last review)

  • Removed TestModule and related files (packages/twenty-server/src/engine/core-modules/core-engine.module.ts, packages/twenty-server/src/engine/core-modules/test-module/test.module.ts, packages/twenty-server/src/engine/core-modules/test-module/test.resolver.ts)
  • Improved WorkspaceEntityManager initialization and dynamic schema generation (packages/twenty-server/src/engine/twenty-orm/datasource/workspace.datasource.ts)
  • Cleaned up logging in WorkspaceEntityManager (packages/twenty-server/src/engine/twenty-orm/entity-manager/entity.manager.ts)
  • Enhanced dynamic schema generation using WorkspaceEntitiesStorage (packages/twenty-server/src/engine/twenty-orm/factories/entity-schema.factory.ts)
  • Introduced dynamic schema generation and repository retrieval for custom objects (packages/twenty-server/src/engine/twenty-orm/repository/workspace.repository.ts, packages/twenty-server/src/engine/twenty-orm/twenty-orm.manager.ts)

10 file(s) reviewed, 2 comment(s)
Edit PR Review Bot Settings

Comment on lines +49 to +50
if (!objectMetadata) {
throw new Error('Object metadata not found');
Copy link
Contributor

Choose a reason for hiding this comment

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

Logic: Throws an error if objectMetadata is not found.

): Promise<WorkspaceRepository<T>>;

async getRepository<T extends ObjectLiteral>(
entityClassOrobjectMetadataName: Type<T> | string,
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: Consider renaming entityClassOrobjectMetadataName to entityClassOrObjectMetadataName for better readability.

@charlesBochet charlesBochet merged commit 088d061 into main Jul 19, 2024
3 of 4 checks passed
@charlesBochet charlesBochet deleted the feat/twenty-orm-custom-object branch July 19, 2024 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: twenty-orm can support custom object and custom fields on standard object
3 participants