File size: 1,195 Bytes
4d48c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Database from 'better-sqlite3';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const db = new Database(path.join(__dirname, '../server/data/lifekline.db'));

// 导入文章
function importArticles() {
  const articlesPath = path.join(__dirname, 'generated-articles.json');
  if (!fs.existsSync(articlesPath)) {
    console.log('未找到 generated-articles.json,请先运行 generate-content.js');
    return;
  }

  const articles = JSON.parse(fs.readFileSync(articlesPath, 'utf-8'));

  const stmt = db.prepare(`
    INSERT OR REPLACE INTO knowledge_articles
    (id, slug, title, category, level, tags, summary, content, created_at)
    VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
  `);

  for (const a of articles) {
    stmt.run(
      a.id,
      a.slug,
      a.title,
      a.category,
      a.level || 1,
      JSON.stringify(a.tags || []),
      a.summary,
      a.content,
      a.createdAt || new Date().toISOString()
    );
    console.log(`导入: ${a.title}`);
  }

  console.log(`\n共导入 ${articles.length} 篇文章`);
}

importArticles();