UNPKG

@softkit/typeorm

Version:

This library has some useful utilities for typeorm, entities, repositories, useful subscribers, interceptors.

39 lines 1.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OptimisticLockingSubscriber = void 0; const tslib_1 = require("tslib"); const common_1 = require("@nestjs/common"); const exceptions_1 = require("@softkit/exceptions"); const typeorm_1 = require("typeorm"); let OptimisticLockingSubscriber = class OptimisticLockingSubscriber { constructor(dataSource) { this.dataSource = dataSource; dataSource.subscribers.push(this); } // eslint-disable-next-line @typescript-eslint/no-explicit-any,complexity beforeUpdate(event) { // To know if an entity has a version number, we check if versionColumn // is defined in the metadata of that entity. // event.databaseEntity doesn't work for update method if (event.metadata.versionColumn && event.entity && event.databaseEntity) { // Getting the current version of the requested entity update const versionFromUpdate = Reflect.get(event.entity, event.metadata.versionColumn.propertyName); // Getting the entity's version from the database const versionFromDatabase = event.databaseEntity[event.metadata.versionColumn.propertyName]; // they should match otherwise someone has changed it before if (versionFromDatabase !== versionFromUpdate) { throw new exceptions_1.OptimisticLockException(versionFromDatabase); } } } }; exports.OptimisticLockingSubscriber = OptimisticLockingSubscriber; exports.OptimisticLockingSubscriber = OptimisticLockingSubscriber = tslib_1.__decorate([ (0, common_1.Injectable)() /** * this subscriber works only with save method and doesn't work with update * */ , tslib_1.__metadata("design:paramtypes", [typeorm_1.DataSource]) ], OptimisticLockingSubscriber); //# sourceMappingURL=optimistic-locking.subscriber.js.map