sequelize-typescript-migration-rafaeltab
Version:
migration tool for sequelize & typescript users
68 lines • 2.79 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const reverseSequelizeColType_1 = require("./reverseSequelizeColType");
const reverseSequelizeDefValueType_1 = require("./reverseSequelizeDefValueType");
const parseIndex_1 = require("./parseIndex");
function reverseModels(sequelize, models) {
const tables = {};
for (let [modelKey, model] of Object.entries(models)) {
const attributes = model.rawAttributes;
const resultAttributes = {};
for (let [column, attribute] of Object.entries(attributes)) {
let rowAttribute = {};
if (attribute.defaultValue) {
const _val = (0, reverseSequelizeDefValueType_1.default)(attribute.defaultValue);
if (_val.notSupported) {
console.log(`[Not supported] Skip defaultValue column of attribute ${model}:${column}`);
continue;
}
rowAttribute["defaultValue"] = _val;
}
if (attribute.type === undefined) {
console.log(`[Not supported] Skip column with undefined type ${model}:${column}`);
continue;
}
const seqType = (0, reverseSequelizeColType_1.default)(sequelize, attribute.type);
if (seqType === "Sequelize.VIRTUAL") {
console.log(`[SKIP] Skip Sequelize.VIRTUAL column "${column}"", defined in model "${model}"`);
continue;
}
rowAttribute = {
seqType: seqType,
};
[
"allowNull",
"unique",
"primaryKey",
"autoIncrement",
"autoIncrementIdentity",
"comment",
"references",
"onUpdate",
"onDelete",
"validate",
].forEach(key => {
if (attribute[key] !== undefined) {
rowAttribute[key] = attribute[key];
}
});
resultAttributes[column] = rowAttribute;
}
tables[model.tableName] = {
tableName: model.tableName,
schema: resultAttributes,
};
let idx_out = {};
if (model.options.indexes.length > 0) {
for (const _i in model.options.indexes) {
const index = (0, parseIndex_1.default)(model.options.indexes[_i]);
idx_out[`${index["hash"]}`] = index;
delete index["hash"];
}
}
tables[model.tableName].indexes = idx_out;
}
return tables;
}
exports.default = reverseModels;
//# sourceMappingURL=getTablesFromModels.js.map