2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const db = require('../../../data/db');
|
2014-07-10 19:10:00 +04:00
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const doRaw = function doRaw(query, transaction, fn) {
|
2016-07-14 13:59:42 +03:00
|
|
|
if (!fn) {
|
|
|
|
fn = transaction;
|
|
|
|
transaction = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (transaction || db.knex).raw(query).then(function (response) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return fn(response);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getTables = function getTables(transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRaw('select * from sqlite_master where type = "table"', transaction, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.reject(_.map(response, 'tbl_name'), function (name) {
|
2014-07-10 19:10:00 +04:00
|
|
|
return name === 'sqlite_sequence';
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getIndexes = function getIndexes(table, transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRaw('pragma index_list("' + table + '")', transaction, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.flatten(_.map(response, 'name'));
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-20 01:56:46 +03:00
|
|
|
const getColumns = function getColumns(table, transaction) {
|
2016-07-14 13:59:42 +03:00
|
|
|
return doRaw('pragma table_info("' + table + '")', transaction, function (response) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.flatten(_.map(response, 'name'));
|
2014-07-10 19:10:00 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2017-11-01 16:44:54 +03:00
|
|
|
getTables: getTables,
|
2014-07-10 19:10:00 +04:00
|
|
|
getIndexes: getIndexes,
|
|
|
|
getColumns: getColumns
|
2014-09-10 08:06:24 +04:00
|
|
|
};
|