minishouyin/test/check-db.js

64 lines
2.0 KiB
JavaScript
Raw Normal View History

2025-11-12 11:35:57 +08:00
const { Sequelize } = require('sequelize');
const path = require('path');
// 数据库文件路径
const dbPath = path.join(__dirname, 'data/cashier.db');
console.log('Checking database at:', dbPath);
// 创建Sequelize实例
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: dbPath,
logging: false
});
async function checkDatabase() {
try {
// 验证连接
await sequelize.authenticate();
console.log('Connection has been established successfully.');
// 查询所有表
const [results] = await sequelize.query("SELECT name FROM sqlite_master WHERE type='table'");
console.log('Tables in database:', results);
// 检查各个表是否存在
const tableNames = results.map(table => table.name);
const requiredTables = ['products', 'categories', 'orders', 'order_items', 'cashiers', 'settings'];
console.log('\nChecking required tables:');
for (const table of requiredTables) {
if (tableNames.includes(table)) {
console.log(`${table} table exists`);
// 查询表中的记录数
try {
const [countResult] = await sequelize.query(`SELECT COUNT(*) as count FROM ${table}`);
console.log(` - Record count: ${countResult[0].count}`);
} catch (err) {
console.log(` - Error getting count: ${err.message}`);
}
} else {
console.log(`${table} table missing`);
}
}
// 如果有products表查询其中的示例数据
const productsTable = results.find(table => table.name === 'products');
if (productsTable) {
console.log('\nSample products:');
try {
const [products] = await sequelize.query("SELECT id, barcode, name, price, stock FROM products LIMIT 5");
console.log(products);
} catch (err) {
console.log('Error getting sample products:', err.message);
}
}
await sequelize.close();
} catch (error) {
console.error('Error checking database:', error);
}
}
checkDatabase();