Skip to content

Instantly share code, notes, and snippets.

@ashutoshpw
Created September 9, 2019 07:59
Show Gist options
  • Save ashutoshpw/2f8bf2273dcbb440303635efbd82ee31 to your computer and use it in GitHub Desktop.
Save ashutoshpw/2f8bf2273dcbb440303635efbd82ee31 to your computer and use it in GitHub Desktop.
Export Database table columns to a file
const models = require('./models');
models.sequelize.query("SELECT table_schema || '.' || table_name FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')").then(function(rows) {
let d = JSON.parse(JSON.stringify(rows))
let my_tables = [];
for(let i=0;i<d.length;i++){
var n = (d[0][i]['?column?']).split(".");
if(n[0] == "public"){
let column_query = `select column_name, DATA_TYPE,NUMERIC_PRECISION, NUMERIC_SCALE
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='${n[1]}'`;
models.sequelize.query(column_query).then(r=>{
columns = JSON.parse(JSON.stringify(r));
//req.log(r[0]);
//req.log(n[1]);
my_tables.push({
table_name:n[1],
//columns: JSON.stringify(r[0])
columns: r[0]
});
});
}
}
setTimeout(()=>{
require('fs').writeFile('db.json',JSON.stringify(my_tables), (err)=>{
if(err) throw err;
req.log(my_tables);
})
}, 20000);
//req.log(my_tables);
}).then(d=>{
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment