fix(db): fall back to JSON store when HyperDB has no users
HyperDB Rocks was opening empty on ~/.config/pearcord while registered users lived only in pearcord-db.json, so session restore showed onboarding despite a valid keypair and marker. If HyperDB has zero users but JSON has rows, close Rocks and use JsonStore for that storage path. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -22,6 +22,35 @@ function tryLoadHyperDB () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const hasHyperDB = tryLoadHyperDB()
|
const hasHyperDB = tryLoadHyperDB()
|
||||||
|
const USERS_COLLECTION = '@pearcord/users'
|
||||||
|
|
||||||
|
function jsonStoreHasUsers (storagePath) {
|
||||||
|
const file = path.join(storagePath, 'pearcord-db.json')
|
||||||
|
if (!fs.existsSync(file)) return false
|
||||||
|
try {
|
||||||
|
const raw = JSON.parse(fs.readFileSync(file, 'utf8'))
|
||||||
|
const users = raw[USERS_COLLECTION]
|
||||||
|
return Array.isArray(users) && users.length > 0
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function hyperdbHasUsers (backend) {
|
||||||
|
try {
|
||||||
|
let rows = await backend.find(USERS_COLLECTION, {}, { limit: 1 })
|
||||||
|
if (rows && typeof rows.toArray === 'function') rows = await rows.toArray()
|
||||||
|
else if (rows && typeof rows[Symbol.asyncIterator] === 'function') {
|
||||||
|
const list = []
|
||||||
|
for await (const row of rows) list.push(row)
|
||||||
|
rows = list
|
||||||
|
}
|
||||||
|
return Array.isArray(rows) && rows.length > 0
|
||||||
|
} catch (err) {
|
||||||
|
if (/Unknown index|Unknown collection/.test(err.message)) return false
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class LocalDatabase {
|
class LocalDatabase {
|
||||||
constructor (storagePath) {
|
constructor (storagePath) {
|
||||||
@@ -40,8 +69,17 @@ class LocalDatabase {
|
|||||||
async ready () {
|
async ready () {
|
||||||
if (this.backend) return this.backend
|
if (this.backend) return this.backend
|
||||||
if (this.engine === 'hyperdb') {
|
if (this.engine === 'hyperdb') {
|
||||||
this.backend = HyperDB.rocks(this.storagePath, def)
|
const rocks = HyperDB.rocks(this.storagePath, def)
|
||||||
|
await rocks.ready()
|
||||||
|
const hasUsers = await hyperdbHasUsers(rocks)
|
||||||
|
if (!hasUsers && jsonStoreHasUsers(this.storagePath)) {
|
||||||
|
await rocks.close()
|
||||||
|
this.engine = 'json'
|
||||||
|
this.backend = new JsonStore(this.storagePath)
|
||||||
await this.backend.ready()
|
await this.backend.ready()
|
||||||
|
return this.backend
|
||||||
|
}
|
||||||
|
this.backend = rocks
|
||||||
} else {
|
} else {
|
||||||
this.backend = new JsonStore(this.storagePath)
|
this.backend = new JsonStore(this.storagePath)
|
||||||
await this.backend.ready()
|
await this.backend.ready()
|
||||||
@@ -130,4 +168,4 @@ class LocalDatabase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { LocalDatabase, hasHyperDB }
|
module.exports = { LocalDatabase, hasHyperDB, jsonStoreHasUsers }
|
||||||
|
|||||||
Reference in New Issue
Block a user