UNPKG

@n4it/typeorm-audit

Version:

TypeORM Auditing: Create history tables and manage changes of entities automagically.

81 lines 3.25 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createHistoryInstance = exports.createHistoryEntity = void 0; const typeorm_1 = require("typeorm"); const types_1 = require("../types"); const is_1 = require("./is"); const createHistoryEntity = (opts) => { let HistoryEntity = class HistoryEntity { id; modifiedBy; entityType; entityId; data; action; modifiedAt; }; __decorate([ (0, typeorm_1.PrimaryGeneratedColumn)((0, is_1.isStringVersionType)(opts.primaryIdType) ? opts.primaryIdType : { type: opts.primaryIdType }), __metadata("design:type", Object) ], HistoryEntity.prototype, "id", void 0); __decorate([ (0, typeorm_1.Column)({ nullable: true, type: opts.modifiedByColumnType, }), __metadata("design:type", String) ], HistoryEntity.prototype, "modifiedBy", void 0); __decorate([ (0, typeorm_1.Column)({ nullable: true, }), __metadata("design:type", String) ], HistoryEntity.prototype, "entityType", void 0); __decorate([ (0, typeorm_1.Column)(), __metadata("design:type", String) ], HistoryEntity.prototype, "entityId", void 0); __decorate([ (0, typeorm_1.Column)(opts.jsonColumnType), __metadata("design:type", Object) ], HistoryEntity.prototype, "data", void 0); __decorate([ (0, typeorm_1.Column)(), __metadata("design:type", String) ], HistoryEntity.prototype, "action", void 0); __decorate([ (0, typeorm_1.CreateDateColumn)(), __metadata("design:type", Date) ], HistoryEntity.prototype, "modifiedAt", void 0); HistoryEntity = __decorate([ (0, typeorm_1.Entity)({ name: opts.tableName }) ], HistoryEntity); return HistoryEntity; }; exports.createHistoryEntity = createHistoryEntity; const createHistoryInstance = ({ opts, target }, newEntity, action, userId) => { const instance = new target(); instance.data = opts.jsonColumnType?.includes('json') ? newEntity : JSON.stringify(newEntity); instance.action = action; instance.entityId = newEntity[opts.primaryIdColumn]; instance.modifiedBy = userId; if (opts.saveEntityType) { instance.entityType = target.name; } return instance; }; exports.createHistoryInstance = createHistoryInstance; //# sourceMappingURL=entity.js.map