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

[Fix] Mikro ORM Knex Custom Repository #7689

Merged
merged 6 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/gauzy/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ if (environment.SENTRY_DSN) {
export class AppModule {
constructor() {
// Set Monday as start of the week
moment.locale(LanguagesEnum.ENGLISH, {
moment.updateLocale(LanguagesEnum.ENGLISH, {
week: {
dow: dayOfWeekAsString(WeekDaysEnum.MONDAY),
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { AccountingTemplate } from '../accounting-template.entity';

export class MikroOrmAccountingTemplateRepository extends EntityRepository<AccountingTemplate> { }
export class MikroOrmAccountingTemplateRepository extends EntityRepository<AccountingTemplate> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { AppointmentEmployee } from '../appointment-employees.entity';

export class MikroOrmAppointmentEmployeeRepository extends EntityRepository<AppointmentEmployee> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { ApprovalPolicy } from '../approval-policy.entity';

export class MikroOrmApprovalPolicyRepository extends EntityRepository<ApprovalPolicy> { }
export class MikroOrmApprovalPolicyRepository extends EntityRepository<ApprovalPolicy> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { AvailabilitySlot } from '../availability-slots.entity';

export class MikroOrmAvailabilitySlotRepository extends EntityRepository<AvailabilitySlot> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateCriterionsRating } from '../candidate-criterion-rating.entity';

export class MikroOrmCandidateCriterionsRatingRepository extends EntityRepository<CandidateCriterionsRating> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateDocument } from '../candidate-documents.entity';

export class MikroOrmCandidateDocumentRepository extends EntityRepository<CandidateDocument> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateEducation } from '../candidate-education.entity';

export class MikroOrmCandidateEducationRepository extends EntityRepository<CandidateEducation> { }
export class MikroOrmCandidateEducationRepository extends EntityRepository<CandidateEducation> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateExperience } from '../candidate-experience.entity';

export class MikroOrmCandidateExperienceRepository extends EntityRepository<CandidateExperience> { }
export class MikroOrmCandidateExperienceRepository extends EntityRepository<CandidateExperience> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateFeedback } from '../candidate-feedbacks.entity';

export class MikroOrmCandidateFeedbackRepository extends EntityRepository<CandidateFeedback> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateInterview } from '../candidate-interview.entity';

export class MikroOrmCandidateInterviewRepository extends EntityRepository<CandidateInterview> { }
export class MikroOrmCandidateInterviewRepository extends EntityRepository<CandidateInterview> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateInterviewers } from '../candidate-interviewers.entity';

export class MikroOrmCandidateInterviewersRepository extends EntityRepository<CandidateInterviewers> { }
export class MikroOrmCandidateInterviewersRepository extends EntityRepository<CandidateInterviewers> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidatePersonalQualities } from '../candidate-personal-qualities.entity';

export class MikroOrmCandidatePersonalQualitiesRepository extends EntityRepository<CandidatePersonalQualities> { }
export class MikroOrmCandidatePersonalQualitiesRepository extends EntityRepository<CandidatePersonalQualities> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateSkill } from '../candidate-skill.entity';

export class MikroOrmCandidateSkillRepository extends EntityRepository<CandidateSkill> { }
export class MikroOrmCandidateSkillRepository extends EntityRepository<CandidateSkill> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateSource } from '../candidate-source.entity';

export class MikroOrmCandidateSourceRepository extends EntityRepository<CandidateSource> { }
export class MikroOrmCandidateSourceRepository extends EntityRepository<CandidateSource> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CandidateTechnologies } from '../candidate-technologies.entity';

export class MikroOrmCandidateTechnologiesRepository extends EntityRepository<CandidateTechnologies> { }
export class MikroOrmCandidateTechnologiesRepository extends EntityRepository<CandidateTechnologies> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { Candidate } from '../candidate.entity';

export class MikroOrmCandidateRepository extends EntityRepository<Candidate> { }
export class MikroOrmCandidateRepository extends EntityRepository<Candidate> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { Contact } from '../contact.entity';

