138 lines
3.1 KiB
JavaScript
138 lines
3.1 KiB
JavaScript
/**
|
||
* 批量迁移脚本:将旧的 query() 调用替换为 Prisma
|
||
*
|
||
* 使用方法:
|
||
* node scripts/migrate-to-prisma.js
|
||
*/
|
||
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
// 需要迁移的API文件路径列表
|
||
const API_FILES = [
|
||
'app/api/referral/bind/route.ts',
|
||
'app/api/referral/visit/route.ts',
|
||
'app/api/miniprogram/pay/route.ts',
|
||
'app/api/miniprogram/pay/notify/route.ts',
|
||
'app/api/user/check-purchased/route.ts',
|
||
'app/api/user/purchase-status/route.ts',
|
||
'app/api/user/reading-progress/route.ts',
|
||
'app/api/user/track/route.ts',
|
||
'app/api/db/config/route.ts',
|
||
'app/api/book/all-chapters/route.ts',
|
||
'app/api/book/hot/route.ts',
|
||
'app/api/book/chapter/[id]/route.ts',
|
||
'app/api/match/users/route.ts',
|
||
'app/api/match/config/route.ts',
|
||
'app/api/search/route.ts'
|
||
]
|
||
|
||
/**
|
||
* 自动替换规则
|
||
*/
|
||
const REPLACE_RULES = [
|
||
{
|
||
// 替换 import
|
||
from: /from '@\/lib\/db'/g,
|
||
to: "from '@/lib/prisma'"
|
||
},
|
||
{
|
||
// 替换 query 导入为 prisma
|
||
from: /import \{ query(.*?) \} from '@\/lib\/prisma'/g,
|
||
to: "import { prisma } from '@/lib/prisma'"
|
||
},
|
||
{
|
||
// 替换 getConfig 导入
|
||
from: /import \{ getConfig \} from '@\/lib\/db'/g,
|
||
to: "import { getPrismaConfig } from '@/lib/prisma-helpers'"
|
||
},
|
||
{
|
||
// 替换 getConfig 调用
|
||
from: /getConfig\(/g,
|
||
to: "getPrismaConfig("
|
||
}
|
||
]
|
||
|
||
/**
|
||
* 添加 Prisma 注释
|
||
*/
|
||
function addPrismaComment(content) {
|
||
if (content.includes('使用 Prisma ORM')) return content
|
||
|
||
const lines = content.split('\n')
|
||
let commentIndex = -1
|
||
|
||
// 找到文件头注释
|
||
for (let i = 0; i < lines.length; i++) {
|
||
if (lines[i].includes('*/')) {
|
||
commentIndex = i
|
||
break
|
||
}
|
||
}
|
||
|
||
if (commentIndex > 0) {
|
||
lines.splice(commentIndex, 0, ' * 使用 Prisma ORM(安全,防SQL注入)')
|
||
}
|
||
|
||
return lines.join('\n')
|
||
}
|
||
|
||
/**
|
||
* 处理单个文件
|
||
*/
|
||
function migrateFile(filePath) {
|
||
const fullPath = path.join(__dirname, '..', filePath)
|
||
|
||
if (!fs.existsSync(fullPath)) {
|
||
console.log(`❌ 文件不存在: ${filePath}`)
|
||
return false
|
||
}
|
||
|
||
let content = fs.readFileSync(fullPath, 'utf-8')
|
||
|
||
// 如果已经迁移过,跳过
|
||
if (content.includes('from \'@/lib/prisma\'')) {
|
||
console.log(`⏭️ 已迁移,跳过: ${filePath}`)
|
||
return false
|
||
}
|
||
|
||
// 应用替换规则
|
||
REPLACE_RULES.forEach(rule => {
|
||
content = content.replace(rule.from, rule.to)
|
||
})
|
||
|
||
// 添加注释
|
||
content = addPrismaComment(content)
|
||
|
||
// 写回文件
|
||
fs.writeFileSync(fullPath, content, 'utf-8')
|
||
console.log(`✅ 已迁移: ${filePath}`)
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 主函数
|
||
*/
|
||
function main() {
|
||
console.log('🚀 开始批量迁移到 Prisma ORM...\n')
|
||
|
||
let successCount = 0
|
||
let skipCount = 0
|
||
|
||
API_FILES.forEach(filePath => {
|
||
const result = migrateFile(filePath)
|
||
if (result) {
|
||
successCount++
|
||
} else {
|
||
skipCount++
|
||
}
|
||
})
|
||
|
||
console.log(`\n✨ 迁移完成!`)
|
||
console.log(` - 成功迁移: ${successCount} 个文件`)
|
||
console.log(` - 跳过: ${skipCount} 个文件`)
|
||
console.log(`\n⚠️ 注意:批量迁移只处理简单替换,复杂的SQL查询需要手动迁移!`)
|
||
}
|
||
|
||
main()
|