UNPKG

graphile-build-pg

Version:

Build a GraphQL schema by reflection over a PostgreSQL schema. Easy to customize since it's built with plugins on graphile-build

59 lines (57 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; exports.formatSQLForDebugging = formatSQLForDebugging; var _debug = _interopRequireDefault(require("debug")); var _chalk = _interopRequireDefault(require("chalk")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function formatSQLForDebugging(sql) { let colourIndex = 0; const allowedColours = [_chalk.default.red, _chalk.default.green, _chalk.default.yellow, _chalk.default.blue, _chalk.default.magenta, _chalk.default.cyan, _chalk.default.white, _chalk.default.black]; function nextColor() { colourIndex = (colourIndex + 1) % allowedColours.length; return allowedColours[colourIndex]; } const colours = {}; /* Yep - that's `colour` from English and `ize` from American */ function colourize(str) { if (!colours[str]) { colours[str] = nextColor(); } return colours[str].bold.call(null, str); } let indentLevel = 0; function handleIndent(all, rawMatch) { const match = rawMatch.replace(/ $/, ""); if (match === "(") { indentLevel++; return match + "\n" + " ".repeat(indentLevel); } else if (match === ")") { indentLevel--; return "\n" + " ".repeat(indentLevel) + match; } else if (match === "),") { indentLevel--; return "\n" + " ".repeat(indentLevel) + match + "\n" + " ".repeat(indentLevel); } else if (match === ",") { return match + "\n" + " ".repeat(indentLevel); } else { return "\n" + " ".repeat(indentLevel) + match.replace(/^\s+/, ""); } } const tidySql = sql.replace(/\s+/g, " ").replace(/\s+(?=$|\n|\))/g, "").replace(/(\n|^|\()\s+/g, "$1").replace(/(\(|\)|\), ?|, ?| (select|insert|update|delete|from|where|and|or|order|limit)(?= ))/g, handleIndent).replace(/\(\s*([A-Za-z0-9_."' =]{1,50})\s*\)/g, "($1)").replace(/\(\s*(\([A-Za-z0-9_."' =]{1,50}\))\s*\)/g, "($1)").replace(/\n\s*and \(TRUE\)/g, _chalk.default.gray(" and (TRUE)")); const colouredSql = tidySql.replace(/__local_[0-9]+__/g, colourize); return colouredSql; } const rawDebugSql = (0, _debug.default)("graphile-build-pg:sql"); function debugSql(sql) { if (!rawDebugSql.enabled) { return; } rawDebugSql("%s", "\n" + formatSQLForDebugging(sql)); } Object.assign(debugSql, rawDebugSql); var _default = debugSql; exports.default = _default; //# sourceMappingURL=debugSql.js.map