104 lines
2.4 KiB
JavaScript
104 lines
2.4 KiB
JavaScript
const fs = require('fs')
|
|
const path = require('path')
|
|
|
|
const {
|
|
ensureData,
|
|
findUserByEmail,
|
|
createUser,
|
|
findGameById,
|
|
createGame,
|
|
updateGameThumbnail,
|
|
createGameItem,
|
|
createCustomItem,
|
|
findTierListById,
|
|
saveTierList,
|
|
createGameSuggestion,
|
|
} = require('../src/db')
|
|
|
|
async function run() {
|
|
const sourcePath = path.join(__dirname, '..', 'data', 'db.json')
|
|
const raw = fs.readFileSync(sourcePath, 'utf8')
|
|
const data = JSON.parse(raw)
|
|
|
|
await ensureData()
|
|
|
|
for (const user of data.users || []) {
|
|
const existing = await findUserByEmail(user.email)
|
|
if (!existing) {
|
|
await createUser({
|
|
id: user.id,
|
|
email: user.email,
|
|
nickname: user.nickname || '',
|
|
passwordHash: user.passwordHash,
|
|
isAdmin: !!user.isAdmin,
|
|
})
|
|
}
|
|
}
|
|
|
|
for (const game of data.games || []) {
|
|
const existing = await findGameById(game.id)
|
|
if (!existing) {
|
|
await createGame({ id: game.id, name: game.name })
|
|
}
|
|
if (game.thumbnailSrc) {
|
|
await updateGameThumbnail(game.id, game.thumbnailSrc)
|
|
}
|
|
}
|
|
|
|
for (const item of data.gameImages || []) {
|
|
try {
|
|
await createGameItem({
|
|
id: item.id,
|
|
gameId: item.gameId,
|
|
src: item.src,
|
|
label: item.label,
|
|
})
|
|
} catch (e) {}
|
|
}
|
|
|
|
for (const suggestion of data.gameSuggestions || []) {
|
|
try {
|
|
await createGameSuggestion({ id: suggestion.id, name: suggestion.name })
|
|
} catch (e) {}
|
|
}
|
|
|
|
const seenCustomIds = new Set()
|
|
for (const tierList of data.tierLists || []) {
|
|
for (const item of tierList.pool || []) {
|
|
if (item.origin !== 'custom' || seenCustomIds.has(item.id)) continue
|
|
seenCustomIds.add(item.id)
|
|
try {
|
|
await createCustomItem({
|
|
id: item.id,
|
|
ownerId: tierList.authorId,
|
|
src: item.src,
|
|
label: item.label,
|
|
})
|
|
} catch (e) {}
|
|
}
|
|
}
|
|
|
|
for (const tierList of data.tierLists || []) {
|
|
const existing = await findTierListById(tierList.id)
|
|
if (!existing) {
|
|
await saveTierList({
|
|
id: tierList.id,
|
|
authorId: tierList.authorId,
|
|
gameId: tierList.gameId,
|
|
title: tierList.title,
|
|
description: tierList.description || '',
|
|
isPublic: !!tierList.isPublic,
|
|
groups: tierList.groups || [],
|
|
pool: tierList.pool || [],
|
|
})
|
|
}
|
|
}
|
|
|
|
console.log('migrate-lowdb-to-mariadb: done')
|
|
}
|
|
|
|
run().catch((error) => {
|
|
console.error(error)
|
|
process.exit(1)
|
|
})
|