UNPKG

typeorm-revisions

Version:

Revisions History for [TypeORM](http://typeorm.io) Entities

203 lines 9.19 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); }; 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 typeorm_1 = require("typeorm"); const ulid_1 = require("ulid"); const index_1 = require("./index"); describe('e2e test', () => { let TestEntity = class TestEntity extends typeorm_1.BaseEntity { }; __decorate([ typeorm_1.PrimaryGeneratedColumn(), __metadata("design:type", Number) ], TestEntity.prototype, "id", void 0); __decorate([ typeorm_1.VersionColumn(), __metadata("design:type", Number) ], TestEntity.prototype, "version", void 0); __decorate([ typeorm_1.CreateDateColumn(), __metadata("design:type", Date) ], TestEntity.prototype, "createdAt", void 0); __decorate([ typeorm_1.Column(), __metadata("design:type", String) ], TestEntity.prototype, "test", void 0); TestEntity = __decorate([ typeorm_1.Entity() ], TestEntity); let TestHistoryEntity = class TestHistoryEntity extends TestEntity { }; __decorate([ typeorm_1.PrimaryGeneratedColumn(), __metadata("design:type", Number) ], TestHistoryEntity.prototype, "id", void 0); __decorate([ typeorm_1.Column(), __metadata("design:type", Number) ], TestHistoryEntity.prototype, "originalID", void 0); __decorate([ typeorm_1.CreateDateColumn(), __metadata("design:type", Date) ], TestHistoryEntity.prototype, "makeActionAt", void 0); __decorate([ index_1.HistoryActionColumn(), __metadata("design:type", String) ], TestHistoryEntity.prototype, "action", void 0); TestHistoryEntity = __decorate([ typeorm_1.Entity() ], TestHistoryEntity); let TestHistorySubscriber = class TestHistorySubscriber extends index_1.HistorySubscriber { constructor() { super(...arguments); this.entity = TestEntity; this.historyEntity = TestHistoryEntity; } }; TestHistorySubscriber = __decorate([ typeorm_1.EventSubscriber() ], TestHistorySubscriber); let TestEntity2 = class TestEntity2 extends typeorm_1.BaseEntity { }; __decorate([ typeorm_1.PrimaryGeneratedColumn(), __metadata("design:type", Number) ], TestEntity2.prototype, "id", void 0); __decorate([ typeorm_1.Column({ default: false, }), __metadata("design:type", Boolean) ], TestEntity2.prototype, "deleted", void 0); __decorate([ typeorm_1.Column(), __metadata("design:type", String) ], TestEntity2.prototype, "test", void 0); TestEntity2 = __decorate([ typeorm_1.Entity() ], TestEntity2); let TestHistoryEntity2 = class TestHistoryEntity2 extends TestEntity2 { }; __decorate([ typeorm_1.PrimaryGeneratedColumn(), __metadata("design:type", Number) ], TestHistoryEntity2.prototype, "id", void 0); __decorate([ typeorm_1.Column(), __metadata("design:type", Number) ], TestHistoryEntity2.prototype, "originalID", void 0); __decorate([ typeorm_1.CreateDateColumn(), __metadata("design:type", Date) ], TestHistoryEntity2.prototype, "makeActionAt", void 0); __decorate([ index_1.HistoryActionColumn(), __metadata("design:type", String) ], TestHistoryEntity2.prototype, "action", void 0); TestHistoryEntity2 = __decorate([ typeorm_1.Entity() ], TestHistoryEntity2); let TestHistorySubscriber2 = class TestHistorySubscriber2 extends index_1.HistorySubscriber { constructor() { super(...arguments); this.entity = TestEntity2; this.historyEntity = TestHistoryEntity2; } beforeHistory(action, history) { if (action === index_1.HistoryActionType.UPDATED && history.deleted) { history.action = index_1.HistoryActionType.DELETED; } } }; TestHistorySubscriber2 = __decorate([ typeorm_1.EventSubscriber() ], TestHistorySubscriber2); beforeEach(() => __awaiter(void 0, void 0, void 0, function* () { const connection = yield typeorm_1.createConnection({ database: 'test.sql', dropSchema: true, entities: [TestEntity, TestHistoryEntity, TestEntity2, TestHistoryEntity2], subscribers: [TestHistorySubscriber, TestHistorySubscriber2], synchronize: true, type: 'sqlite', username: 'root', }); expect(connection).toBeDefined(); expect(connection.isConnected).toBeTruthy(); })); it('create history', () => __awaiter(void 0, void 0, void 0, function* () { const testEntity = yield TestEntity.create({ test: 'test' }).save(); const histories = yield TestHistoryEntity.find(); expect(histories).toHaveLength(1); expect(histories[0].originalID).toBe(testEntity.id); expect(histories[0].action).toBe(index_1.HistoryActionType.CREATED); expect(histories[0].test).toBe('test'); })); it('update history', () => __awaiter(void 0, void 0, void 0, function* () { const testEntity = yield TestEntity.create({ test: 'test' }).save(); testEntity.test = 'updated'; yield testEntity.save(); const histories = yield TestHistoryEntity.find(); expect(histories).toHaveLength(2); expect(histories[0].action).toBe(index_1.HistoryActionType.CREATED); expect(histories[0].test).toBe('test'); expect(histories[1].action).toBe(index_1.HistoryActionType.UPDATED); expect(histories[1].test).toBe('updated'); })); it('delete history', () => __awaiter(void 0, void 0, void 0, function* () { const testEntity = yield TestEntity.create({ test: 'test' }).save(); yield testEntity.remove(); const histories = yield TestHistoryEntity.find(); expect(histories).toHaveLength(2); expect(histories[0].action).toBe(index_1.HistoryActionType.CREATED); expect(histories[1].action).toBe(index_1.HistoryActionType.DELETED); })); it('should be delete action when deleted column is true', () => __awaiter(void 0, void 0, void 0, function* () { const testEntity = yield TestEntity2.create({ test: 'test' }).save(); testEntity.deleted = true; yield testEntity.save(); const histories = yield TestHistoryEntity2.find(); expect(histories).toHaveLength(2); expect(histories[0].action).toBe(index_1.HistoryActionType.CREATED); expect(histories[0].deleted).toBeFalsy(); expect(histories[1].action).toBe(index_1.HistoryActionType.DELETED); expect(histories[1].deleted).toBeTruthy(); })); it('create many histories', () => __awaiter(void 0, void 0, void 0, function* () { let entities = Array(100) .fill(0) .map(() => TestEntity.create({ test: ulid_1.ulid() })); entities = yield TestEntity.save(entities); yield expect(TestHistoryEntity.count()).resolves.toBe(100); entities.forEach(e => (e.test = ulid_1.ulid())); yield TestEntity.save(entities); yield expect(TestHistoryEntity.count()).resolves.toBe(200); yield TestEntity.remove(entities); yield expect(TestHistoryEntity.count()).resolves.toBe(300); })); it('a', () => __awaiter(void 0, void 0, void 0, function* () { const testEntity = yield TestEntity.create({ test: 'test' }).save(); testEntity.test = 'updated'; yield testEntity.save(); yield testEntity.remove(); })); afterEach(() => typeorm_1.getConnection().close()); }); //# sourceMappingURL=historySubscriber.spec-e2e.js.map