Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Handle tables with keywords as names #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 51 additions & 4 deletions dist/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var GetTableList = function GetTableList(connection) {
var GetFieldsFromTable = function GetFieldsFromTable(connection, table) {
var fields = [];
return new Promise(function (resolve, reject) {
connection.query('desc ' + table, function (err, rows) {
connection.query('desc `' + table + '`', function (err, rows) {
if (err) {
reject(err);
}
Expand All @@ -83,10 +83,12 @@ var CreateConnection = function CreateConnection() {
password = args.password,
host = args.host,
database = args.database,
_args$port = args.port,
port = _args$port === undefined ? 3306 : _args$port,
_args$multipleStateme = args.multipleStatements,
multipleStatements = _args$multipleStateme === undefined ? true : _args$multipleStateme;

var connection = _mysql2.default.createConnection({ user: user, password: password, host: host, database: database, multipleStatements: multipleStatements });
var connection = _mysql2.default.createConnection({ user: user, password: password, host: host, database: database, port: port, multipleStatements: multipleStatements });
return connection;
};

Expand Down Expand Up @@ -138,6 +140,27 @@ var AddRelationsToSchema = function AddRelationsToSchema(connection, schema) {
});
};

var AddIndexesToSchema = function AddIndexesToSchema(connection, schema) {
return new Promise(function (resolve, reject) {

var promises = [];
var tableNames = Object.keys(schema.tables);
tableNames.forEach(function (tableName, index, array) {
promises.push(GetTableIndexes(connection, tableName).then(function (indexes) {
if (!schema.tables[tableName]) {
schema.tables[tableName] = { fields: [], relationsFromTable: [], relationsToTable: [] };
}
schema.tables[tableName].indexes = indexes;
}));
});
Promise.all(promises).then(function () {
return resolve(schema);
}).catch(function (err) {
return reject(err);
});
});
};

var AddRelationsByFieldNameToSchema = function AddRelationsByFieldNameToSchema(schema) {
var aliases = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var ignoreDefaultNames = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
Expand All @@ -158,6 +181,28 @@ var AddRelationsByFieldNameToSchema = function AddRelationsByFieldNameToSchema(s
return schema;
};

var GetTableIndexes = function GetTableIndexes(connection, table) {
var sqlIndexes = ' SELECT table_name AS `Table`,\n index_name AS `Index`,\n GROUP_CONCAT(column_name ORDER BY seq_in_index) AS `Columns`,\n SUM(NON_UNIQUE) = 0 AS `Unique`\n FROM information_schema.statistics\n WHERE table_schema = \'' + connection.config.database + '\' and (TABLE_NAME = \'' + table + '\')\n GROUP BY 1,2;';

var indexes = [];
return new Promise(function (resolve, reject) {
connection.query(sqlIndexes, function (err, indexesResp) {
if (err) {
reject(err);
}
indexesResp.forEach(function (value, index, array) {
var Table = value.Table,
Index = value.Index,
Columns = value.Columns,
Unique = value.Unique; // Extract info

indexes.push({ Index: Index, Columns: Columns, Unique: Unique });
});
resolve(indexes);
});
});
};

var GetRelationsFromTable = function GetRelationsFromTable(connection, table) {
var sqlRelaciones = ' SELECT TABLE_SCHEMA as db, \n TABLE_NAME as t1,\n COLUMN_NAME as t1Field,\n REFERENCED_TABLE_SCHEMA as db2,\n REFERENCED_TABLE_NAME as t2,\n REFERENCED_COLUMN_NAME as t2Field \n FROM \n INFORMATION_SCHEMA.KEY_COLUMN_USAGE \n WHERE \n TABLE_SCHEMA = SCHEMA()\n AND REFERENCED_TABLE_NAME IS NOT NULL \n and (TABLE_NAME = \'' + table + '\');'; // and (REFERENCED_TABLE_NAME = '${table}');`

Expand All @@ -175,7 +220,7 @@ var GetRelationsFromTable = function GetRelationsFromTable(connection, table) {
t2 = value.t2,
t2Field = value.t2Field; // Extract info

relations.push({ localField: t1Field, foreignTable: t2, foreignField: t2Field });
relations.push({ localField: t1Field, foreignSchema: db2, foreignTable: t2, foreignField: t2Field });
});
resolve(relations);
});
Expand All @@ -199,7 +244,7 @@ var GetRelationsToTable = function GetRelationsToTable(connection, table) {
t2 = value.t2,
t2Field = value.t2Field; // Extract info

relations.push({ localField: t2Field, foreignTable: t1, foreignField: t1Field });
relations.push({ localField: t2Field, foreignSchema: db, foreignTable: t1, foreignField: t1Field });
});
resolve(relations);
});
Expand All @@ -217,6 +262,8 @@ var CreateFileWithContent = function CreateFileWithContent(fileName, content, ou
var GetSchemaWithRelations = function GetSchemaWithRelations(connection) {
return GetSchema(connection).then(function (res) {
return AddRelationsToSchema(connection, res);
}).then(function (res) {
return AddIndexesToSchema(connection, res);
}).catch(function (err) {
console.error(err);
throw err;
Expand Down
60 changes: 55 additions & 5 deletions src/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const GetFieldsFromTable = (connection, table) => {
const fields = [];
return (
new Promise(function (resolve, reject) {
connection.query(`desc ${table}`, function (err, rows) {
connection.query(`desc \`${table}\``, function (err, rows) {
if (err) {
reject(err);
}
Expand All @@ -67,8 +67,8 @@ const GetFieldsFromTable = (connection, table) => {
}

const CreateConnection = (args = {}) => {
const { user, password, host, database, multipleStatements = true } = args;
const connection = mysql.createConnection({ user, password, host, database, multipleStatements });
const { user, password, host, database, port = 3306, multipleStatements = true } = args;
const connection = mysql.createConnection({ user, password, host, database, port, multipleStatements });
return connection;
}

Expand Down Expand Up @@ -121,6 +121,29 @@ const AddRelationsToSchema = (connection, schema) => {
);
};

const AddIndexesToSchema = (connection, schema) => {
return (
new Promise(function (resolve, reject) {

var promises = [];
const tableNames = Object.keys(schema.tables);
tableNames.forEach((tableName, index, array) => {
promises.push(
GetTableIndexes(connection, tableName)
.then((indexes) => {
if (!schema.tables[tableName]) {
schema.tables[tableName] = { fields: [], relationsFromTable: [], relationsToTable: [] };
}
schema.tables[tableName].indexes = indexes;
})
);
});
Promise.all(promises).then(() => resolve(schema))
.catch((err) => reject(err));
})
);
};

const AddRelationsByFieldNameToSchema = (schema, aliases = [], ignoreDefaultNames = false, prefix = 'id_', sufix = '_id') => {
const tableNames = Object.keys(schema.tables);
tableNames.forEach((tableName, index, array) => {
Expand All @@ -132,6 +155,32 @@ const AddRelationsByFieldNameToSchema = (schema, aliases = [], ignoreDefaultName
return schema;
};

const GetTableIndexes = (connection, table) => {
const sqlIndexes = ` SELECT table_name AS \`Table\`,
index_name AS \`Index\`,
GROUP_CONCAT(column_name ORDER BY seq_in_index) AS \`Columns\`,
SUM(NON_UNIQUE) = 0 AS \`Unique\`
FROM information_schema.statistics
WHERE table_schema = '${connection.config.database}' and (TABLE_NAME = '${table}')
GROUP BY 1,2;`;

const indexes = [];
return (
new Promise(function (resolve, reject) {
connection.query(sqlIndexes, function (err, indexesResp) {
if (err) {
reject(err);
}
indexesResp.forEach((value, index, array) => {
const {Table, Index, Columns, Unique} = value; // Extract info
indexes.push({Index: Index, Columns: Columns, Unique: Unique});
});
resolve(indexes);
});
})
);
};

const GetRelationsFromTable = (connection, table) => {
const sqlRelaciones = ` SELECT TABLE_SCHEMA as db,
TABLE_NAME as t1,
Expand All @@ -155,7 +204,7 @@ const GetRelationsFromTable = (connection, table) => {
}
relationsResp.forEach((value, index, array) => {
const { db, t1, t1Field, db2, t2, t2Field } = value; // Extract info
relations.push({ localField: t1Field, foreignTable: t2, foreignField: t2Field });
relations.push({ localField: t1Field, foreignSchema: db2, foreignTable: t2, foreignField: t2Field });
});
resolve(relations);
});
Expand Down Expand Up @@ -186,7 +235,7 @@ const GetRelationsToTable = (connection, table) => {
}
relationsResp.forEach((value, index, array) => {
const { db, t1, t1Field, db2, t2, t2Field } = value; // Extract info
relations.push({ localField: t2Field, foreignTable: t1, foreignField: t1Field });
relations.push({ localField: t2Field, foreignSchema:db, foreignTable: t1, foreignField: t1Field });
});
resolve(relations);
});
Expand All @@ -205,6 +254,7 @@ const CreateFileWithContent = (fileName, content, outputFolder) => {
const GetSchemaWithRelations = (connection) => {
return GetSchema(connection)
.then(res => AddRelationsToSchema(connection, res))
.then(res => AddIndexesToSchema(connection, res))
.catch((err) => {
console.error(err);
throw err;
Expand Down