UNPKG

quick-erd

Version:

quick and easy text-based ERD + code generator for migration, query, typescript types and orm entity

105 lines (104 loc) 3.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.tableToString = tableToString; exports.fieldToString = fieldToString; exports.astToText = astToText; exports.printTables = printTables; exports.isInternalTable = isInternalTable; const enum_1 = require("./enum"); const guide_1 = require("./guide"); const meta_1 = require("./meta"); function tableToString(table) { return ` ${table.name} ${'-'.repeat(table.name.length)} ${table.field_list.map(fieldToString).join('\n')} `; } function fieldToString(field) { let type = field.type; if (type.match(/^enum/i)) { type = (0, enum_1.formatEnum)(type); } let text = `${field.name} ${type}`; if (field.is_unsigned) { text += ` unsigned`; } if (field.is_null) { text += ' NULL'; } if (field.is_unique) { text += ' unique'; } if (field.is_primary_key) { text += ' PK'; } if (field.references) { const ref = field.references; text += ` FK ${ref.type} ${ref.table}.${ref.field}`; } return text; } function astToText(ast) { let text = ''; text += (0, guide_1.makeGuide)('https://erd.surge.sh or https://quick-erd.surge.sh').replace(' or ', '\n# or '); for (const table of ast.table_list) { text += '\n\n\n' + tableToString(table).trim(); } text += '\n\n'; if (ast.zoom) { text += '\n' + (0, meta_1.zoomToLine)(ast.zoom); } if (ast.view) { text += '\n' + (0, meta_1.viewToLine)(ast.view); } if (ast.textBgColor) { text += '\n' + (0, meta_1.textBgColorToLine)(ast.textBgColor); } if (ast.textColor) { text += '\n' + (0, meta_1.textColorToLine)(ast.textColor); } if (ast.diagramBgColor) { text += '\n' + (0, meta_1.diagramBgColorToLine)(ast.diagramBgColor); } if (ast.diagramTextColor) { text += '\n' + (0, meta_1.diagramTextColorToLine)(ast.diagramTextColor); } if (ast.tableBgColor) { text += '\n' + (0, meta_1.tableBgColorToLine)(ast.tableBgColor); } if (ast.tableTextColor) { text += '\n' + (0, meta_1.tableTextColorToLine)(ast.tableTextColor); } for (const table of ast.table_list) { if (table.position) { text += '\n' + (0, meta_1.tableNameToLine)(table.name, table.position); } } return text.trim(); } function printTables(tables) { tables = skipInternalFields(tables); const text = astToText({ table_list: tables }); // eslint-disable-next-line no-console console.log(text); } function isInternalTable(name) { switch (name) { case 'knex_migrations': case 'knex_migrations_lock': case 'sqlite_sequence': return true; default: return false; } } const skip_fields = ['created_at', 'updated_at']; function skipInternalFields(tables) { return tables .filter(table => !isInternalTable(table.name)) .map(table => ({ ...table, field_list: table.field_list.filter(field => !skip_fields.includes(field.name)), })); }