New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@dbml/core

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dbml/core - npm Package Compare versions

Comparing version 2.0.1 to 2.1.0

lib/parse/mssql/base_parsers.js

23

lib/export/DbmlExporter.js

@@ -223,4 +223,14 @@ "use strict";

}, {
key: "buildFieldName",
value: function buildFieldName(fieldIds, model) {
var fieldNames = fieldIds.map(function (fieldId) {
return "\"".concat(model.fields[fieldId].name, "\"");
}).join(', ');
return fieldIds.length === 1 ? fieldNames : "(".concat(fieldNames, ")");
}
}, {
key: "exportRefs",
value: function exportRefs(refIds, model) {
var _this2 = this;
var strArr = refIds.map(function (refId) {

@@ -236,6 +246,8 @@ var ref = model.refs[refId];

var line = 'Ref';
var refEndpointField = model.fields[refEndpoint.fieldId];
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
var refEndpointTable = model.tables[refEndpointField.tableId];
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
var refEndpointFieldName = _this2.buildFieldName(refEndpoint.fieldIds, model, 'dbml');
if (ref.name) {

@@ -246,8 +258,11 @@ line += " ".concat((0, _utils.shouldPrintSchema)(model.schemas[ref.schemaId], model) ? "\"".concat(model.schemas[ref.schemaId].name, "\".") : '', "\"").concat(ref.name, "\"");

line += ':';
line += "".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\".\"").concat(refEndpointField.name, "\" ");
var foreignEndpointField = model.fields[foreignEndpoint.fieldId];
line += "".concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\".").concat(refEndpointFieldName, " ");
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
var foreignEndpointFieldName = _this2.buildFieldName(foreignEndpoint.fieldIds, model, 'dbml');
if (foreignEndpoint.relation === '1') line += '- ';else line += '< ';
line += "".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\".\"").concat(foreignEndpointField.name, "\"");
line += "".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\".").concat(foreignEndpointFieldName);
var refActions = [];

@@ -254,0 +269,0 @@

2

lib/export/ModelExporter.js

@@ -8,4 +8,2 @@ "use strict";

var _lodash = _interopRequireDefault(require("lodash"));
var _DbmlExporter = _interopRequireDefault(require("./DbmlExporter"));

@@ -12,0 +10,0 @@

@@ -148,4 +148,14 @@ "use strict";

}, {
key: "buildFieldName",
value: function buildFieldName(fieldIds, model) {
var fieldNames = fieldIds.map(function (fieldId) {
return "`".concat(model.fields[fieldId].name, "`");
}).join(', ');
return "(".concat(fieldNames, ")");
}
}, {
key: "exportRefs",
value: function exportRefs(refIds, model) {
var _this = this;
var strArr = refIds.map(function (refId) {

@@ -160,8 +170,14 @@ var ref = model.refs[refId];

var refEndpoint = model.endpoints[refEndpointId];
var refEndpointField = model.fields[refEndpoint.fieldId];
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
var refEndpointTable = model.tables[refEndpointField.tableId];
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
var foreignEndpointField = model.fields[foreignEndpoint.fieldId];
var refEndpointFieldName = _this.buildFieldName(refEndpoint.fieldIds, model, 'mysql');
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mysql');
var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "`".concat(foreignEndpointSchema.name, "`.") : '', "`").concat(foreignEndpointTable.name, "` ADD ");

@@ -173,3 +189,3 @@

line += "FOREIGN KEY (`".concat(foreignEndpointField.name, "`) REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` (`").concat(refEndpointField.name, "`)");
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "`".concat(refEndpointSchema.name, "`.") : '', "`").concat(refEndpointTable.name, "` ").concat(refEndpointFieldName);

@@ -232,2 +248,18 @@ if (ref.onDelete) {

}, {
key: "exportComments",
value: function exportComments(comments, model) {
var commentArr = comments.map(function (comment) {
var line = '';
if (comment.type === 'table') {
var table = model.tables[comment.tableId];
line += "ALTER TABLE `".concat(table.name, "` COMMENT = \"").concat(table.note.replace(/"/g, "'"), "\"");
}
line += ';\n';
return line;
});
return commentArr.length ? commentArr.join('\n') : '';
}
}, {
key: "export",

@@ -239,2 +271,3 @@ value: function _export(model) {

var indexIds = [];
var comments = [];
database.schemaIds.forEach(function (schemaId) {

@@ -266,2 +299,9 @@ var schema = model.schemas[schemaId];

}))));
comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
var note = model.tables[tableId].note;
return note ? [{
type: 'table',
tableId: tableId
}] : [];
}))));
});

