UNPKG

sequelize-typescript-migration

Version:
61 lines 1.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function sortActions(actions) { const orderedActionTypes = [ "removeIndex", "removeColumn", "dropTable", "createTable", "addColumn", "changeColumn", "addIndex", ]; const sortByLengthExists = (left, right) => { if (left.length === 0 && right.length > 0) { return -1; } if (right.length === 0 && left.length > 0) { return 1; } return 0; }; actions.sort((left, right) => { if (orderedActionTypes.indexOf(left.actionType) < orderedActionTypes.indexOf(right.actionType)) { return -1; } if (orderedActionTypes.indexOf(left.actionType) > orderedActionTypes.indexOf(right.actionType)) { return 1; } if (left.actionType === "dropTable" && right.actionType === "dropTable") { return sortByLengthExists(right.depends, left.depends); } return sortByLengthExists(left.depends, right.depends); }); for (let i = 0; i < actions.length; i++) { const leftAction = actions[i]; if (leftAction.depends.length === 0) { continue; } for (let j = 0; j < actions.length; j++) { const rightAction = actions[j]; if (rightAction.depends.length === 0) { continue; } if (leftAction.actionType != rightAction.actionType) { continue; } if (rightAction.depends.indexOf(leftAction.tableName) !== -1) { if (i > j) { const c = actions[i]; actions[i] = actions[j]; actions[j] = c; } } } } return actions; } exports.default = sortActions; //# sourceMappingURL=sortActions.js.map