Files
iboard/scripts/seed.cjs
T
jif2.zhang afbb9bc603
Docker Build and Push / build-image (push) Successful in 2m37s
feat(宏观): ifinance 分类动态挂载为宏观 tab, 报警迁入监控, 下线油价/本地经济数据/策略研究
- 宏观只保留本地 新闻/经济文章, 外接 ifinance categories[] 动态成为 tab (纯前端直连, CORS/_headers, https)
- 报警从宏观迁到监控第 4 个 tab (保留 Alert 模块, 供未来新增报警源)
- 彻底删除油价/经济数据/策略研究: 前端组件+api、后端 apizero/data 路由、cron、fetchers、种子数据
- 新增迁移 DROP Indicator/IndicatorPoint/Strategy/OilPriceHistory; 重新生成 Prisma Client
- CSP 放开 *.honor3.com (含端口通配), 避免新增子域反复重装外壳
2026-10-30 17:17:10 +08:00

87 lines
2.8 KiB
JavaScript

/**
* iBoard 基础数据初始化脚本
* 用法: npm run db:seed
* 幂等: 主键/唯一键冲突跳过
*/
require('dotenv').config();
const crypto = require('crypto');
const mysql = require('mysql2/promise');
function hashPassword(plain) {
const salt = crypto.randomBytes(16).toString('hex');
const hash = crypto.scryptSync(plain, salt, 64).toString('hex');
return 'scrypt$' + salt + '$' + hash;
}
function parseUrl(url) {
const u = new URL(url);
return {
host: u.hostname,
port: Number(u.port) || 3306,
user: decodeURIComponent(u.username),
password: decodeURIComponent(u.password),
database: u.pathname.replace(/^\//, ''),
};
}
async function insertIgnore(conn, sql, params) {
try {
const [r] = await conn.execute(sql, params);
return r.affectedRows;
} catch (e) {
if (e.code === 'ER_DUP_ENTRY') return 0;
throw e;
}
}
async function main() {
const cfg = parseUrl(process.env.DATABASE_URL);
if (!cfg.host) throw new Error('DATABASE_URL 未配置或解析失败');
const conn = await mysql.createConnection(cfg);
console.log('已连接:', cfg.host + ':' + cfg.port + '/' + cfg.database);
const adminPwd = hashPassword('admin123');
const n1 = await insertIgnore(
conn,
'INSERT INTO `User` (name, email, password, createdAt, updatedAt) VALUES (?, ?, ?, NOW(3), NOW(3))',
['Admin', 'admin@iboard.local', adminPwd],
);
console.log('[User] 新增管理员: ' + n1 + ' 行 (admin@iboard.local / admin123)');
const article = {
title: '欢迎使用 iBoard',
slug: 'welcome-to-iboard',
content: 'iBoard 用于收集和整理经济数据,以策略为主体重新组织。\n\n本文为示例条目,正式文章仍由 content/articles/*.md 文件承载。',
date: '2026-08-03',
};
const n4 = await insertIgnore(
conn,
'INSERT INTO `Article` (title, slug, content, date, createdAt, updatedAt) VALUES (?, ?, ?, ?, NOW(3), NOW(3))',
[article.title, article.slug, article.content, article.date],
);
console.log('[Article] 新增示例文章: ' + n4 + ' 行');
const sowRows = [
{ month: '2024-01', inventory: 4067 },
{ month: '2024-02', inventory: 4042 },
{ month: '2024-03', inventory: 3992 },
{ month: '2024-04', inventory: 3986 },
{ month: '2024-05', inventory: 3996 },
{ month: '2024-06', inventory: 4038 },
];
let n5 = 0;
for (const r of sowRows) {
n5 += await insertIgnore(
conn,
'INSERT INTO `Econ_SowInventory` (month, inventory, createdAt, updatedAt) VALUES (?, ?, NOW(3), NOW(3))',
[r.month, r.inventory],
);
}
console.log('[Econ_SowInventory] 新增: ' + n5 + ' 行');
await conn.end();
console.log('\nSeed 完成.');
}
main().catch((e) => { console.error('Seed 失败:', e); process.exit(1); });