Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Sequelize show tables

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!

like image 325
SlowHusky Avatar asked Apr 04 '17 20:04

SlowHusky


People also ask

How do I add a table to Sequelize?

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).

Does Sequelize automatically create table?

Automatically creates tables in MySQL database if they don't exist by calling await sequelize.

How do I check if a table exists Sequelized?

you can try to call describeTable of QueryInterface (to get QI call getQueryInterface in sequelize) to get information about a table.


2 Answers

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.

like image 175
user2926055 Avatar answered Oct 23 '22 12:10

user2926055


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')

like image 25
user2871305 Avatar answered Oct 23 '22 13:10

user2871305