cherry-studio/src/main/data/db/seeding/preferenceSeeding.ts
2025-08-09 14:30:24 +08:00

48 lines
1.3 KiB
TypeScript

import { preferenceTable } from '@data/db/schemas/preference'
import { defaultPreferences } from '@shared/data/preferences'
import type { DbType, ISeed } from '../types'
class PreferenceSeed implements ISeed {
async migrate(db: DbType): Promise<void> {
const preferences = await db.select().from(preferenceTable)
// Convert existing preferences to a Map for quick lookup
const existingPrefs = new Map(preferences.map((p) => [`${p.scope}.${p.key}`, p]))
// Collect all new preferences to insert
const newPreferences: Array<{
scope: string
key: string
value: unknown
}> = []
// Process each scope in defaultPreferences
for (const [scope, scopeData] of Object.entries(defaultPreferences)) {
// Process each key-value pair in the scope
for (const [key, value] of Object.entries(scopeData)) {
const prefKey = `${scope}.${key}`
// Skip if this preference already exists
if (existingPrefs.has(prefKey)) {
continue
}
// Add to new preferences array
newPreferences.push({
scope,
key,
value
})
}
}
// If there are new preferences to insert, do it in a transaction
if (newPreferences.length > 0) {
await db.insert(preferenceTable).values(newPreferences)
}
}
}
export default PreferenceSeed