connection.<methodName>()
ConstructorName.prototype.process = function() {
const connection = mysql.createConnection({
host: "-",
user: "-",
password: "-",
database: "-"
});
connection.connect();
new Promise(function (resolve, reject) {
connection.query("SHOW TABLES", (err, results, fields) => {
if (err) reject(err);
results.some(item => (Object.values(item).indexOf('<tablename>') !== - 1))
? resolve('таблица в порядке')
: reject(`Таблица с именем ${'name'} не обнаружена`);
});
})
.then(
result => {
return new Promise(function (resolve, reject) {
connection.query(`SHOW COLUMNS FROM <tablename>`, (err, results, fields) => {
if (err) reject(err);
const tableFields = results.map(item => item["Field"]);
["<fieldname>", "<fieldname>", "<fieldname>", "<fieldname>"].every(item => tableFields.indexOf(item) !== -1)
? resolve('Поля в порядке')
: reject(`поле с именем ${'<fieldname>'} не обнаружено`);
})
})
},
error => { console.log(error) }
)
// Дальше множество аналогичного кода в .then(тоска-печаль)...
.finally(
() => connection.end()
);
};
ConstructorName.prototype.process = async () => {
const connection = mysql.createConnection({
host: "-",
user: "-",
password: "-",
database: "-"
});
connection.connect();
const operation = new Promise((resolve, reject) => {
connection.query("SHOW TABLES", (err, results, fields) => {
if (err) reject(err);
results.some(item => (Object.values(item).indexOf('<tablename>') !== - 1))
? resolve('таблица в порядке')
: reject(`Таблица с именем ${'name'} не обнаружена`);
});
});
try {
const result = await operation;
const newOperation = new Promise(function (resolve, reject) {
connection.query(`SHOW COLUMNS FROM <tablename>`, (err, results, fields) => {
if (err) reject(err);
const tableFields = results.map(item => item["Field"]);
["<fieldname>", "<fieldname>", "<fieldname>", "<fieldname>"].every(item => tableFields.indexOf(item) !== -1)
? resolve('Поля в порядке')
: reject(`поле с именем ${'<fieldname>'} не обнаружено`);
})
});
await newOperation;
} catch (error) {
console.log(error);
} finally {
connection.end();
}
};