feat: twenty orm for standard and custom objects (#6178)
### 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: ```typescript 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 --------- Co-authored-by: Charles Bochet <charles@twenty.com> Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
@ -1,24 +1,18 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { Any } from 'typeorm';
|
||||
import omit from 'lodash.omit';
|
||||
import { Any } from 'typeorm';
|
||||
|
||||
import { TIMELINE_CALENDAR_EVENTS_DEFAULT_PAGE_SIZE } from 'src/engine/core-modules/calendar/constants/calendar.constants';
|
||||
import { TimelineCalendarEventsWithTotal } from 'src/engine/core-modules/calendar/dtos/timeline-calendar-events-with-total.dto';
|
||||
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
|
||||
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
|
||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
|
||||
import { CalendarChannelVisibility } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity';
|
||||
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/common/standard-objects/calendar-event.workspace-entity';
|
||||
import { PersonWorkspaceEntity } from 'src/modules/person/standard-objects/person.workspace-entity';
|
||||
|
||||
@Injectable()
|
||||
export class TimelineCalendarEventService {
|
||||
constructor(
|
||||
@InjectWorkspaceRepository(CalendarEventWorkspaceEntity)
|
||||
private readonly calendarEventRepository: WorkspaceRepository<CalendarEventWorkspaceEntity>,
|
||||
@InjectWorkspaceRepository(PersonWorkspaceEntity)
|
||||
private readonly personRepository: WorkspaceRepository<PersonWorkspaceEntity>,
|
||||
) {}
|
||||
constructor(private readonly twentyORMManager: TwentyORMManager) {}
|
||||
|
||||
// TODO: Align return type with the entities to avoid mapping
|
||||
async getCalendarEventsFromPersonIds(
|
||||
@ -28,7 +22,12 @@ export class TimelineCalendarEventService {
|
||||
): Promise<TimelineCalendarEventsWithTotal> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
const calendarEventIds = await this.calendarEventRepository.find({
|
||||
const calendarEventRepository =
|
||||
await this.twentyORMManager.getRepository<CalendarEventWorkspaceEntity>(
|
||||
'calendarEvent',
|
||||
);
|
||||
|
||||
const calendarEventIds = await calendarEventRepository.find({
|
||||
where: {
|
||||
calendarEventParticipants: {
|
||||
personId: Any(personIds),
|
||||
@ -55,7 +54,7 @@ export class TimelineCalendarEventService {
|
||||
}
|
||||
|
||||
// We've split the query into two parts, because we want to fetch all the participants without any filtering
|
||||
const [events, total] = await this.calendarEventRepository.findAndCount({
|
||||
const [events, total] = await calendarEventRepository.findAndCount({
|
||||
where: {
|
||||
id: Any(ids),
|
||||
},
|
||||
@ -131,7 +130,12 @@ export class TimelineCalendarEventService {
|
||||
page = 1,
|
||||
pageSize: number = TIMELINE_CALENDAR_EVENTS_DEFAULT_PAGE_SIZE,
|
||||
): Promise<TimelineCalendarEventsWithTotal> {
|
||||
const personIds = await this.personRepository.find({
|
||||
const personRepository =
|
||||
await this.twentyORMManager.getRepository<PersonWorkspaceEntity>(
|
||||
'person',
|
||||
);
|
||||
|
||||
const personIds = await personRepository.find({
|
||||
where: {
|
||||
companyId,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user