@@ -275,2 +315,8 @@

if (!_lodash["default"].isEmpty(comments)) {
if (hasBlockAbove) res += '\n';
res += MySQLExporter.exportComments(comments, model);
hasBlockAbove = true;
}
return res;

@@ -277,0 +323,0 @@ }

@@ -62,3 +62,4 @@ "use strict";

if (field.increment) {
line = "\"".concat(field.name, "\" SERIAL");
var typeSerial = field.type.type_name === 'bigint' ? 'BIGSERIAL' : 'SERIAL';
line = "\"".concat(field.name, "\" ").concat(typeSerial);
} else if ((0, _utils.hasWhiteSpace)(field.type.type_name)) {

@@ -154,4 +155,14 @@ line = "\"".concat(field.name, "\" \"").concat(field.type.type_name, "\"");

}, {
key: "buildFieldName",
value: function buildFieldName(fieldIds, model) {
var fieldNames = fieldIds.map(function (fieldId) {
return "\"".concat(model.fields[fieldId].name, "\"");
}).join(', ');
return "(".concat(fieldNames, ")");
}
}, {
key: "exportRefs",
value: function exportRefs(refIds, model) {
var _this = this;
var strArr = refIds.map(function (refId) {

@@ -166,8 +177,14 @@ var ref = model.refs[refId];

var refEndpoint = model.endpoints[refEndpointId];
var refEndpointField = model.fields[refEndpoint.fieldId];
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
var refEndpointTable = model.tables[refEndpointField.tableId];
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
var foreignEndpointField = model.fields[foreignEndpoint.fieldId];
var refEndpointFieldName = _this.buildFieldName(refEndpoint.fieldIds, model, 'postgres');
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'postgres');
var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "\"".concat(foreignEndpointSchema.name, "\".") : '', "\"").concat(foreignEndpointTable.name, "\" ADD ");

@@ -179,3 +196,3 @@

line += "FOREIGN KEY (\"".concat(foreignEndpointField.name, "\") REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" (\"").concat(refEndpointField.name, "\")");
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "\"".concat(refEndpointSchema.name, "\".") : '', "\"").concat(refEndpointTable.name, "\" ").concat(refEndpointFieldName);

@@ -248,8 +265,18 @@ if (ref.onDelete) {

var line = 'COMMENT ON';
var table = model.tables[comment.tableId];
var schema = model.schemas[table.schemaId];
if (comment.type === 'column') {
var field = model.fields[comment.fieldId];
var table = model.tables[field.tableId];
var schema = model.schemas[table.schemaId];
line += " COLUMN ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\".\"").concat(field.name, "\" IS '").concat(field.note, "'");
switch (comment.type) {
case 'table':
{
line += " TABLE ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\" IS '").concat(table.note, "'");
break;
}
case 'column':
{
var field = model.fields[comment.fieldId];
line += " COLUMN ".concat((0, _utils.shouldPrintSchema)(schema, model) ? "\"".concat(schema.name, "\".") : '', "\"").concat(table.name, "\".\"").concat(field.name, "\" IS '").concat(field.note, "'");
break;
}
}

@@ -304,4 +331,6 @@

comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
var fieldIds = model.tables[tableId].fieldIds;
return fieldIds.filter(function (fieldId) {
var _model$tables$tableId = model.tables[tableId],
fieldIds = _model$tables$tableId.fieldIds,
note = _model$tables$tableId.note;
var fieldObjects = fieldIds.filter(function (fieldId) {
return model.fields[fieldId].note;

@@ -311,5 +340,10 @@ }).map(function (fieldId) {

type: 'column',
fieldId: fieldId
fieldId: fieldId,
tableId: tableId
};
});
return note ? [{
type: 'table',
tableId: tableId
}].concat(fieldObjects) : fieldObjects;
}))));

