feat: deleteIfNoRelations

This commit is contained in:
Ahmed Bouhuolia
2025-06-28 22:35:29 +02:00
parent 0ca98c7ae4
commit fa5c3bd955
11 changed files with 229 additions and 143 deletions

View File

@@ -1,4 +1,5 @@
import { QueryBuilder, Model } from 'objection';
import { ModelHasRelationsError } from '@/common/exceptions/ModelHasRelations.exception';
interface PaginationResult<M extends Model> {
results: M[];
@@ -32,15 +33,44 @@ export class PaginationQueryBuilder<
};
}) as unknown as PaginationQueryBuilderType<M>;
}
async deleteIfNoRelations({
type,
message,
}: {
type?: string;
message?: string;
}) {
const relationMappings = this.modelClass().relationMappings;
const relationNames = Object.keys(relationMappings || {});
if (relationNames.length === 0) {
// No relations defined
return this.delete();
}
const recordQuery = this.clone();
relationNames.forEach((relationName: string) => {
recordQuery.withGraphFetched(relationName);
});
const record = await recordQuery;
const hasRelations = relationNames.some((name) => {
const val = record[name];
return Array.isArray(val) ? val.length > 0 : val != null;
});
if (!hasRelations) {
return this.clone().delete();
} else {
throw new ModelHasRelationsError(type, message);
}
}
}
// New BaseQueryBuilder extending PaginationQueryBuilder
export class BaseQueryBuilder<
M extends Model,
R = M[],
> extends PaginationQueryBuilder<M, R> {
// You can add more shared query methods here in the future
changeAmount(whereAttributes, attribute, amount) {
const changeMethod = amount > 0 ? 'increment' : 'decrement';