Skip to content

Commit

Permalink
fix: dexie data upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Sep 16, 2024
1 parent 74b80b4 commit 8350ac0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
16 changes: 8 additions & 8 deletions src/renderer/src/databases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ db.version(1).stores({
files: 'id, name, origin_name, path, size, ext, type, created_at, count'
})

db.version(2).stores({
files: 'id, name, origin_name, path, size, ext, type, created_at, count',
topics: '&id, messages',
settings: '&id, value'
})
db.version(2)
.stores({
files: 'id, name, origin_name, path, size, ext, type, created_at, count',
topics: '&id, messages',
settings: '&id, value'
})
.upgrade(populateTopics)

db.on('populate', async (trans) => {
populateTopics(trans)
})
db.on('populate', populateTopics)

export default db
36 changes: 17 additions & 19 deletions src/renderer/src/databases/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ import localforage from 'localforage'
export async function populateTopics(trans: Transaction) {
const indexedKeys = await localforage.keys()

if (indexedKeys.length === 0) {
return
}

for (const key of indexedKeys) {
const value: any = await localforage.getItem(key)
if (key.startsWith('topic:')) {
await trans.db.table('topics').add({ id: value.id, messages: value.messages })
}
if (key === 'image://avatar') {
await trans.db.table('settings').add({ id: key, value: await localforage.getItem(key) })
if (indexedKeys.length > 0) {
for (const key of indexedKeys) {
const value: any = await localforage.getItem(key)
if (key.startsWith('topic:')) {
await trans.db.table('topics').add({ id: value.id, messages: value.messages })
}
if (key === 'image://avatar') {
await trans.db.table('settings').add({ id: key, value: await localforage.getItem(key) })
}
}
}

window.modal.success({
title: i18n.t('message.upgrade.success.title'),
content: i18n.t('message.upgrade.success.content'),
okText: i18n.t('message.upgrade.success.button'),
centered: true,
onOk: () => window.api.reload()
})
window.modal.success({
title: i18n.t('message.upgrade.success.title'),
content: i18n.t('message.upgrade.success.content'),
okText: i18n.t('message.upgrade.success.button'),
centered: true,
onOk: () => window.api.reload()
})
}
}

0 comments on commit 8350ac0

Please sign in to comment.