fix(backend): Workaround MikroORM syntax error
This commit is contained in:
parent
cac944e9c3
commit
2b90f20d45
1 changed files with 15 additions and 2 deletions
|
@ -1,12 +1,25 @@
|
|||
import { EntityRepository, FilterQuery } from '@mikro-orm/core';
|
||||
import { EntityRepository, FilterQuery, SyntaxErrorException } from '@mikro-orm/core';
|
||||
import { EntityAlreadyExistsException } from '../exceptions/entity-already-exists-exception.js';
|
||||
import { getLogger } from '../logging/initalize.js';
|
||||
|
||||
export abstract class DwengoEntityRepository<T extends object> extends EntityRepository<T> {
|
||||
public async save(entity: T, options?: { preventOverwrite?: boolean }): Promise<void> {
|
||||
if (options?.preventOverwrite && (await this.findOne(entity))) {
|
||||
throw new EntityAlreadyExistsException(`A ${this.getEntityName()} with this identifier already exists.`);
|
||||
}
|
||||
await this.getEntityManager().persistAndFlush(entity);
|
||||
try {
|
||||
await this.getEntityManager().persistAndFlush(entity);
|
||||
} catch (e: unknown) {
|
||||
// Workaround for MikroORM bug: Sometimes, queries are generated with random syntax errors.
|
||||
// The faulty query is then retried everytime something is persisted. By clearing the entity
|
||||
// Manager in that case, we make sure that future queries will work.
|
||||
if (e instanceof SyntaxErrorException) {
|
||||
getLogger().error("SyntaxErrorException caught => entity manager cleared.");
|
||||
this.em.clear();
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
public async deleteWhere(query: FilterQuery<T>): Promise<void> {
|
||||
const toDelete = await this.findOne(query);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue