UNPKG

sql-ddl-to-json-schema

Version:

Parse and convert SQL DDL statements to a JSON Schema.

141 lines (140 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PrimaryKey = void 0; const utils_1 = require("../../../../shared/utils"); const index_column_1 = require("./index-column"); const index_options_1 = require("./index-options"); /** * Primary key of a table. */ class PrimaryKey { name; indexType; columns; options; /** * Creates a primary key from a JSON def. * * @param json JSON format parsed from SQL. */ static fromDef(json) { if (json.id === 'O_CREATE_TABLE_CREATE_DEFINITION') { return PrimaryKey.fromObject(json.def.primaryKey); } throw new TypeError(`Unknown json id to build primary key from: ${json.id}`); } /** * Creates a primary key from an object containing needed properties. * * @param json Object containing properties. */ static fromObject(json) { const primaryKey = new PrimaryKey(); primaryKey.columns = json.columns.map(index_column_1.IndexColumn.fromDef); if (json.name) { primaryKey.name = json.name; } if (json.index) { primaryKey.indexType = json.index.def.toLowerCase(); } if (json.options && json.options.length) { primaryKey.options = index_options_1.IndexOptions.fromArray(json.options); } return primaryKey; } /** * JSON casting of this object calls this method. */ toJSON() { const json = { columns: (this.columns ?? []).map((c) => c.toJSON()), }; if ((0, utils_1.isDefined)(this.name)) { json.name = this.name; } if ((0, utils_1.isDefined)(this.options)) { json.options = this.options.toJSON(); } if ((0, utils_1.isDefined)(this.indexType)) { json.indexType = this.indexType; } return json; } /** * Create a deep clone of this model. */ clone() { const primaryKey = new PrimaryKey(); primaryKey.columns = (this.columns ?? []).map((c) => c.clone()); if ((0, utils_1.isDefined)(this.indexType)) { primaryKey.indexType = this.indexType; } if (this.options) { primaryKey.options = this.options.clone(); } return primaryKey; } /** * Pushes an index column to this primary key. * * @param {IndexColumn} indexColumn Index column to be pushed. */ pushColumn(indexColumn) { if (!this.columns) { this.columns = []; } this.columns.push(indexColumn); } /** * Drops a column from key. Returns whether column was removed. * * @param name Column name to be dropped. */ dropColumn(name) { if (!this.columns) { return false; } let pos = -1; const found = this.columns.some((c, i) => { pos = i; return c.column === name; }); if (!found || pos < 0) { return false; } const end = this.columns.splice(pos); end.shift(); this.columns = this.columns.concat(end); return true; } /** * Get the columns in given table which this * primary key's index columns refer to. * * @param table Table in question. */ getColumnsFromTable(table) { return (table.columns ?? []).filter((tableColumn) => (this.columns ?? []).some((indexColumn) => indexColumn.column === tableColumn.name)); } /** * Whether the given table has all of this primary key's columns. * * @param table Table in question. */ hasAllColumnsFromTable(table) { return ((table.columns ?? []).filter((tableColumn) => (this.columns ?? []).some((indexColumn) => indexColumn.column === tableColumn.name)).length === (this.columns ?? []).length); } /** * Rename index column name. * * @param column Column being renamed. * @param newName New column name. */ renameColumn(column, newName) { return this.columns?.filter((c) => c.column === column.name) .forEach((c) => { c.column = newName; }); } } exports.PrimaryKey = PrimaryKey;