@@ -316,0 +350,0 @@ });

@@ -145,4 +145,14 @@ "use strict";

}, {
key: "buildFieldName",
value: function buildFieldName(fieldIds, model) {
var fieldNames = fieldIds.map(function (fieldId) {
return "[".concat(model.fields[fieldId].name, "]");
}).join(', ');
return "(".concat(fieldNames, ")");
}
}, {
key: "exportRefs",
value: function exportRefs(refIds, model) {
var _this = this;
var strArr = refIds.map(function (refId) {

@@ -157,8 +167,14 @@ var ref = model.refs[refId];

var refEndpoint = model.endpoints[refEndpointId];
var refEndpointField = model.fields[refEndpoint.fieldId];
var refEndpointField = model.fields[refEndpoint.fieldIds[0]];
var refEndpointTable = model.tables[refEndpointField.tableId];
var refEndpointSchema = model.schemas[refEndpointTable.schemaId];
var foreignEndpointField = model.fields[foreignEndpoint.fieldId];
var refEndpointFieldName = _this.buildFieldName(refEndpoint.fieldIds, model, 'mssql');
var foreignEndpointField = model.fields[foreignEndpoint.fieldIds[0]];
var foreignEndpointTable = model.tables[foreignEndpointField.tableId];
var foreignEndpointSchema = model.schemas[foreignEndpointTable.schemaId];
var foreignEndpointFieldName = _this.buildFieldName(foreignEndpoint.fieldIds, model, 'mssql');
var line = "ALTER TABLE ".concat((0, _utils.shouldPrintSchema)(foreignEndpointSchema, model) ? "[".concat(foreignEndpointSchema.name, "].") : '', "[").concat(foreignEndpointTable.name, "] ADD ");

@@ -170,3 +186,3 @@

line += "FOREIGN KEY ([".concat(foreignEndpointField.name, "]) REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ([").concat(refEndpointField.name, "])");
line += "FOREIGN KEY ".concat(foreignEndpointFieldName, " REFERENCES ").concat((0, _utils.shouldPrintSchema)(refEndpointSchema, model) ? "[".concat(refEndpointSchema.name, "].") : '', "[").concat(refEndpointTable.name, "] ").concat(refEndpointFieldName);

@@ -227,14 +243,27 @@ if (ref.onDelete) {

var commentArr = comments.map(function (comment) {
var table = model.tables[comment.tableId];
var schema = model.schemas[table.schemaId];
var line = '';
line = 'EXEC sp_addextendedproperty\n';
if (comment.type === 'column') {
var field = model.fields[comment.fieldId];
var table = model.tables[field.tableId];
var schema = model.schemas[table.schemaId];
line = 'EXEC sp_addextendedproperty\n';
line += '@name = N\'Column_Description\'\n';
line += "@value = '".concat(field.note, "'\n");
line += "@level0type = N'Schema', @level0name = '".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat(schema.name) : 'dbo', "',\n");
line += "@level1type = N'Table', @level1name = '".concat(table.name, "',\n");
line += "@level2type = N'Column', @level2name = '".concat(field.name, "';\n");
switch (comment.type) {
case 'table':
{
line += "@name = N'Table_Description',\n";
line += "@value = '".concat(table.note, "',\n");
line += "@level0type = N'Schema', @level0name = '".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat(schema.name) : 'dbo', "',\n");
line += "@level1type = N'Table', @level1name = '".concat(table.name, "';\n");
break;
}
case 'column':
{
var field = model.fields[comment.fieldId];
line += "@name = N'Column_Description',\n";
line += "@value = '".concat(field.note, "',\n");
line += "@level0type = N'Schema', @level0name = '".concat((0, _utils.shouldPrintSchema)(schema, model) ? "".concat(schema.name) : 'dbo', "',\n");
line += "@level1type = N'Table', @level1name = '".concat(table.name, "',\n");
line += "@level2type = N'Column', @level2name = '".concat(field.name, "';\n");
break;
}
}

@@ -276,4 +305,6 @@

hasBlockAbove = true;
}
} /////////PUSH COMMENTS OF TABLE & COLUMN/////////
// console.log(JSON.stringify(tableIds, null, 2));
indexIds.push.apply(indexIds, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {

@@ -283,4 +314,6 @@ return model.tables[tableId].indexIds;

comments.push.apply(comments, _toConsumableArray(_lodash["default"].flatten(tableIds.map(function (tableId) {
var fieldIds = model.tables[tableId].fieldIds;
return fieldIds.filter(function (fieldId) {
var _model$tables$tableId = model.tables[tableId],
fieldIds = _model$tables$tableId.fieldIds,
note = _model$tables$tableId.note;
var fieldObject = fieldIds.filter(function (fieldId) {
return model.fields[fieldId].note;

@@ -290,5 +323,10 @@ }).map(function (fieldId) {

type: 'column',
fieldId: fieldId
fieldId: fieldId,
tableId: tableId
};
});
return note ? [{
type: 'table',
tableId: tableId
}].concat(fieldObject) : fieldObject;
}))));

@@ -295,0 +333,0 @@ });

@@ -50,3 +50,3 @@ "use strict";

schemaName = _ref.schemaName,
fieldName = _ref.fieldName,
fieldNames = _ref.fieldNames,
relation = _ref.relation,

@@ -62,3 +62,4 @@ token = _ref.token,

_this.tableName = tableName;
_this.fieldName = fieldName;
_this.fieldNames = fieldNames;
_this.fields = [];
_this.ref = ref;

@@ -78,6 +79,4 @@ _this.dbState = _this.ref.dbState;

var field = table.findField(fieldName);
_this.setFields(fieldNames, table);
_this.setField(field, table);
return _this;

@@ -94,5 +93,22 @@ }

value: function equals(endpoint) {
return this.field.id === endpoint.field.id;
if (this.fields.length !== endpoint.fields.length) return false;
return this.compareFields(endpoint);
}
}, {
key: "compareFields",
value: function compareFields(endpoint) {
var sortedThisFieldIds = this.fields.map(function (field) {
return field.id;
}).sort();
var sortedEndpointFieldIds = endpoint.fields.map(function (field) {
return field.id;
}).sort();
for (var i = 0; i < sortedThisFieldIds.length; i += 1) {
if (sortedThisFieldIds[i] !== sortedEndpointFieldIds[i]) return false;
}
return true;
}
}, {
key: "export",

@@ -107,3 +123,5 @@ value: function _export() {

refId: this.ref.id,
fieldId: this.field.id
fieldIds: this.fields.map(function (field) {
return field.id;
})
};

@@ -117,3 +135,3 @@ }

tableName: this.tableName,
fieldName: this.fieldName,
fieldNames: this.fieldNames,
relation: this.relation

@@ -123,9 +141,20 @@ };

}, {
key: "setFields",
value: function setFields(fieldNames, table) {
var _this2 = this;
fieldNames.forEach(function (fieldName) {
var field = table.findField(fieldName);
_this2.setField(field, table);
});
}
}, {
key: "setField",
value: function setField(field, table) {
if (!field) {
this.error("Can't find field ".concat((0, _utils.shouldPrintSchema)(table.schema) ? "\"".concat(table.schema.name, "\".") : '', "\"").concat(this.fieldName, "\" in table \"").concat(this.tableName, "\""));
this.error("Can't find field ".concat((0, _utils.shouldPrintSchema)(table.schema) ? "\"".concat(table.schema.name, "\".") : '', "\"").concat(field.name, "\" in table \"").concat(this.tableName, "\""));
}
this.field = field;
this.fields.push(field);
field.pushEndpoint(this);

@@ -132,0 +161,0 @@ }

@@ -107,2 +107,7 @@ "use strict";

}
if (this.endpoints[0].fields.length !== this.endpoints[1].fields.length) {
this.error('Two endpoints have unequal number of fields');
} // TODO: Handle Error with different number of fields
}

