dynamit-cli
Version:
The DynamoDB migrations tool CLI
67 lines (66 loc) • 3.24 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dynamodb_1 = require("aws-sdk/clients/dynamodb");
const node_plop_1 = __importDefault(require("node-plop"));
const path_1 = __importDefault(require("path"));
const ramda_1 = require("ramda");
const umzug_1 = __importDefault(require("umzug"));
const logger_1 = __importDefault(require("../helpers/logger"));
const path_2 = require("../helpers/path");
const dynamodb_2 = __importDefault(require("../storages/dynamodb"));
exports.DynamoDBStorage = dynamodb_2.default;
class Migrator extends umzug_1.default {
/**
* Migrator factory function, creates an umzug instance with dynamodb storage.
* @param options
* @param options.region - an AWS Region
* @param options.dynamodb - a DynamoDB document client instance
* @param options.endpointUrl - an optional endpoint URL for local DynamoDB instances
* @param options.tableName - a name of migration table in DynamoDB
* @param options.attributeName - name of the table primaryKey attribute in DynamoDB
*/
constructor(options = {}) {
let { dynamodb, tableName, attributeName, migrationsPath } = options;
dynamodb = dynamodb || new dynamodb_1.DocumentClient(ramda_1.pick(['region', 'accessKeyId', 'secretAccessKey', 'endpointUrl'], options));
tableName = tableName || 'migrations';
attributeName = attributeName || 'name';
migrationsPath = migrationsPath || 'migrations';
super({
storage: new dynamodb_2.default({ dynamodb, tableName, attributeName }),
migrations: {
params: [dynamodb, options],
path: migrationsPath,
},
logging: logger_1.default.log
});
const plop = node_plop_1.default(path_1.default.join(__dirname, '../../.plop/plopfile.js'));
this.generator = plop.getGenerator('migration');
this.migrationsPath = migrationsPath;
}
generate(migrationName) {
return __awaiter(this, void 0, void 0, function* () {
yield this.generator.runActions({
migrationsPath: this.migrationsPath,
timestamp: path_2.getCurrentYYYYMMDDHHmms(),
name: migrationName
});
});
}
}
exports.Migrator = Migrator;
/**
* Migrator instance with options by default.
*/
exports.defaultMigrator = new Migrator();