export class MikroOrmContactRepository extends EntityRepository<Contact> { }
export class MikroOrmContactRepository extends EntityRepository<Contact> { }
19 changes: 12 additions & 7 deletions packages/core/src/core/crud/crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
} from '@mikro-orm/core';
import { IPagination } from '@gauzy/contracts';
import { BaseEntity } from '../entities/internal';
import { multiORMCreateQueryBuilder } from '../../core/orm/query-builder/query-builder.factory';
import { IQueryBuilder } from '../../core/orm/query-builder/iquery-builder';
import {
MultiORM,
MultiORMEnum,
Expand All @@ -42,12 +44,12 @@ import {
IUpdateCriteria
} from './icrud.service';
import { ITryRequest } from './try-request';
import { multiORMCreateQueryBuilder } from 'core/orm/query-builder/query-builder.factory';

// Get the type of the Object-Relational Mapping (ORM) used in the application.
const ormType: MultiORM = getORMType();

export abstract class CrudService<T extends BaseEntity> implements ICrudService<T> {

constructor(
protected readonly repository: Repository<T>,
protected readonly mikroRepository: EntityRepository<T>
Expand All @@ -70,17 +72,20 @@ export abstract class CrudService<T extends BaseEntity> implements ICrudService<
}

/**
* Creates an ORM-specific query builder for the repository, supporting MikroORM and TypeORM.
*
* @param entity
* @returns
* @param alias - Optional alias for the primary table in the query.
* @returns An `IQueryBuilder<T>` instance suitable for the repository's ORM type.
* @throws Error if the ORM type is not implemented.
*/
createQueryBuilder(entity?: any) {
console.log('this.mikroRepository.getEntityManager', this.mikroRepository.getEntityName());
public createQueryBuilder(alias?: string): IQueryBuilder<T> {
switch (this.ormType) {
case MultiORMEnum.MikroORM:
return multiORMCreateQueryBuilder<T>(this.repository, this.ormType as MultiORMEnum);
return multiORMCreateQueryBuilder<T>(this.mikroRepository as any, this.ormType as MultiORMEnum, alias);

case MultiORMEnum.TypeORM:
return multiORMCreateQueryBuilder<T>(this.mikroRepository as any, this.ormType as MultiORMEnum);
return multiORMCreateQueryBuilder<T>(this.repository, this.ormType as MultiORMEnum, alias);

default:
throw new Error(`Not implemented for ${this.ormType}`);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/core/crud/tenant-aware-crud.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
UpdateResult
} from 'typeorm';
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { IPagination, IUser, PermissionsEnum } from '@gauzy/contracts';
import { isNotEmpty } from '@gauzy/common';
import { RequestContext } from '../context';
Expand Down
26 changes: 21 additions & 5 deletions packages/core/src/core/orm/query-builder/query-builder.factory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@

import { Repository } from 'typeorm';
import { EntityRepository } from '@mikro-orm/knex';
import { MultiORMEnum } from '../../../core/utils';
import { IQueryBuilder } from './iquery-builder';
import { TypeOrmQueryBuilder } from './typeorm-query-builder';
import { MikroOrmQueryBuilder } from './mikro-orm-query-builder';
import { MultiORMEnum } from 'core/utils';

export function createQueryBuilder<Entity>(repository: Repository<Entity> | EntityRepository<any>): IQueryBuilder<Entity> {
type CombinedEntityRepository<Entity> = Repository<Entity> | EntityRepository<any>;

/**
* Creates a query builder for a given repository, supporting both TypeORM and MikroORM.
*
* @param repository - A TypeORM `Repository` or a MikroORM `EntityRepository`.
* @returns A `TypeOrmQueryBuilder` or `MikroOrmQueryBuilder` depending on the repository type.
* @throws Error if the repository is neither TypeORM nor MikroORM.
*/
export function createQueryBuilder<Entity>(repository: Repository<Entity> | EntityRepository<any>, alias?: string): IQueryBuilder<Entity> {
if (repository instanceof Repository) {
return new TypeOrmQueryBuilder(repository);
} else if (repository instanceof EntityRepository) {
Expand All @@ -15,11 +24,18 @@ export function createQueryBuilder<Entity>(repository: Repository<Entity> | Enti
throw new Error('Unsupported repository orm-type');
}


export function multiORMCreateQueryBuilder<Entity>(repository: Repository<Entity> | EntityRepository<any>, ormType: MultiORMEnum = MultiORMEnum.TypeORM): IQueryBuilder<Entity> {
/**
* Generates a query builder specific to the chosen ORM type (TypeORM or MikroORM).
*
* @param repository - A repository instance, either TypeORM `Repository` or MikroORM `EntityRepository`.
* @param ormType - Specifies the ORM type using `MultiORMEnum`, defaulting to TypeORM.
* @returns A query builder (`TypeOrmQueryBuilder` or `MikroOrmQueryBuilder`) based on the specified ORM.
* @throws Error if an unsupported `ormType` is provided.
*/
export function multiORMCreateQueryBuilder<Entity>(repository: CombinedEntityRepository<Entity>, ormType: MultiORMEnum = MultiORMEnum.TypeORM, alias?: string): IQueryBuilder<Entity> {
switch (ormType) {
case MultiORMEnum.MikroORM:
return new MikroOrmQueryBuilder(repository as EntityRepository<any>);
return new MikroOrmQueryBuilder(repository as EntityRepository<any>, alias);

case MultiORMEnum.TypeORM:
return new TypeOrmQueryBuilder(repository as Repository<Entity>);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { EntityManager, EntityRepository, AnyEntity } from '@mikro-orm/knex';

export class MikroOrmBaseEntityRepository<T extends Object> extends EntityRepository<T> {

/**
* Persists the given entity or array of entities in the database.
* This method schedules the entities for insertion into the database but does not execute
* the database operation immediately. It returns the EntityManager instance for further chaining.
*
* @param entity - The entity or array of entities to persist.
* @returns The EntityManager instance.
*/
persist(entity: AnyEntity | AnyEntity[]): EntityManager {
return this.em.persist(entity);
}

/**
* Persists the given entity or array of entities in the database and immediately
* executes the database operation to insert them. This method is asynchronous.
*
* @param entity - The entity or array of entities to persist and flush.
*/
async persistAndFlush(entity: AnyEntity | AnyEntity[]): Promise<void> {
await this.em.persistAndFlush(entity);
}

/**
* Schedules the given entity for removal from the database.
* Similar to 'persist', this method does not immediately execute the removal operation.
* It returns the EntityManager instance for further operations or chaining.
*
* @param entity - The entity to remove.
* @returns The EntityManager instance.
*/
remove(entity: AnyEntity): EntityManager {
return this.em.remove(entity);
}

/**
* Schedules the given entity for removal and immediately executes the database operation
* to remove it. This method is asynchronous, ensuring the entity is removed
* from the database once the promise resolves.
*
* @param entity - The entity to remove and flush.
*/
async removeAndFlush(entity: AnyEntity): Promise<void> {
await this.em.removeAndFlush(entity);
}

/**
* Executes all scheduled database operations like insertions, updates, and deletions
* that are queued in the EntityManager. This method is asynchronous and ensures
* that all changes are persisted in the database once the promise resolves.
*/
async flush(): Promise<void> {
return this.em.flush();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { Country } from '../country.entity';

export class MikroOrmCountryRepository extends EntityRepository<Country> { }
export class MikroOrmCountryRepository extends EntityRepository<Country> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { Currency } from '../currency.entity';

export class MikroOrmCurrencyRepository extends EntityRepository<Currency> { }
export class MikroOrmCurrencyRepository extends EntityRepository<Currency> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { CustomSmtp } from '../custom-smtp.entity';

export class MikroOrmCustomSmtpRepository extends EntityRepository<CustomSmtp> { }
export class MikroOrmCustomSmtpRepository extends EntityRepository<CustomSmtp> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { Deal } from '../deal.entity';

export class MikroOrmDealRepository extends EntityRepository<Deal> { }
export class MikroOrmDealRepository extends EntityRepository<Deal> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmailHistory } from '../email-history.entity';

export class MikroOrmEmailHistoryRepository extends EntityRepository<EmailHistory> { }
export class MikroOrmEmailHistoryRepository extends EntityRepository<EmailHistory> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmailReset } from '../email-reset.entity';

export class MikroOrmEmailResetRepository extends EntityRepository<EmailReset> { }
export class MikroOrmEmailResetRepository extends EntityRepository<EmailReset> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmailTemplate } from '../email-template.entity';

export class MikroOrmEmailTemplateRepository extends EntityRepository<EmailTemplate> { }
export class MikroOrmEmailTemplateRepository extends EntityRepository<EmailTemplate> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmployeeAppointment } from '../employee-appointment.entity';

export class MikroOrmEmployeeAppointmentRepository extends EntityRepository<EmployeeAppointment> { }
export class MikroOrmEmployeeAppointmentRepository extends EntityRepository<EmployeeAppointment> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmployeeAward } from '../employee-award.entity';

export class MikroOrmEmployeeAwardRepository extends EntityRepository<EmployeeAward> { }
export class MikroOrmEmployeeAwardRepository extends EntityRepository<EmployeeAward> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { JobSearchCategory } from '../job-search-category.entity';

export class MikroOrmJobSearchCategoryRepository extends EntityRepository<JobSearchCategory> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { JobSearchOccupation } from '../job-search-occupation.entity';

export class MikroOrmJobSearchOccupationRepository extends EntityRepository<JobSearchOccupation> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { EmployeeUpworkJobsSearchCriterion } from '../employee-upwork-jobs-search-criterion.entity';

export class MikroOrmEmployeeUpworkJobsSearchCriterionRepository extends EntityRepository<EmployeeUpworkJobsSearchCriterion> { }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityRepository } from '@mikro-orm/core';
import { EntityRepository } from '@mikro-orm/knex';
import { JobPresetUpworkJobSearchCriterion } from '../job-preset-upwork-job-search-criterion.entity';

export class MikroOrmJobPresetUpworkJobSearchCriterionRepository extends EntityRepository<JobPresetUpworkJobSearchCriterion> { }
Loading
Loading