nest-arango
Version:
ArangoDB driver module for NestJS with a built-in CLI tool for creating and running migration scripts
62 lines • 2.92 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.RunCommand = void 0;
const arangojs_1 = require("arangojs");
const fs_1 = require("fs");
const path_1 = require("path");
const collection_load_1 = require("./../utils/collection.load");
const migration_load_1 = require("./../utils/migration.load");
class RunCommand {
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
RETURN migration
`);
const processedMigrations = [];
while (cursor.hasNext) {
const migration = yield cursor.next();
processedMigrations.push(migration.name);
}
const migrationFiles = (0, fs_1.readdirSync)(migrationsDir).sort((a, b) => a.localeCompare(b));
for (const migrationFile of migrationFiles) {
try {
const migrationFilePath = (0, path_1.join)(migrationsDir, migrationFile);
const migration = yield (0, migration_load_1.loadMigration)(migrationFilePath);
if (processedMigrations.includes(migrationFile)) {
continue;
}
yield migration.up(database);
yield migrationCol.save({
name: migrationFile,
timestamp: Date.now(),
});
console.log(`\x1b[32m↑ \x1b[0mMigration \x1b[34m${migrationFilePath}\x1b[0m done.`);
}
catch (err) {
if (err instanceof Error) {
console.log(err.message);
}
else {
console.log(err);
}
return;
}
}
});
}
}
exports.RunCommand = RunCommand;
//# sourceMappingURL=run.command.js.map