UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

161 lines (160 loc) 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ColumnOptions = void 0; const utils_1 = require("../../../../shared/utils"); /** * Options of a table column. */ class ColumnOptions { unsigned; zerofill; charset; collation; nullable; default; autoincrement; unique; primary; comment; invisibleWithSystemVersioning; invisibleWithoutSystemVersioning; invisible; format; storage; onUpdate; /** * Creates column options instance from an array of definitions. * * @param json JSON format parsed from SQL. */ static fromArray(json) { const columnOptions = new ColumnOptions(); json.forEach((columnDefinition) => { Object.assign(columnOptions, columnDefinition.def); }); if (columnOptions.collation) { columnOptions.collation = columnOptions.collation.toLowerCase(); } if (columnOptions.charset) { columnOptions.charset = columnOptions.charset.toLowerCase(); } if (columnOptions.storage) { columnOptions.storage = columnOptions.storage.toLowerCase(); } if (columnOptions.format) { columnOptions.format = columnOptions.format.toLowerCase(); } /** * If column is not 'NOT NULL', consider it 'NULL DEFAULT NULL'. */ if (!(0, utils_1.isDefined)(columnOptions.nullable)) { columnOptions.nullable = true; } /** * If column has zerofill property, it is unsigned. * @see https://mariadb.com/kb/en/library/int/ */ if (columnOptions.zerofill) { columnOptions.unsigned = true; } /** * If column is primary key, then it is not nullable. */ if (columnOptions.primary) { columnOptions.nullable = false; } return columnOptions; } /** * JSON casting of this object calls this method. */ toJSON() { const json = {}; if ((0, utils_1.isDefined)(this.unsigned)) { json.unsigned = this.unsigned; } if ((0, utils_1.isDefined)(this.zerofill)) { json.zerofill = this.zerofill; } if ((0, utils_1.isDefined)(this.charset)) { json.charset = this.charset; } if ((0, utils_1.isDefined)(this.collation)) { json.collation = this.collation; } if ((0, utils_1.isDefined)(this.nullable)) { json.nullable = this.nullable; } if ((0, utils_1.isDefined)(this.nullable)) { json.nullable = this.nullable; } if ((0, utils_1.isDefined)(this.default)) { json.default = this.default; } if ((0, utils_1.isDefined)(this.autoincrement)) { json.autoincrement = this.autoincrement; } if ((0, utils_1.isDefined)(this.unique)) { json.unique = this.unique; } if ((0, utils_1.isDefined)(this.primary)) { json.primary = this.primary; } if ((0, utils_1.isDefined)(this.invisible)) { json.invisible = this.invisible; } if ((0, utils_1.isDefined)(this.format)) { json.format = this.format; } if ((0, utils_1.isDefined)(this.storage)) { json.storage = this.storage; } if ((0, utils_1.isDefined)(this.comment)) { json.comment = this.comment; } if ((0, utils_1.isDefined)(this.onUpdate)) { json.onUpdate = this.onUpdate; } /** * Change "null" string to null default column value. */ if ((0, utils_1.isString)(json.default) && json.default.toLowerCase() === 'null') { json.default = null; } if ((0, utils_1.isDefined)(this.invisibleWithSystemVersioning)) { json.invisibleWithSystemVersioning = this.invisibleWithSystemVersioning; } if ((0, utils_1.isDefined)(this.invisibleWithoutSystemVersioning)) { json.invisibleWithoutSystemVersioning = this.invisibleWithoutSystemVersioning; } return json; } /** * Merge this option instance with another one. * Common properties of this instance are overwritten. * * @param options JSON format parsed from SQL. */ mergeWith(options) { Object.getOwnPropertyNames(options).forEach((k) => { const value = options[k]; if ((0, utils_1.isDefined)(value)) { (0, utils_1.setProperty)(this, k, value); } }); } /** * Create a deep clone of this model. */ clone() { const options = new ColumnOptions(); Object.getOwnPropertyNames(this).forEach((k) => { const value = this[k]; if ((0, utils_1.isDefined)(value)) { (0, utils_1.setProperty)(options, k, value); } }); return options; } } exports.ColumnOptions = ColumnOptions;