sql-ddl-to-json-schema
Version:
Parse and convert SQL DDL statements to a JSON Schema.
148 lines (147 loc) • 4.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Column = void 0;
const utils_1 = require("../../../../shared/utils");
const column_reference_1 = require("./column-reference");
const column_options_1 = require("./column-options");
const index_column_1 = require("./index-column");
const primary_key_1 = require("./primary-key");
const foreign_key_1 = require("./foreign-key");
const unique_key_1 = require("./unique-key");
const datatype_1 = require("./datatype");
/**
* Table column.
*/
class Column {
name;
type;
reference;
options;
/**
* Creates a column from a JSON def.
*
* @param json JSON format parsed from SQL.
*/
static fromDef(json) {
if (json.id === 'O_CREATE_TABLE_CREATE_DEFINITION') {
const column = json.def.column;
return Column.fromObject({
name: column.name,
datatype: column.def.datatype,
reference: column.def.reference,
columnDefinition: column.def.columnDefinition,
});
}
throw new TypeError(`Unknown json id to build column from: ${json.id}`);
}
/**
* Creates a column from an object containing needed properties.
*
* @param json Object containing properties.
*/
static fromObject(json) {
const column = new Column();
column.name = json.name;
column.type = datatype_1.Datatype.fromDef(json.datatype);
if (json.reference) {
column.reference = column_reference_1.ColumnReference.fromDef(json.reference);
}
if (json.columnDefinition) {
column.options = column_options_1.ColumnOptions.fromArray(json.columnDefinition);
}
return column;
}
/**
* JSON casting of this object calls this method.
*/
toJSON() {
const json = {
name: this.name,
type: this.type.toJSON(),
};
if ((0, utils_1.isDefined)(this.options)) {
json.options = this.options.toJSON();
}
if ((0, utils_1.isDefined)(this.reference)) {
json.reference = this.reference.toJSON();
}
return json;
}
/**
* Create a deep clone of this model.
*/
clone() {
const column = new Column();
column.name = this.name;
column.type = this.type.clone();
if (this.options) {
column.options = this.options.clone();
}
return column;
}
/**
* Whether this column is primary key.
*/
isPrimaryKey() {
return this.options ? this.options.primary === true : false;
}
/**
* Whether this column is unique key.
*/
isUniqueKey() {
return this.options ? this.options.unique === true : false;
}
/**
* Whether this column is foreign key.
*/
isForeignKey() {
return !!this.reference;
}
/**
* Extracts instance of PrimaryKey if this column is primary key.
* Removes 'primary' property from options.
*/
extractPrimaryKey() {
if (!this.isPrimaryKey()) {
return undefined;
}
delete this.options.primary;
const indexColumn = new index_column_1.IndexColumn();
indexColumn.column = this.name;
const primaryKey = new primary_key_1.PrimaryKey();
primaryKey.pushColumn(indexColumn);
return primaryKey;
}
/**
* Extracts instance of ForeignKey if this column references other table.
* Removes 'reference' property from options.
*/
extractForeignKey() {
if (!this.isForeignKey()) {
return undefined;
}
const indexColumn = new index_column_1.IndexColumn();
indexColumn.column = this.name;
const foreignKey = new foreign_key_1.ForeignKey();
foreignKey.pushColumn(indexColumn);
foreignKey.reference = this.reference;
delete this.reference;
return foreignKey;
}
/**
* Extracts instance of UniqueKey if this column is unique key.
* Removes 'unique' property from options.
*/
extractUniqueKey() {
if (!this.isUniqueKey()) {
return undefined;
}
delete this.options.unique;
const indexColumn = new index_column_1.IndexColumn();
indexColumn.column = this.name;
const uniqueKey = new unique_key_1.UniqueKey();
uniqueKey.columns.push(indexColumn);
return uniqueKey;
}
}
exports.Column = Column;