Merge pull request #1088 from orbitdb/fix/instance-caching

Fix db instance caching
This commit is contained in:
Haad 2023-09-02 07:30:48 +03:00 committed by GitHub
commit ebbaf60bd3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -131,6 +131,10 @@ const OrbitDB = async ({ ipfs, id, identities, directory } = {}) => {
address = OrbitDBAddress(m.hash)
name = manifest.name
meta = manifest.meta
// Check if we already have the database open and return if it is
if (databases[address]) {
return databases[address]
}
}
Database = Database || getDatabaseType(type)()

View File

@ -527,4 +527,39 @@ describe('Open databases', function () {
deepStrictEqual(all.map(e => e.value), expected)
})
})
describe('opening same database', () => {
let db
before(async () => {
orbitdb1 = await createOrbitDB({ ipfs: ipfs1, id: 'user1' })
})
after(async () => {
if (db) {
await db.close()
}
if (orbitdb1) {
await orbitdb1.stop()
}
await rmrf('./orbitdb')
})
it('returns the database instance when opened with a name multiple times', async () => {
let err
let db1, db2
try {
db1 = await orbitdb1.open('helloworld1')
db2 = await orbitdb1.open('helloworld1')
} catch (e) {
err = e
}
strictEqual(err, undefined)
strictEqual(db1.name, 'helloworld1')
strictEqual(db2.name, 'helloworld1')
strictEqual(db1.address, db2.address)
})
})
})