UNPKG

typeorm-versions

Version:
126 lines 4.96 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 Version_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.Version = exports.VersionEvent = void 0; const class_transformer_1 = require("class-transformer"); const typeorm_1 = require("typeorm"); const SqliteDriver_1 = require("typeorm/driver/sqlite/SqliteDriver"); var VersionEvent; (function (VersionEvent) { VersionEvent["INSERT"] = "INSERT"; VersionEvent["UPDATE"] = "UPDATE"; VersionEvent["REMOVE"] = "REMOVE"; })(VersionEvent = exports.VersionEvent || (exports.VersionEvent = {})); let Version = Version_1 = class Version { /** * @deprecated in favor of useDataSource */ static useConnection(dataSource) { this.useDataSource(dataSource); } static useDataSource(dataSource) { this.usedDataSource = dataSource; } /** * @deprecated in favor of getDataSource */ getConnection() { return this.getDataSource(); } getDataSource() { return this.constructor.usedDataSource; } getObject() { for (const t of (0, typeorm_1.getMetadataArgsStorage)().tables) { if (t.target?.name === this.itemType) { return (0, class_transformer_1.plainToClass)(t.target, this.object, { enableImplicitConversion: true }); } } return {}; } previous() { // Date comparison workaround for SQLite let timestampQuery = (0, typeorm_1.LessThan)(this.timestamp); if (this.getDataSource().driver instanceof SqliteDriver_1.SqliteDriver) { timestampQuery = (0, typeorm_1.Raw)(alias => `STRFTIME('%Y-%m-%d %H:%M:%f', ${alias}) < STRFTIME('%Y-%m-%d %H:%M:%f', '${this.timestamp.toISOString()}')`); } return this.getDataSource().getRepository(Version_1).findOne({ where: { itemId: this.itemId, itemType: this.itemType, id: (0, typeorm_1.Not)(this.id), timestamp: timestampQuery, }, order: { timestamp: 'DESC' } }); } next() { // Date comparison workaround for SQLite let timestampQuery = (0, typeorm_1.MoreThan)(this.timestamp); if (this.getDataSource().driver instanceof SqliteDriver_1.SqliteDriver) { timestampQuery = (0, typeorm_1.Raw)(alias => `STRFTIME('%Y-%m-%d %H:%M:%f', ${alias}) > STRFTIME('%Y-%m-%d %H:%M:%f', '${this.timestamp.toISOString()}')`); } return this.getDataSource().getRepository(Version_1).findOne({ where: { itemId: this.itemId, itemType: this.itemType, id: (0, typeorm_1.Not)(this.id), timestamp: timestampQuery, }, order: { timestamp: 'DESC' } }); } async index() { const versions = await this.getDataSource().getRepository(Version_1).find({ where: { itemId: this.itemId, itemType: this.itemType, }, order: { timestamp: 'DESC' } }); return versions.indexOf(this); } }; __decorate([ (0, typeorm_1.PrimaryGeneratedColumn)(), __metadata("design:type", Number) ], Version.prototype, "id", void 0); __decorate([ (0, typeorm_1.Column)({ nullable: false }), __metadata("design:type", String) ], Version.prototype, "itemType", void 0); __decorate([ (0, typeorm_1.Column)({ nullable: false }), __metadata("design:type", String) ], Version.prototype, "itemId", void 0); __decorate([ (0, typeorm_1.Column)({ nullable: false }), __metadata("design:type", String) ], Version.prototype, "event", void 0); __decorate([ (0, typeorm_1.Column)(), __metadata("design:type", String) ], Version.prototype, "owner", void 0); __decorate([ (0, typeorm_1.Column)({ type: "simple-json" }), __metadata("design:type", Object) ], Version.prototype, "object", void 0); __decorate([ (0, typeorm_1.Column)({ precision: 6 }), __metadata("design:type", Date) ], Version.prototype, "timestamp", void 0); Version = Version_1 = __decorate([ (0, typeorm_1.Entity)() ], Version); exports.Version = Version; ; //# sourceMappingURL=Version.js.map