nest-arango
Version:
ArangoDB driver module for NestJS with a built-in CLI tool for creating and running migration scripts
52 lines • 2.41 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 });
exports.RevertCommand = void 0;
const arangojs_1 = require("arangojs");
const path_1 = require("path");
const collection_load_1 = require("./../utils/collection.load");
const migration_load_1 = require("./../utils/migration.load");
class RevertCommand {
static execute(migrationsDir, database, collectionName) {
return __awaiter(this, void 0, void 0, function* () {
const migrationCol = yield (0, collection_load_1.loadMigrationCollection)(collectionName, database);
const cursor = yield database.query((0, arangojs_1.aql) `
WITH ${migrationCol}
FOR migration IN ${migrationCol}
SORT migration.timestamp DESC
LIMIT 1
RETURN migration
`);
if (!cursor.hasNext) {
return;
}
const lastProcessedMigration = yield cursor.next();
const migrationFilePath = (0, path_1.join)(migrationsDir, lastProcessedMigration.name);
try {
const migration = yield (0, migration_load_1.loadMigration)(migrationFilePath);
yield migration.down(database);
yield migrationCol.remove(lastProcessedMigration);
console.log(`\x1b[31m↓ \x1b[0mMigration \x1b[34m${migrationFilePath}\x1b[0m reverted.`);
}
catch (err) {
if (err instanceof Error) {
console.log(err.message);
}
else {
console.log(err);
}
return;
}
});
}
}
exports.RevertCommand = RevertCommand;
//# sourceMappingURL=revert.command.js.map