fix:数据库错误

This commit is contained in:
2025-11-27 10:44:27 +08:00
parent b23514cfe6
commit 074a7f1ff0
9 changed files with 346 additions and 93 deletions

View File

@@ -19,15 +19,50 @@ const TRANSACTION_TABLE_SQL = `
let sqliteConnection
let db
let initialized = false
let jeepLoaderPromise
let jeepElementReadyPromise
const waitFor = (ms = 100) =>
new Promise((resolve) => {
setTimeout(resolve, ms)
})
const ensureStoreReady = async (jeepEl, retries = 5) => {
for (let attempt = 0; attempt < retries; attempt += 1) {
const isOpen = (await jeepEl.isStoreOpen?.()) === true
if (isOpen) return
if (typeof jeepEl.openStore === 'function') {
const opened = await jeepEl.openStore('jeepSqliteStore', 'databases')
if (opened) return
}
await waitFor(200 * (attempt + 1))
}
throw new Error('jeep-sqlite IndexedDB store 未初始化成功')
}
const ensureJeepElement = async () => {
if (!customElements.get('jeep-sqlite')) {
defineJeep(window)
if (!jeepLoaderPromise) {
jeepLoaderPromise = defineJeep(window)
}
if (!document.querySelector('jeep-sqlite')) {
const jeepEl = document.createElement('jeep-sqlite')
document.body.appendChild(jeepEl)
await jeepLoaderPromise
if (!jeepElementReadyPromise) {
jeepElementReadyPromise = (async () => {
let jeepEl = document.querySelector('jeep-sqlite')
if (!jeepEl) {
jeepEl = document.createElement('jeep-sqlite')
jeepEl.setAttribute('auto-save', 'true')
document.body.appendChild(jeepEl)
}
jeepEl.setAttribute('wasm-path', '/assets')
await customElements.whenDefined('jeep-sqlite')
await jeepEl.componentOnReady?.()
return jeepEl
})()
}
const element = await jeepElementReadyPromise
await ensureStoreReady(element)
await CapacitorSQLite.initWebStore()
}
@@ -71,6 +106,16 @@ export const getDb = async () => {
return db
}
export const saveDbToStore = async () => {
await initSQLite()
if (!sqliteConnection?.saveToStore) return
try {
await sqliteConnection.saveToStore(DB_NAME)
} catch (error) {
console.warn('[sqlite] saveToStore 执行失败,数据将保留在内存中', error)
}
}
export const closeSQLite = async () => {
if (db) {
await db.close()