Skip to content

Commit

Permalink
fix(core): transform old data before import
Browse files Browse the repository at this point in the history
  • Loading branch information
Chilfish committed Mar 29, 2024
1 parent 91ed57e commit 2cde106
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
6 changes: 5 additions & 1 deletion apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ onNuxtReady(async () => {
publicStore.users = JSON.parse(users || '[]')
publicStore.curUid = curUid || ''
await publicStore.migrateUser()
watchImmediate(() => publicStore.curUid, async () => {
loaded.value = false
await publicStore.migrateUser()
loaded.value = true
})
loaded.value = true
})
Expand Down
22 changes: 15 additions & 7 deletions packages/core/src/stores/public.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import type { UID, UserInfo } from '@types'
import { IDB } from '../utils/storage'
import { parseOldPost } from '../utils'

export const usePublicStore = defineStore('public', () => {
const globalImg = ref('')
Expand Down Expand Up @@ -39,15 +40,22 @@ export const usePublicStore = defineStore('public', () => {
* 从旧版中迁移 user 数据到 idb 中
*/
async function migrateUser() {
// if (DB_VERSION > 2)
// return
if (!curUser.value)
return

const dbName = `uid-${curUid.value}` as UID
const idb = new IDB(dbName)

users.value.forEach(async (user) => {
const dbName = `uid-${user.uid}` as UID
const idb = new IDB(dbName)
await idb.setUserInfo(toRaw(user))
const user = await idb.getUserInfo()
if (user)
return await idb.close()
})

await idb.setUserInfo(toRaw(curUser.value))
const posts = await idb.getAllDBPosts()
const newPosts = posts.map(post => parseOldPost(post))
await idb.addDBPosts(newPosts, true, false)

return await idb.close()
}

return {
Expand Down

0 comments on commit 2cde106

Please sign in to comment.