@jungvonmatt/sb-migrate
Version:
CLI tool for managing Storyblok schema and content migrations
93 lines • 4.49 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleUpdateDatasource = void 0;
const picocolors_1 = __importDefault(require("picocolors"));
const api_1 = require("../../utils/api");
const lodash_1 = require("lodash");
const storyblok_1 = require("../../utils/storyblok");
const storyblok_2 = require("../../utils/storyblok");
/**
* Handles the update of an existing datasource based on the provided migration schema.
* This function performs the following operations:
* 1. Checks if the operation is a dry run
* 2. Verifies that either datasource updates or entries are provided
* 3. Retrieves the existing datasource
* 4. Updates datasource properties if provided
* 5. Updates datasource entries if provided
* 6. Creates a rollback file for the changes
*
* @param {Object} migration - The migration object containing the datasource updates
* @param {number | string} migration.id - The ID of the datasource to update
* @param {Partial<DatasourceMigration>} [migration.datasource] - Optional updates to the datasource properties
* @param {Omit<IPendingDataSourceEntry, "datasource_id">[]} [migration.entries] - Optional entries to update in the datasource
* @param {RunMigrationOptions} options - Configuration options for the migration
* @param {boolean} [options.isDryrun] - Whether to perform a dry run without making actual changes
* @throws {Error} If the datasource update fails
*/
const handleUpdateDatasource = async (migration, options) => {
console.log(`${picocolors_1.default.blue("-")} Updating datasource: ${migration.id}`);
if (options.isDryrun) {
console.log(`${picocolors_1.default.yellow("!")} Dry run - not updating datasource`);
console.log(migration);
return;
}
if (!migration.datasource && !migration.entries) {
console.error(`${picocolors_1.default.red("✗")} Must provide either datasource updates or entries`);
return;
}
try {
// First get the datasource details
const response = await api_1.api.datasources.get(migration.id);
const datasource = response.data.datasource;
// Store original datasource for rollback
const originalDatasource = (0, lodash_1.cloneDeep)(datasource);
// First update the datasource properties if provided
if (migration.datasource) {
if (migration.datasource.name) {
datasource.name = migration.datasource.name;
}
if (migration.datasource.slug) {
datasource.slug = migration.datasource.slug;
}
await api_1.api.datasources.update(datasource);
console.log(`${picocolors_1.default.green("✓")} Datasource properties updated successfully: ${datasource.name}`);
}
// Then handle entries if provided
if (migration.entries && migration.entries.length > 0) {
const entries = migration.entries || [];
const [, datasourceEntries] = await (0, storyblok_1.addOrUpdateDatasource)({
name: datasource.name,
slug: datasource.slug,
}, entries.map((entry) => ({
name: entry.name,
value: entry.value,
dimension_value: entry.dimension_value,
})));
console.log(`${picocolors_1.default.green("-")} Updated ${datasourceEntries.length} entries`);
}
// Create rollback data
const rollbackData = [
{
id: originalDatasource.id,
name: originalDatasource.name,
slug: originalDatasource.slug,
dimensions: originalDatasource.dimensions,
},
];
// Generate a unique identifier for this migration
const migrationId = typeof migration.id === "string"
? migration.id.replace(/[^a-zA-Z0-9]/g, "_")
: migration.id;
// Create rollback file
await (0, storyblok_2.createRollbackFile)(rollbackData, `datasource_${migrationId}`, "update");
}
catch (error) {
// Don't rethrow - let the calling function handle it
console.error(`${picocolors_1.default.red("✗")} Failed to update datasource:`, error);
}
};
exports.handleUpdateDatasource = handleUpdateDatasource;
//# sourceMappingURL=update.js.map