feat(0.2.1): M3 前端 PowerSync 基础 (schema/connector/db) + 依赖; fix storage sslmode
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
// PowerSync 本地库单例 (0.2.1 离线编辑)
|
||||
// 页面用法: usePowerSyncQuery 等见 src/lib/powersync/react.jsx; 写入用 db.execute/writeTransaction
|
||||
import { PowerSyncDatabase } from '@powersync/web'
|
||||
import { AppSchema } from './schema.js'
|
||||
import { IBoardConnector } from './connector.js'
|
||||
|
||||
let dbInstance = null
|
||||
|
||||
export function getPowersyncDb() {
|
||||
if (!dbInstance) {
|
||||
dbInstance = new PowerSyncDatabase({
|
||||
schema: AppSchema,
|
||||
database: {
|
||||
// 默认 IDBBatchAtomicVFS (IndexedDB), 兼容性最广; WebView2 (Chromium) 后续可切 OPFS 加速
|
||||
dbFilename: 'iboard-powersync.db',
|
||||
},
|
||||
})
|
||||
}
|
||||
return dbInstance
|
||||
}
|
||||
|
||||
/** 登录后调用: 建立同步连接 (幂等) */
|
||||
export async function connectPowersync() {
|
||||
const db = getPowersyncDb()
|
||||
db.connect(new IBoardConnector())
|
||||
return db
|
||||
}
|
||||
|
||||
/** 退出登录时调用: 断开同步并清空本地库 */
|
||||
export async function disconnectAndClearPowersync() {
|
||||
if (!dbInstance) return
|
||||
try {
|
||||
await dbInstance.disconnectAndClear()
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user