json-api-nestjs
Version:
JsonApi Plugin for NestJs
42 lines • 1.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteRelationship = deleteRelationship;
const common_1 = require("@nestjs/common");
async function deleteRelationship(id, rel, input) {
const idsResult = await this.microOrmUtilService.validateRelationInputData(rel, input);
// const currentEntityRef = this.microOrmUtilService.entityManager.getReference(
// this.microOrmUtilService.entity,
// id as any
// );
const currentEntityRef = await this.microOrmUtilService
.queryBuilder()
.where({
[this.microOrmUtilService.currentPrimaryColumn]: id,
})
.getSingleResult();
if (!currentEntityRef) {
const error = {
code: 'invalid_arguments',
message: `Resource '${this.microOrmUtilService.currentAlias}' with id '${id}' does not exist`,
path: ['fields'],
};
throw new common_1.NotFoundException([error]);
}
await this.microOrmUtilService.entityManager.populate(currentEntityRef, [
rel,
]);
if (Array.isArray(idsResult)) {
const relEntity = this.microOrmUtilService.getRelation(rel).entity();
const relRef = idsResult.map((i) => this.microOrmUtilService.entityManager.getReference(relEntity, i));
currentEntityRef[rel].remove(...relRef);
}
else {
if (currentEntityRef[rel][this.microOrmUtilService.getPrimaryNameFor(rel)] ==
idsResult) {
// @ts-ignore
currentEntityRef[rel] = null;
}
}
await this.microOrmUtilService.entityManager.flush();
}
//# sourceMappingURL=delete-relationship.js.map