制作Sequelize展示表

Slo*_*sky 3 node.js sequelize.js

我正在node.js中创建一个项目,我的一个页面将显示数据库中所有表的列表.我想知道Sequelize是否有像"Show tables"这样的功能.

谢谢!

use*_*055 6

我不认为Sequelize中有一个API,但你总是可以回到原始SQL."显示表格"是一种MySQL主义,所以你可以这样做:

var seq = new Sequelize('mysql://localhost/mysql');
seq.query('show tables').then(function(rows) {
    console.log(JSON.stringify(rows));
});
Run Code Online (Sandbox Code Playgroud)

console.log用您自己的解析和显示逻辑替换.


use*_*305 6

使用 SequelizeshowAllSchemas方法:

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);
})
Run Code Online (Sandbox Code Playgroud)

这将是“正确”的 Sequelize 方法,而不是 .query('show tables')

  • 如果您使用 postgres,那么这将返回模式列表而不是表列表。 (3认同)