object-migrations
Version:
Linear, in-memory migrations for versioned objects
67 lines (66 loc) • 2.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NoMigrationStepsError = exports.MigrationError = exports.MigratorError = void 0;
exports.describeVersion = describeVersion;
/**
* Base class for all errors thrown by the migrator.
*/
class MigratorError extends Error {
constructor(name, message) {
super(message);
this.name = name;
}
}
exports.MigratorError = MigratorError;
/**
* Wraps an error thrown within the migration function, while migrating an object between successive
* versions.
*/
class MigrationError extends MigratorError {
constructor(
/**
* Version from which migration was attempted (version of the object being migrated).
*/
from,
/**
* Version to which migration was attempted.
*/
to,
/**
* The underlying (wrapped) error.
*/
cause) {
super('MigrationError', `Error "${String(cause)}" thrown while migrating an object from version ` +
`${describeVersion(from)} to ${describeVersion(to)}`);
this.from = from;
this.to = to;
this.cause = cause;
}
}
exports.MigrationError = MigrationError;
/**
* Indicates that the sequence of steps to migrate an object between two versions couldn't be
* determined.
*
* Occurs when migrations between the versions (or a chain of migrations between successive versions
* starting & ending at the them) haven't been registered beforehand.
*/
class NoMigrationStepsError extends MigratorError {
constructor(
/**
* Version from which migration was attempted.
*/
from,
/**
* Version to which migration was attempted.
*/
to) {
super('NoMigrationStepsError', `No migration steps from version ${describeVersion(from)} to ${describeVersion(to)}`);
this.from = from;
this.to = to;
}
}
exports.NoMigrationStepsError = NoMigrationStepsError;
function describeVersion(v) {
return typeof v === 'function' && v.name !== '' ? v.name : String(v);
}