2020-02-07 07:04:18 +04:00
|
|
|
import joi from '@hapi/joi';
|
|
|
|
|
|
|
|
export abstract class Migration {
|
|
|
|
protected abstract schema: joi.ObjectSchema;
|
|
|
|
protected abstract previous: Migration | null;
|
|
|
|
protected abstract migrate(data: any): any;
|
|
|
|
|
|
|
|
validate(data: any): joi.ValidationResult {
|
|
|
|
return this.schema.validate(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
forward(data: any): any {
|
|
|
|
const { error, value } = this.validate(data);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (this.previous) {
|
|
|
|
return this.migrate(this.previous.forward(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(
|
2020-02-14 05:18:23 +04:00
|
|
|
`Unable to migrate ${JSON.stringify(
|
|
|
|
data
|
|
|
|
)}. Reached the end of the migration chain.`
|
2020-02-07 07:04:18 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|