Sequelize 检查 table 是否为空

Sequelize check table is empty

我正在使用 Node js 并在 table 中进行插入或更新。我如何使用 sequelize 不彻底 sql query.is 检查 table 是否为空有一种方法我也可以获得 table

中的总行数

要计算 table 中的所有行数,您可以使用以下方法 ex:

Users.findAndCountAll()
.then(result => {
    console.log(result.count);
    console.log(result.rows);
})
.catch(err => {
    throw err;
});

我建议您搜索 sequelize 文档,这里是 link:Sequelize Documentation

您可以使用计数:

const { count, rows } = await Users.findAndCountAll();
if (count === 0 && rows === 0) {
  // do you thing
}