@@ -109,0 +114,0 @@ }, {

@@ -18,2 +18,4 @@ "use strict";

var _mssqlParser = _interopRequireDefault(require("./mssqlParser"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

@@ -63,2 +65,7 @@

}, {
key: "parseMSSQLToJSON",
value: function parseMSSQLToJSON(str) {
return _mssqlParser["default"].parseWithPegError(str);
}
}, {
key: "parse",

@@ -85,2 +92,6 @@ value: function parse(str, format) {

case 'mssql':
rawDatabase = Parser.parseMSSQLToJSON(str);
break;
case 'json':

@@ -87,0 +98,0 @@ if (_typeof(str) === 'object') {

@@ -2629,3 +2629,3 @@ /*

function isSameEndpoints(endpoint1, endpoint2) {
return endpoint1.tableName == endpoint2.tableName && endpoint1.fieldName == endpoint2.fieldName;
return endpoint1.tableName == endpoint2.tableName && lodash.isEqual(lodash.sortBy(endpoint1.fieldNames), lodash.sortBy(endpoint2.fieldNames));
}

@@ -2692,6 +2692,6 @@

if (!endpoints[0].fieldName) {
if (!endpoints[0].fieldNames) {
var singleNameOfPrimaryTable = pluralize.singular(endpoints[1].tableName);
var columnName = "".concat(singleNameOfPrimaryTable, "_id");
endpoints[0].fieldName = columnName;
endpoints[0].fieldNames = [columnName];
var columnField = fromTable.fields.find(function (field) {

@@ -2710,8 +2710,8 @@ return field.name === columnName;

endpoints[0].fieldName = columnName;
endpoints[0].fieldNames = [columnName];
}
if (!endpoints[1].fieldName) {
if (!endpoints[1].fieldNames) {
var primaryKey = 'id';
endpoints[1].fieldName = primaryKey;
endpoints[1].fieldNames = [primaryKey];
}

@@ -2736,7 +2736,7 @@

if (currentProp.columnName) {
endpoints[0].fieldName = currentProp.columnName;
endpoints[0].fieldNames = [currentProp.columnName];
}
if (currentProp.primaryKey) {
endpoints[1].fieldName = currentProp.primaryKey;
endpoints[1].fieldNames = [currentProp.primaryKey];
}

@@ -2806,7 +2806,7 @@

tableName: table.name,
fieldName: columnName,
fieldNames: [columnName],
relation: '1'
}, {
tableName: referenceTable,
fieldName: primaryKeyName,
fieldNames: [primaryKeyName],
relation: '1'

@@ -2853,7 +2853,7 @@ }]

tableName: table.name,
fieldName: field.name,
fieldNames: [field.name],
relation: '1'
}, {
tableName: refWithTable.name,
fieldName: 'id',
fieldNames: ['id'],
relation: '1'

@@ -2860,0 +2860,0 @@ }]

{
"name": "@dbml/core",
"version": "2.0.1",
"version": "2.1.0",
"description": "> TODO: description",

@@ -33,2 +33,3 @@ "author": "Holistics <dev@holistics.io>",

"lodash": "^4.17.15",
"parsimmon": "^1.13.0",
"pluralize": "^8.0.0"

@@ -57,3 +58,3 @@ },

},
"gitHead": "86b474d228d9d0162fbe1b9c983c679378327bbf"
"gitHead": "5ad4565926a0bbc6a6354d3d85a7454a26f2a7b7"
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc