generated from idea2app/Strapi-PNPM-Docker-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimport-data.ts
More file actions
178 lines (139 loc) · 5.02 KB
/
import-data.ts
File metadata and controls
178 lines (139 loc) · 5.02 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env tsx
/**
* Strapi database import script using MobX-RESTful-migrator
* Support import NGO organization data from Excel file to Strapi database
*/
import * as fs from 'node:fs';
import { RestMigrator } from 'mobx-restful-migrator';
import { ExcelReader } from './utils/excel-reader';
import { Config, SourceOrganization } from './types';
import { TargetOrganizationModel } from './utils/strapi-api';
import { migrationMapping } from './migration/organization-mapping';
import { MigratorLogger } from './utils/migrator-logger';
// Configuration
const CONFIG: Config = {
STRAPI_URL: process.env.STRAPI_URL || 'http://localhost:1337',
STRAPI_TOKEN: process.env.STRAPI_TOKEN || '',
EXCEL_FILE: process.env.EXCEL_FILE || '教育公益开放式数据库.xlsx',
SHEET_NAME: process.env.SHEET_NAME || null,
BATCH_SIZE: parseInt(process.env.BATCH_SIZE || '10'),
BATCH_DELAY: parseInt(process.env.BATCH_DELAY || '0'),
DRY_RUN: process.env.DRY_RUN === 'true',
MAX_ROWS: parseInt(process.env.MAX_ROWS || '0'),
};
// Data source generator function
async function* loadOrganizationData(): AsyncGenerator<SourceOrganization> {
console.log(`正在读取 Excel 文件: ${CONFIG.EXCEL_FILE}`);
if (!fs.existsSync(CONFIG.EXCEL_FILE)) {
throw new Error(`Excel 文件不存在: ${CONFIG.EXCEL_FILE}`);
}
// Use existing Excel reader
const rawOrganizations = ExcelReader.readExcelFile(
CONFIG.EXCEL_FILE,
CONFIG.SHEET_NAME,
);
if (CONFIG.MAX_ROWS > 0) {
rawOrganizations.splice(CONFIG.MAX_ROWS);
}
console.log(`从 Excel 读取到 ${rawOrganizations.length} 条记录`);
// Yield each organization from existing reader
yield* rawOrganizations;
}
// Main function
async function main(): Promise<void> {
let logger: MigratorLogger | null = null;
// Handle process signals to ensure logs are saved on forced exit
const handleExit = (signal: string) => {
console.log(`\n收到 ${signal} 信号,正在保存日志...`);
if (logger) {
logger.saveToFiles();
console.log('日志已保存,程序退出。');
}
process.exit(0);
};
process.on('SIGINT', () => handleExit('SIGINT'));
process.on('SIGTERM', () => handleExit('SIGTERM'));
process.on('SIGQUIT', () => handleExit('SIGQUIT'));
try {
console.log('=== Strapi 数据导入工具 ===\n');
// Validate configuration
if (!CONFIG.STRAPI_TOKEN && !CONFIG.DRY_RUN) {
throw new Error('请设置 STRAPI_TOKEN 环境变量或使用 DRY_RUN=true');
}
if (CONFIG.DRY_RUN) {
console.log('🔥 DRY RUN 模式 - 不会实际创建数据\n');
}
// Initialize logger
logger = new MigratorLogger();
// Create migrator instance
const migrator = new RestMigrator(
loadOrganizationData,
TargetOrganizationModel,
migrationMapping,
logger,
);
console.log('开始数据迁移...\n');
let count = 0;
for await (const organization of migrator.boot()) {
count++;
}
// Print final statistics
logger.printStats();
console.log('\n导入完成!');
// Save logs to files
await logger.saveToFiles();
} catch (error: any) {
console.error('导入失败:', error.message);
if (error.stack) {
console.error('错误堆栈:', error.stack);
}
if (logger) {
await logger.saveToFiles();
}
process.exit(1);
}
}
// Handle command line arguments
function parseArgs(): void {
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
Strapi 数据导入工具
支持从 Excel 文件导入 NGO 组织数据到 Strapi 数据库。
用法:
tsx scripts/import-data.ts [选项]
选项:
--dry-run, -d 仅模拟导入,不实际创建数据
--help, -h 显示帮助信息
环境变量:
STRAPI_URL Strapi 服务器地址 (默认: http://localhost:1337)
STRAPI_TOKEN Strapi API Token
EXCEL_FILE Excel 文件路径 (默认: 教育公益开放式数据库.xlsx)
SHEET_NAME 工作表名称 (默认: 使用第一个工作表)
BATCH_SIZE 批次大小 (默认: 10) - 由迁移框架自动处理
BATCH_DELAY 批次间延迟秒数 (默认: 0) - 由迁移框架自动处理
MAX_ROWS 最大处理行数 (默认: 0,表示全部)
DRY_RUN 模拟运行 (true/false, 默认: false)
VERBOSE_LOGGING 详细日志 (true/false, 默认: false)
示例:
# 基本使用
STRAPI_TOKEN=your_token tsx scripts/import-data.ts
# 指定工作表
SHEET_NAME="甘肃省" STRAPI_TOKEN=your_token tsx scripts/import-data.ts
# 仅测试前10行
MAX_ROWS=10 DRY_RUN=true tsx scripts/import-data.ts
# 设置详细日志
VERBOSE_LOGGING=true STRAPI_TOKEN=your_token tsx scripts/import-data.ts
`);
process.exit(0);
}
if (args.includes('--dry-run') || args.includes('-d')) {
CONFIG.DRY_RUN = true;
}
}
// Entry point
if (require.main === module) {
parseArgs();
main();
}
export { DataTransformer, ExcelReader, TargetOrganizationModel as DataImporter, TargetOrganizationModel as StrapiAPI };