@dbml/core
Advanced tools
Comparing version 2.3.1 to 2.4.0
@@ -12,2 +12,4 @@ "use strict"; | ||
var _config = require("../model_structure/config"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
@@ -59,3 +61,9 @@ | ||
var field = model.fields[fieldId]; | ||
var line = "\"".concat(field.name, "\" ").concat(DbmlExporter.hasWhiteSpace(field.type.type_name) || DbmlExporter.hasSquareBracket(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name); | ||
var schemaName = ''; | ||
if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) { | ||
schemaName = DbmlExporter.hasWhiteSpace(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, "."); | ||
} | ||
var line = "\"".concat(field.name, "\" ").concat(schemaName).concat(DbmlExporter.hasWhiteSpace(field.type.type_name) || DbmlExporter.hasSquareBracket(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name); | ||
var constraints = []; | ||
@@ -62,0 +70,0 @@ |
@@ -147,3 +147,3 @@ "use strict"; | ||
}); | ||
return tableStrs.length ? tableStrs.join('\n') : ''; | ||
return tableStrs; | ||
} | ||
@@ -203,3 +203,3 @@ }, { | ||
}); | ||
return strArr.length ? strArr.join('\n') : ''; | ||
return strArr; | ||
} | ||
@@ -246,3 +246,3 @@ }, { | ||
}); | ||
return indexArr.length ? indexArr.join('\n') : ''; | ||
return indexArr; | ||
} | ||
@@ -263,3 +263,3 @@ }, { | ||
}); | ||
return commentArr.length ? commentArr.join('\n') : ''; | ||
return commentArr; | ||
} | ||
@@ -269,8 +269,4 @@ }, { | ||
value: function _export(model) { | ||
var res = ''; | ||
var hasBlockAbove = false; | ||
var database = model.database['1']; | ||
var indexIds = []; | ||
var comments = []; | ||
database.schemaIds.forEach(function (schemaId) { | ||
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) { | ||
var schema = model.schemas[schemaId]; | ||
@@ -281,23 +277,22 @@ var tableIds = schema.tableIds, | ||
if ((0, _utils.shouldPrintSchema)(schema, model)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += "CREATE DATABASE `".concat(schema.name, "`;\n"); | ||
hasBlockAbove = true; | ||
prevStatements.schemas.push("CREATE SCHEMA `".concat(schema.name, "`;\n")); | ||
} | ||
if (!_lodash["default"].isEmpty(tableIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += MySQLExporter.exportTables(tableIds, model); | ||
hasBlockAbove = true; | ||
var _prevStatements$table; | ||
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(MySQLExporter.exportTables(tableIds, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += MySQLExporter.exportRefs(refIds, model); | ||
hasBlockAbove = true; | ||
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
var _prevStatements$index; | ||
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(MySQLExporter.exportIndexes(indexIds, model))); | ||
} | ||
indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})))); | ||
comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
var note = model.tables[tableId].note; | ||
@@ -308,17 +303,28 @@ return note ? [{ | ||
}] : []; | ||
})))); | ||
})); | ||
if (!_lodash["default"].isEmpty(commentNodes)) { | ||
var _prevStatements$comme; | ||
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(MySQLExporter.exportComments(commentNodes, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
var _prevStatements$refs; | ||
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(MySQLExporter.exportRefs(refIds, model))); | ||
} | ||
return prevStatements; | ||
}, { | ||
schemas: [], | ||
enums: [], | ||
tables: [], | ||
indexes: [], | ||
comments: [], | ||
refs: [] | ||
}); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += MySQLExporter.exportIndexes(indexIds, model); | ||
hasBlockAbove = true; | ||
} | ||
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n'); | ||
if (!_lodash["default"].isEmpty(comments)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += MySQLExporter.exportComments(comments, model); | ||
hasBlockAbove = true; | ||
} | ||
return res; | ||
@@ -325,0 +331,0 @@ } |
@@ -12,2 +12,4 @@ "use strict"; | ||
var _config = require("../model_structure/config"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
@@ -54,3 +56,3 @@ | ||
}); | ||
return enumArr.length ? enumArr.join('\n') : ''; | ||
return enumArr; | ||
} | ||
@@ -68,6 +70,11 @@ }, { | ||
line = "\"".concat(field.name, "\" ").concat(typeSerial); | ||
} else if ((0, _utils.hasWhiteSpace)(field.type.type_name)) { | ||
line = "\"".concat(field.name, "\" \"").concat(field.type.type_name, "\""); | ||
} else { | ||
line = "\"".concat(field.name, "\" ").concat(field.type.type_name); | ||
var schemaName = ''; | ||
if (field.type.schemaName && field.type.schemaName !== _config.DEFAULT_SCHEMA_NAME) { | ||
schemaName = (0, _utils.hasWhiteSpace)(field.type.schemaName) ? "\"".concat(field.type.schemaName, "\".") : "".concat(field.type.schemaName, "."); | ||
} | ||
var typeName = (0, _utils.hasWhiteSpace)(field.type.type_name) ? "\"".concat(field.type.type_name, "\"") : field.type.type_name; | ||
line = "\"".concat(field.name, "\" ").concat(schemaName).concat(typeName); | ||
} | ||
@@ -156,3 +163,3 @@ | ||
}); | ||
return tableStrs.length ? tableStrs.join('\n') : ''; | ||
return tableStrs; | ||
} | ||
@@ -212,3 +219,3 @@ }, { | ||
}); | ||
return strArr.length ? strArr.join('\n') : ''; | ||
return strArr; | ||
} | ||
@@ -261,3 +268,3 @@ }, { | ||
}); | ||
return indexArr.length ? indexArr.join('\n') : ''; | ||
return indexArr; | ||
} | ||
@@ -293,3 +300,3 @@ }, { | ||
}); | ||
return commentArr.length ? commentArr.join('\n') : ''; | ||
return commentArr; | ||
} | ||
@@ -299,8 +306,4 @@ }, { | ||
value: function _export(model) { | ||
var res = ''; | ||
var hasBlockAbove = false; | ||
var database = model.database['1']; | ||
var indexIds = []; | ||
var comments = []; | ||
database.schemaIds.forEach(function (schemaId) { | ||
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) { | ||
var schema = model.schemas[schemaId]; | ||
@@ -312,29 +315,28 @@ var tableIds = schema.tableIds, | ||
if ((0, _utils.shouldPrintSchema)(schema, model)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += "CREATE SCHEMA \"".concat(schema.name, "\";\n"); | ||
hasBlockAbove = true; | ||
prevStatements.schemas.push("CREATE SCHEMA \"".concat(schema.name, "\";\n")); | ||
} | ||
if (!_lodash["default"].isEmpty(enumIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += PostgresExporter.exportEnums(enumIds, model); | ||
hasBlockAbove = true; | ||
var _prevStatements$enums; | ||
(_prevStatements$enums = prevStatements.enums).push.apply(_prevStatements$enums, _toConsumableArray(PostgresExporter.exportEnums(enumIds, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(tableIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += PostgresExporter.exportTables(tableIds, model); | ||
hasBlockAbove = true; | ||
var _prevStatements$table; | ||
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(PostgresExporter.exportTables(tableIds, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += PostgresExporter.exportRefs(refIds, model); | ||
hasBlockAbove = true; | ||
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
var _prevStatements$index; | ||
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(PostgresExporter.exportIndexes(indexIds, model))); | ||
} | ||
indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})))); | ||
comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
var _model$tables$tableId = model.tables[tableId], | ||
@@ -356,17 +358,28 @@ fieldIds = _model$tables$tableId.fieldIds, | ||
}].concat(fieldObjects) : fieldObjects; | ||
})))); | ||
})); | ||
if (!_lodash["default"].isEmpty(commentNodes)) { | ||
var _prevStatements$comme; | ||
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(PostgresExporter.exportComments(commentNodes, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
var _prevStatements$refs; | ||
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(PostgresExporter.exportRefs(refIds, model))); | ||
} | ||
return prevStatements; | ||
}, { | ||
schemas: [], | ||
enums: [], | ||
tables: [], | ||
indexes: [], | ||
comments: [], | ||
refs: [] | ||
}); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += PostgresExporter.exportIndexes(indexIds, model); | ||
hasBlockAbove = true; | ||
} | ||
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n'); | ||
if (!_lodash["default"].isEmpty(comments)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += PostgresExporter.exportComments(comments, model); | ||
hasBlockAbove = true; | ||
} | ||
return res; | ||
@@ -373,0 +386,0 @@ } |
@@ -144,3 +144,3 @@ "use strict"; | ||
}); | ||
return tableStrs.length ? tableStrs.join('\n') : ''; | ||
return tableStrs; | ||
} | ||
@@ -200,3 +200,3 @@ }, { | ||
}); | ||
return strArr.length ? strArr.join('\n') : ''; | ||
return strArr; | ||
} | ||
@@ -238,3 +238,3 @@ }, { | ||
}); | ||
return indexArr.length ? indexArr.join('\n') : ''; | ||
return indexArr; | ||
} | ||
@@ -278,3 +278,3 @@ }, { | ||
}); | ||
return commentArr.length ? commentArr.join('\n') : ''; | ||
return commentArr; | ||
} | ||
@@ -284,8 +284,4 @@ }, { | ||
value: function _export(model) { | ||
var res = ''; | ||
var hasBlockAbove = false; | ||
var database = model.database['1']; | ||
var indexIds = []; | ||
var comments = []; | ||
database.schemaIds.forEach(function (schemaId) { | ||
var statements = database.schemaIds.reduce(function (prevStatements, schemaId) { | ||
var schema = model.schemas[schemaId]; | ||
@@ -296,29 +292,26 @@ var tableIds = schema.tableIds, | ||
if ((0, _utils.shouldPrintSchema)(schema, model)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += "CREATE SCHEMA [".concat(schema.name, "];\nGO\n"); | ||
hasBlockAbove = true; | ||
prevStatements.schemas.push("CREATE SCHEMA [".concat(schema.name, "]\nGO\n")); | ||
} | ||
if (!_lodash["default"].isEmpty(tableIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += SqlServerExporter.exportTables(tableIds, model); | ||
hasBlockAbove = true; | ||
var _prevStatements$table; | ||
(_prevStatements$table = prevStatements.tables).push.apply(_prevStatements$table, _toConsumableArray(SqlServerExporter.exportTables(tableIds, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += SqlServerExporter.exportRefs(refIds, model); | ||
hasBlockAbove = true; | ||
} /////////PUSH COMMENTS OF TABLE & COLUMN///////// | ||
// console.log(JSON.stringify(tableIds, null, 2)); | ||
var indexIds = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
var _prevStatements$index; | ||
indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
return model.tables[tableId].indexIds; | ||
})))); | ||
comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) { | ||
(_prevStatements$index = prevStatements.indexes).push.apply(_prevStatements$index, _toConsumableArray(SqlServerExporter.exportIndexes(indexIds, model))); | ||
} | ||
var commentNodes = _lodash["default"].flatten(tableIds.map(function (tableId) { | ||
var _model$tables$tableId = model.tables[tableId], | ||
fieldIds = _model$tables$tableId.fieldIds, | ||
note = _model$tables$tableId.note; | ||
var fieldObject = fieldIds.filter(function (fieldId) { | ||
var fieldObjects = fieldIds.filter(function (fieldId) { | ||
return model.fields[fieldId].note; | ||
@@ -335,18 +328,29 @@ }).map(function (fieldId) { | ||
tableId: tableId | ||
}].concat(fieldObject) : fieldObject; | ||
})))); | ||
}].concat(fieldObjects) : fieldObjects; | ||
})); | ||
if (!_lodash["default"].isEmpty(commentNodes)) { | ||
var _prevStatements$comme; | ||
(_prevStatements$comme = prevStatements.comments).push.apply(_prevStatements$comme, _toConsumableArray(SqlServerExporter.exportComments(commentNodes, model))); | ||
} | ||
if (!_lodash["default"].isEmpty(refIds)) { | ||
var _prevStatements$refs; | ||
(_prevStatements$refs = prevStatements.refs).push.apply(_prevStatements$refs, _toConsumableArray(SqlServerExporter.exportRefs(refIds, model))); | ||
} | ||
return prevStatements; | ||
}, { | ||
schemas: [], | ||
enums: [], | ||
tables: [], | ||
indexes: [], | ||
comments: [], | ||
refs: [] | ||
}); | ||
if (!_lodash["default"].isEmpty(indexIds)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += SqlServerExporter.exportIndexes(indexIds, model); | ||
hasBlockAbove = true; | ||
} | ||
var res = _lodash["default"].concat(statements.schemas, statements.enums, statements.tables, statements.indexes, statements.comments, statements.refs).join('\n'); | ||
if (!_lodash["default"].isEmpty(comments)) { | ||
if (hasBlockAbove) res += '\n'; | ||
res += SqlServerExporter.exportComments(comments, model); | ||
hasBlockAbove = true; | ||
} | ||
return res; | ||
@@ -353,0 +357,0 @@ } |
@@ -14,3 +14,3 @@ "use strict"; | ||
var _enum = _interopRequireDefault(require("./enum")); | ||
var _enum2 = _interopRequireDefault(require("./enum")); | ||
@@ -76,3 +76,5 @@ var _tableGroup = _interopRequireDefault(require("./tableGroup")); | ||
_ref$project = _ref.project, | ||
project = _ref$project === void 0 ? {} : _ref$project; | ||
project = _ref$project === void 0 ? {} : _ref$project, | ||
_ref$aliases = _ref.aliases, | ||
aliases = _ref$aliases === void 0 ? [] : _ref$aliases; | ||
@@ -90,6 +92,9 @@ _classCallCheck(this, Database); | ||
_this.databaseType = project.database_type; | ||
_this.name = project.name; // The process order is important. Do not change ! | ||
_this.name = project.name; | ||
_this.aliases = aliases; // The process order is important. Do not change ! | ||
_this.processSchemas(schemas); | ||
_this.processSchemaElements(enums, _config.ENUM); | ||
_this.processSchemaElements(tables, _config.TABLE); | ||
@@ -99,4 +104,2 @@ | ||
_this.processSchemaElements(enums, _config.ENUM); | ||
_this.processSchemaElements(tableGroups, _config.TABLE_GROUP); | ||
@@ -148,4 +151,3 @@ | ||
if (element.schemaName === _config.DEFAULT_SCHEMA_NAME) { | ||
_this3.hasDefaultSchema = true; | ||
if (element.schemaName === _config.DEFAULT_SCHEMA_NAME) {// this.hasDefaultSchema = true; | ||
} | ||
@@ -164,3 +166,3 @@ } else { | ||
case _config.ENUM: | ||
schema.pushEnum(new _enum["default"](_objectSpread(_objectSpread({}, element), {}, { | ||
schema.pushEnum(new _enum2["default"](_objectSpread(_objectSpread({}, element), {}, { | ||
schema: schema | ||
@@ -206,13 +208,52 @@ }))); | ||
}, { | ||
key: "findTableAlias", | ||
value: function findTableAlias(alias) { | ||
var sym = this.aliases.find(function (a) { | ||
return a.name === alias; | ||
}); | ||
if (!sym || sym.kind !== 'table') return null; | ||
var schemaName = sym.value.schemaName || _config.DEFAULT_SCHEMA_NAME; | ||
var schema = this.schemas.find(function (s) { | ||
return s.name === schemaName; | ||
}); | ||
if (!schema) return null; | ||
var tableName = sym.value.tableName; | ||
var table = schema.tables.find(function (t) { | ||
return t.name === tableName; | ||
}); | ||
return table; | ||
} | ||
}, { | ||
key: "findTable", | ||
value: function findTable(rawTable) { | ||
var schema = this.findOrCreateSchema(rawTable.schemaName || _config.DEFAULT_SCHEMA_NAME); | ||
value: function findTable(schemaName, tableName) { | ||
var table = null; | ||
if (!schemaName) { | ||
table = this.findTableAlias(tableName); | ||
if (table) return table; | ||
} | ||
var schema = this.findOrCreateSchema(schemaName || _config.DEFAULT_SCHEMA_NAME); | ||
if (!schema) { | ||
this.error("Schema ".concat(rawTable.schemaName || _config.DEFAULT_SCHEMA_NAME, " don't exist")); | ||
this.error("Schema ".concat(schemaName || _config.DEFAULT_SCHEMA_NAME, " don't exist")); | ||
} | ||
return schema.findTable(rawTable.name); | ||
return schema.findTable(tableName); | ||
} | ||
}, { | ||
key: "findEnum", | ||
value: function findEnum(schemaName, name) { | ||
var schema = this.schemas.find(function (s) { | ||
return s.name === schemaName || s.alias === schemaName; | ||
}); | ||
if (!schema) return null; | ||
var _enum = schema.enums.find(function (e) { | ||
return e.name === name; | ||
}); | ||
return _enum; | ||
} | ||
}, { | ||
key: "export", | ||
@@ -219,0 +260,0 @@ value: function _export() { |
@@ -75,6 +75,6 @@ "use strict"; | ||
var schema = ref.schema.database.findOrCreateSchema(schemaName || _config.DEFAULT_SCHEMA_NAME); | ||
var table = schema.findTable(tableName); | ||
var table = schema.database.findTable(schemaName, tableName); | ||
if (!table) { | ||
_this.error("Can't find table ".concat((0, _utils.shouldPrintSchema)(schema) ? "\"".concat(schema.name, "\".") : '', "\"").concat(tableName, "\"")); | ||
_this.error("Can't find table ".concat((0, _utils.shouldPrintSchemaName)(schemaName) ? "\"".concat(schemaName, "\".") : '', "\"").concat(tableName, "\"")); | ||
} | ||
@@ -81,0 +81,0 @@ |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _config = require("./config"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } | ||
@@ -74,3 +76,3 @@ | ||
_this.name = name; // type : { type_name, value } | ||
_this.name = name; // type : { type_name, value, schemaName } | ||
@@ -90,2 +92,4 @@ _this.type = type; | ||
_this.bindType(); | ||
return _this; | ||
@@ -100,2 +104,28 @@ } | ||
}, { | ||
key: "bindType", | ||
value: function bindType() { | ||
var typeName = this.type.type_name; | ||
var typeSchemaName = this.type.schemaName || _config.DEFAULT_SCHEMA_NAME; | ||
if (this.type.schemaName) { | ||
var _enum = this.table.schema.database.findEnum(typeSchemaName, typeName); | ||
if (!_enum) { | ||
this.error("Cannot find type ".concat(typeSchemaName, " in schema ").concat(typeSchemaName)); | ||
return; | ||
} | ||
this._enum = _enum; | ||
_enum.pushField(this); | ||
} else { | ||
var _enum2 = this.table.schema.database.findEnum(typeSchemaName, typeName); | ||
if (!_enum2) return; | ||
this._enum = _enum2; | ||
_enum2.pushField(this); | ||
} | ||
} | ||
}, { | ||
key: "pushEndpoint", | ||
@@ -102,0 +132,0 @@ value: function pushEndpoint(endpoint) { |
@@ -103,4 +103,3 @@ "use strict"; | ||
if (endpoint.schemaName === _config.DEFAULT_SCHEMA_NAME) { | ||
_this2.database.hasDefaultSchema = true; | ||
if (endpoint.schemaName === _config.DEFAULT_SCHEMA_NAME) {// this.schema.database.hasDefaultSchema = true; | ||
} | ||
@@ -107,0 +106,0 @@ }); |
@@ -91,6 +91,6 @@ "use strict"; | ||
_this.processEnums(enums); | ||
_this.processTables(tables); | ||
_this.processEnums(enums); | ||
_this.processRefs(refs); | ||
@@ -138,3 +138,3 @@ | ||
return this.tables.find(function (t) { | ||
return t.name === tableName || t.alias === tableName; | ||
return t.name === tableName; | ||
}); | ||
@@ -158,3 +158,2 @@ } | ||
this.enums.push(_enum); | ||
this.bindEnumToField(_enum); | ||
} | ||
@@ -171,17 +170,2 @@ }, { | ||
}, { | ||
key: "bindEnumToField", | ||
value: function bindEnumToField(_enum) { | ||
this.database.schemas.forEach(function (schema) { | ||
schema.tables.forEach(function (table) { | ||
table.fields.forEach(function (field) { | ||
if (_enum.name === field.type.type_name && (field.type.schemaName || _config.DEFAULT_SCHEMA_NAME) === schema.name) { | ||
field._enum = _enum; | ||
_enum.pushField(field); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: "processRefs", | ||
@@ -188,0 +172,0 @@ value: function processRefs(rawRefs) { |
@@ -83,3 +83,3 @@ "use strict"; | ||
rawTables.forEach(function (rawTable) { | ||
var table = _this2.schema.database.findTable(rawTable); | ||
var table = _this2.schema.database.findTable(rawTable.schemaName, rawTable.name); | ||
@@ -86,0 +86,0 @@ if (!table) { |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.shouldPrintSchema = shouldPrintSchema; | ||
exports.shouldPrintSchemaName = shouldPrintSchemaName; | ||
@@ -13,2 +14,6 @@ var _config = require("./config"); | ||
return schema.name !== _config.DEFAULT_SCHEMA_NAME || schema.name === _config.DEFAULT_SCHEMA_NAME && schema.database.hasDefaultSchema; | ||
} | ||
function shouldPrintSchemaName(schemaName) { | ||
return schemaName !== _config.DEFAULT_SCHEMA_NAME; | ||
} |
@@ -5,5 +5,9 @@ "use strict"; | ||
var _require = require('../utils'), | ||
getFullTableName = _require.getFullTableName; | ||
function makeEndPoint(tableName, columnName, relation) { | ||
return { | ||
tableName: tableName[tableName.length - 1], | ||
tableName: tableName.name, | ||
schemaName: tableName.schemaName, | ||
fieldNames: columnName, | ||
@@ -28,3 +32,4 @@ relation: relation | ||
var value = {}; | ||
value.endpoint = makeEndPoint(tableName, columnName, '1'); | ||
var fullTableName = getFullTableName(tableName); | ||
value.endpoint = makeEndPoint(fullTableName, columnName, '1'); | ||
setOption(value, fkOptions); | ||
@@ -48,2 +53,3 @@ return { | ||
var value = {}; | ||
var fullTableName = getFullTableName(tableName); | ||
@@ -61,3 +67,4 @@ if (!endpoint2) { | ||
endpoint2.value.relation = '1'; | ||
endpoint2.value.tableName = _.last(tableName); | ||
endpoint2.value.tableName = fullTableName.name; | ||
endpoint2.value.schemaName = fullTableName.schemaName; | ||
value.endpoints = [endpoint1.value, endpoint2.value]; | ||
@@ -64,0 +71,0 @@ setOption(value, fkOptions); |
@@ -5,6 +5,9 @@ "use strict"; | ||
function findTable(ast, tableName) { | ||
return ast.tables.find(function (_table) { | ||
return _table.name === tableName; | ||
function findTable(ast, tableName, schemaName) { | ||
var realSchemaName = schemaName || 'public'; | ||
var table = ast.tables.find(function (t) { | ||
var targetSchemaName = t.schemaName || 'public'; | ||
return targetSchemaName === realSchemaName && t.name === tableName; | ||
}); | ||
return table; | ||
} | ||
@@ -19,3 +22,3 @@ | ||
function handleIndexes(index, ast) { | ||
var table = findTable(ast, index.tableName); | ||
var table = findTable(ast, index.tableName, index.schemaName); | ||
table.indexes.push(index); | ||
@@ -42,3 +45,3 @@ index.tableName = null; | ||
function handleDefaults(dbdefault, ast) { | ||
var table = findTable(ast, dbdefault.tableName); | ||
var table = findTable(ast, dbdefault.tableName, dbdefault.schemaName); | ||
var field = findField(table, dbdefault.fieldName); | ||
@@ -51,3 +54,3 @@ dbdefault.fieldName = null; | ||
function handleEnums(_enum, ast) { | ||
var table = findTable(ast, _enum.tableName); | ||
var table = findTable(ast, _enum.tableName, _enum.schemaName); | ||
var field = findField(table, _enum.fieldName); | ||
@@ -54,0 +57,0 @@ _enum.name = "".concat(_enum.tableName, "_").concat(_enum.fieldName, "_enum"); |
@@ -5,2 +5,5 @@ "use strict"; | ||
var _require = require('../../../utils'), | ||
getFullTableName = _require.getFullTableName; | ||
function handleRef(tableName, result) { | ||
@@ -10,11 +13,14 @@ var endpointWithNoTableName = result.value.endpoints.find(function (ele) { | ||
}); | ||
endpointWithNoTableName.tableName = _.last(tableName); | ||
endpointWithNoTableName.tableName = tableName.name; | ||
endpointWithNoTableName.schemaName = tableName.schemaName; | ||
} | ||
function addTableName(tableName, result) { | ||
result.value.tableName = _.last(tableName); | ||
result.value.tableName = tableName.name; | ||
result.value.schemaName = tableName.schemaName; | ||
} | ||
function handleAlterTableResult(_keyword, tableName, results) { | ||
if (!results) return null; // eslint-disable-next-line consistent-return | ||
if (!results) return null; | ||
var fullName = getFullTableName(tableName); // eslint-disable-next-line consistent-return | ||
@@ -25,3 +31,3 @@ results.forEach(function (result) { | ||
case 'refs': | ||
handleRef(tableName, result); | ||
handleRef(fullName, result); | ||
break; | ||
@@ -32,3 +38,3 @@ | ||
case 'enums': | ||
addTableName(tableName, result); | ||
addTableName(fullName, result); | ||
break; | ||
@@ -35,0 +41,0 @@ |
"use strict"; | ||
var _require = require('../../../utils'), | ||
getFullTableName = _require.getFullTableName; | ||
function makeIndex(_create, isUnique, _clustered, _index, indexName, _on, tableName, columnNames) { | ||
var fullTableName = getFullTableName(tableName); | ||
var columns = []; | ||
@@ -16,3 +20,4 @@ columnNames.forEach(function (columnName) { | ||
unique: isUnique ? true : null, | ||
tableName: tableName[tableName.length - 1], | ||
tableName: fullTableName.name, | ||
schemaName: fullTableName.schemaName, | ||
columns: columns | ||
@@ -19,0 +24,0 @@ } |
@@ -11,2 +11,5 @@ "use strict"; | ||
var _require = require('../../../utils'), | ||
getFullTableName = _require.getFullTableName; | ||
function createRefFromInlineRef(linesRefs, inlineRefs, fieldName, tableName) { | ||
@@ -20,3 +23,4 @@ if (!inlineRefs || inlineRefs.length === 0) return; | ||
newRef.endpoints.push({ | ||
tableName: tableName, | ||
tableName: tableName.name, | ||
schemaName: tableName.schemaName, | ||
fieldNames: [fieldName], | ||
@@ -38,3 +42,3 @@ relation: '*' | ||
var fieldType = fieldValue.type; | ||
_enum.name = "".concat(tableName, "_").concat(fieldValue.enums.name); | ||
_enum.name = "".concat(tableName.schemaName ? "".concat(tableName.schemaName, "_") : '').concat(tableName.name, "_").concat(fieldValue.enums.name); | ||
fieldType.type_name = _enum.name; | ||
@@ -77,3 +81,4 @@ fieldType.args = _enum.values.map(function (value) { | ||
var ref = line.value; | ||
ref.endpoints[0].tableName = tableName; | ||
ref.endpoints[0].tableName = tableName.name; | ||
ref.endpoints[0].schemaName = tableName.schemaName; | ||
} | ||
@@ -91,10 +96,7 @@ | ||
function makeTable(_keyword, tableName, _keyword2, lines) { | ||
var linesValue = getLinesValue(lines, _.last(tableName)); | ||
var fullName = getFullTableName(tableName); | ||
var linesValue = getLinesValue(lines, fullName); | ||
return { | ||
type: 'tables', | ||
value: _objectSpread(_objectSpread({ | ||
name: _.last(tableName) | ||
}, linesValue.value), {}, { | ||
schemaName: tableName.length > 1 ? tableName[tableName.length - 2] : null | ||
}) | ||
value: _objectSpread(_objectSpread({}, fullName), linesValue.value) | ||
}; | ||
@@ -101,0 +103,0 @@ } |
@@ -7,2 +7,4 @@ "use strict"; | ||
var _ = require('lodash'); | ||
var _require = require('./keyword_parsers'), | ||
@@ -51,2 +53,15 @@ LParen = _require.LParen, | ||
}; | ||
}; | ||
exports.getFullTableName = function (nameList) { | ||
var schemaName = null; | ||
if (nameList.length > 1) { | ||
schemaName = nameList[nameList.length - 2]; | ||
} | ||
return { | ||
name: _.last(nameList), | ||
schemaName: schemaName | ||
}; | ||
}; |
{ | ||
"name": "@dbml/core", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "> TODO: description", | ||
@@ -59,3 +59,3 @@ "author": "Holistics <dev@holistics.io>", | ||
}, | ||
"gitHead": "ddd4566d6773c3fb7893222a8903833e847a58b7" | ||
"gitHead": "bbc1d589c6dcefce089bd6007a24cad2808a3818" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1069967
24484