UNPKG

ddl-manager

Version:

store postgres procedures and triggers in files

90 lines 3.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.equalColumnName = exports.formatDefault = exports.Column = void 0; const Type_1 = require("./Type"); const Comment_1 = require("./Comment"); const constants_1 = require("../postgres/constants"); class Column { constructor(table, name, type, nulls, defaultValue, comment) { this.table = table; this.name = name.slice(0, constants_1.MAX_NAME_LENGTH); this.type = new Type_1.Type(type); this.default = defaultValue || null; this.nulls = nulls !== null && nulls !== void 0 ? nulls : true; this.comment = comment || Comment_1.Comment.frozen("column"); this.cacheSignature = this.comment.cacheSignature; this.frozen = this.comment.frozen; } getSignature() { return this.table.toString() + "." + this.name; } toJSON() { return { table: { schema: this.table.schema, name: this.table.name }, name: this.name, type: this.type.value, "default": this.default, cacheSignature: this.cacheSignature, comment: this.comment.toString() }; } equalName(column) { const otherName = typeof column === "string" ? column : column.name; return equalColumnName(this.name, otherName); } same(newColumn) { return (this.type.suit(newColumn.type) && formatDefault({ type: this.type.value, default: this.default }) === formatDefault({ type: newColumn.type.value, default: newColumn.default }) && this.comment.equal(newColumn.comment)); } clone(newParams = {}) { var _a, _b, _c; return new Column(this.table, this.name, (_a = newParams.type) !== null && _a !== void 0 ? _a : this.type.toString(), (_c = (_b = newParams.nulls) !== null && _b !== void 0 ? _b : this.nulls) !== null && _c !== void 0 ? _c : true, this.default, "comment" in newParams ? newParams.comment : this.comment); } isFrozen() { return this.comment.frozen; } markColumnAsFrozen(oldColumn) { this.comment = this.comment.markAsFrozen({ type: oldColumn.type.toString(), nulls: oldColumn.nulls }); } startMigration() { this.comment = this.comment.startMigration(); } finishMigration() { this.comment = this.comment.finishMigration(); } hasNotFinishedMigration() { return this.comment.hasNotFinishedMigration(); } } exports.Column = Column; function formatDefault(arg) { let someDefault = ("" + arg.default).trim().toLowerCase(); someDefault = someDefault.replace(/\s*::\s*([\w\s]+|numeric\([\d\s,]+\))(\[])?$/, ""); someDefault = someDefault.trim(); if (someDefault === "{}") { someDefault = "'{}'"; } return someDefault; } exports.formatDefault = formatDefault; function equalColumnName(nameA, nameB) { return (nameA.slice(0, constants_1.MAX_NAME_LENGTH) == nameB.slice(0, constants_1.MAX_NAME_LENGTH)); } exports.equalColumnName = equalColumnName; //# sourceMappingURL=Column.js.map