I'm creating a project in node.js and one of my pages will show a list of all the tables in the database. I would like to know if Sequelize has a function like "Show tables".
Thanks!
Sequelize create table with sync() method. The Sequelize model you create using the sequelize. define() method comes with a sync() method that you can use to create a table. The table created using the sync() method will follow the model definition for its column(s).
Automatically creates tables in MySQL database if they don't exist by calling await sequelize.
you can try to call describeTable of QueryInterface (to get QI call getQueryInterface in sequelize) to get information about a table.
I don't think there's an API in Sequelize, but you can always fall back to raw SQL. "Show tables" is a MySQLism, so you could just do:
var seq = new Sequelize('mysql://localhost/mysql');
seq.query('show tables').then(function(rows) {
console.log(JSON.stringify(rows));
});
Replace the console.log
with your own parsing and display logic.
Use the Sequelize showAllSchemas
method:
var sequelize = new Sequelize('mysql://localhost/mysql');
sequelize.getQueryInterface().showAllSchemas().then((tableObj) => {
console.log('// Tables in database','==========================');
console.log(tableObj);
})
.catch((err) => {
console.log('showAllSchemas ERROR',err);
})
This would be the "proper" Sequelize way to do it as opposed to .query('show tables')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With