typeorm-revisions
Version:
Revisions History for [TypeORM](http://typeorm.io) Entities
55 lines • 2.66 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const historyActionType_1 = require("./historyActionType");
class HistorySubscriber {
beforeHistory(action, history) { }
afterHistory(action, history) { }
listenTo() {
return this.entity;
}
createHistoryEntity(manager, entity) {
return manager.create(this.historyEntity, entity);
}
afterInsert(event) {
return __awaiter(this, void 0, void 0, function* () {
yield this.createHistory(event.manager, event.metadata, historyActionType_1.HistoryActionType.CREATED, event.entity);
});
}
afterUpdate(event) {
return __awaiter(this, void 0, void 0, function* () {
yield this.createHistory(event.manager, event.metadata, historyActionType_1.HistoryActionType.UPDATED, event.entity);
});
}
beforeRemove(event) {
return __awaiter(this, void 0, void 0, function* () {
yield this.createHistory(event.manager, event.metadata, historyActionType_1.HistoryActionType.DELETED, event.entity);
});
}
createHistory(manager, metadata, action, entity) {
return __awaiter(this, void 0, void 0, function* () {
if (!entity || Object.keys(metadata.propertiesMap).includes('action')) {
return;
}
const history = yield this.createHistoryEntity(manager, entity);
history.action = action;
for (const primaryColumn of metadata.primaryColumns) {
history.originalID = Reflect.get(history, primaryColumn.propertyName);
Reflect.deleteProperty(history, primaryColumn.propertyName);
}
yield this.beforeHistory(history.action, history);
yield manager.save(history);
yield this.afterHistory(history.action, history);
});
}
}
exports.HistorySubscriber = HistorySubscriber;
//# sourceMappingURL=historySubscriber.js.map