UNPKG

@caspiandb/data-api-migrations-serverless

Version:

Serverless plugin to start an Aurora Serverless Data API emulator for local development.

147 lines 6.71 kB
"use strict"; 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()); }); }; const chalk = require("chalk"); const data_api_migrations_1 = require("@caspiandb/data-api-migrations"); class DataAPIMigrationsServerless { constructor(serverless, options) { this.serverless = serverless; this.options = options; const commonOptions = { stage: { usage: 'The stage e.g. (local, dev, staging, prod, etc.)', required: false, default: 'local', type: 'string', } }; this.stage = options.stage || 'local'; const lifecycleEvents = this.stage === 'local' ? ['init', 'exec', 'end'] : ['exec']; this.commands = { migrations: { usage: 'Aurora Serverless DataAPI migration management.', lifecycleEvents: ['help'], commands: { create: { usage: 'Generate a new migration file.', lifecycleEvents: ['generate'], options: { name: { usage: 'Name of the migration e.g. sls migration create --name createUsersTable', required: true, shortcut: 'n', type: 'string', } } }, bump: { usage: 'Bump migration file version.', lifecycleEvents: ['bump'], options: { name: { usage: 'Name of the migration e.g. sls migration bump --name createUsersTable', required: true, shortcut: 'n', type: 'string,' } } }, apply: { usage: 'Apply all pending migrations.', lifecycleEvents, options: Object.assign({}, commonOptions) }, rollback: { usage: 'Rollback the most recent (applied) migration.', lifecycleEvents, options: Object.assign(Object.assign({}, commonOptions), { count: { usage: 'How many migrations to rollback, default: 1', required: false, shortcut: 'n', type: 'number', } }) }, status: { usage: 'List the migrations that have been applied.', lifecycleEvents, options: Object.assign({}, commonOptions) } } } }; this.hooks = { 'migrations:create:generate': this.generateMigrationFile.bind(this), 'migrations:bump:bump': this.bumpMigrationFile.bind(this), 'migrations:apply:exec': this.applyMigrations.bind(this), 'migrations:rollback:exec': this.rollbackMigrations.bind(this), 'migrations:status:exec': this.fetchMigrationStatus.bind(this) }; } manager() { return new data_api_migrations_1.default(Object.assign({ isLocal: this.stage === 'local', cwd: this.serverless.config.servicePath, logger: this.log.bind(this) }, this.config)); } generateMigrationFile() { return __awaiter(this, void 0, void 0, function* () { const fileName = yield this.manager().generateMigration(this.options.name); this.log(`${chalk.greenBright(fileName)} created.`); }); } bumpMigrationFile() { return __awaiter(this, void 0, void 0, function* () { const fileName = yield this.manager().bumpMigration(this.options.name); if (fileName) { this.log(`Migration ${this.options.name} bumped to ${chalk.greenBright(fileName)} .`); } else { throw new Error(`Migration for ${this.options.name}: not found`); } }); } applyMigrations() { return __awaiter(this, void 0, void 0, function* () { const ids = yield this.manager().applyMigrations(this.serverless); ids.forEach((id) => this.log(`${chalk.greenBright(id)} applied.`)); }); } rollbackMigrations() { return __awaiter(this, void 0, void 0, function* () { const count = this.options.count || 1; const ids = yield this.manager().rollbackMigrations(this.serverless, count); ids.forEach((id) => this.log(`${chalk.greenBright(id)} rolled back.`)); }); } fetchMigrationStatus() { return __awaiter(this, void 0, void 0, function* () { const ids = yield this.manager().getAppliedMigrationIds(); ids.forEach((id) => this.log(`${chalk.greenBright(id)} is applied.`)); }); } get config() { const baseConfig = this.serverless.service.custom['DataAPIMigrations']; if (baseConfig === undefined) { throw new Error('"custom"."DataAPIMigrations" is missing from serverless.yml'); } const { migrationsFolder = './migrations', typescript = true, [this.stage]: dataAPI, tsConfig, } = baseConfig; if (dataAPI === undefined) { throw new Error(`"custom"."DataAPIMigrations"."${this.stage}" is missing from serverless.yml`); } return { migrationsFolder, typescript, dataAPI, tsConfig, }; } log(message) { this.serverless.cli.log(`${chalk.magentaBright('Data API Migrations:')} ${message}`); } } module.exports = DataAPIMigrationsServerless; //# sourceMappingURL=index.js.map