Files
competition-website/seed-carousel.js
WorkBuddy a303cf2d53 chore: 后端首次基线提交
运行必须源码:app/(controller/model/service/middleware/lib/utils/validate/router)、config/、database/(migrations/seeders/sql)、启动脚本、app/public/templates 模板。
已忽略运行非必须:node_modules/、logs/、run/、app/public 运行时上传数据、.env*、构建/缓存产物。
2026-08-17 12:25:42 +08:00

41 lines
1.8 KiB
JavaScript

const { Sequelize } = require('./node_modules/sequelize');
const sequelize = new Sequelize('competition_db', 'root', '74191221', {
host: 'localhost', port: 3306, dialect: 'mysql',
logging: false
});
(async () => {
try {
await sequelize.authenticate();
console.log('DB connected');
// MariaDB 5.5 compatible: only one TIMESTAMP can have DEFAULT CURRENT_TIMESTAMP
await sequelize.query(`
CREATE TABLE IF NOT EXISTS carousel (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
image_url VARCHAR(500) NOT NULL,
sort_order INT NOT NULL DEFAULT 0,
link_url VARCHAR(500) NULL,
enabled TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
PRIMARY KEY (id),
KEY idx_sort_order (sort_order),
KEY idx_enabled (enabled)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);
const [rows] = await sequelize.query('SELECT COUNT(*) as cnt FROM carousel');
console.log('Existing:', rows[0].cnt);
if (rows[0].cnt === 0) {
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1446776811953-b23d57bd21aa?w=1200', 1, 1, NOW(), NOW())");
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=1200', 2, 1, NOW(), NOW())");
await sequelize.query("INSERT INTO carousel (image_url, sort_order, enabled, created_at, updated_at) VALUES ('https://images.unsplash.com/photo-1614728263952-84ea256f9679?w=1200', 3, 1, NOW(), NOW())");
console.log('Inserted 3 carousel slides');
}
console.log('Done');
} catch(e) { console.error(e.message); }
finally { await sequelize.close(); }
})();