Adding case to handle multi-DB setups, to confirm store same doc ID to different databases and they run in different cache keys.
This commit is contained in:
parent
eeca1cb3ba
commit
c62b6da703
|
@ -9,7 +9,8 @@ tk.freeze(START_DATE)
|
|||
const DELAY = 5000
|
||||
|
||||
const db = dangerousGetDB("test")
|
||||
const writethrough = new Writethrough(db, DELAY)
|
||||
const db2 = dangerousGetDB("test2")
|
||||
const writethrough = new Writethrough(db, DELAY), writethrough2 = new Writethrough(db2, DELAY)
|
||||
|
||||
describe("writethrough", () => {
|
||||
describe("put", () => {
|
||||
|
@ -43,5 +44,16 @@ describe("writethrough", () => {
|
|||
expect(response.value).toBe(3)
|
||||
})
|
||||
})
|
||||
|
||||
describe("same doc, different databases (tenancy)", () => {
|
||||
it("should be able to two different databases", async () => {
|
||||
const resp1 = await writethrough.put({ _id: "db1", value: "first" })
|
||||
const resp2 = await writethrough2.put({ _id: "db1", value: "second" })
|
||||
expect(resp1.rev).toBeDefined()
|
||||
expect(resp2.rev).toBeDefined()
|
||||
expect((await db.get("db1")).value).toBe("first")
|
||||
expect((await db2.get("db1")).value).toBe("second")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -17,6 +17,10 @@ async function getCache() {
|
|||
return CACHE
|
||||
}
|
||||
|
||||
function makeCacheKey(db: PouchDB.Database, key: string) {
|
||||
return db.name + key
|
||||
}
|
||||
|
||||
function makeCacheItem(doc: any, lastWrite: number | null = null): CacheItem {
|
||||
return { doc, lastWrite: lastWrite || Date.now() }
|
||||
}
|
||||
|
@ -28,10 +32,11 @@ export async function put(
|
|||
) {
|
||||
const cache = await getCache()
|
||||
const key = doc._id
|
||||
let cacheItem: CacheItem | undefined = await cache.get(key)
|
||||
let cacheItem: CacheItem | undefined = await cache.get(makeCacheKey(db, key))
|
||||
const updateDb = !cacheItem || cacheItem.lastWrite < Date.now() - writeRateMs
|
||||
let output = doc
|
||||
if (updateDb) {
|
||||
try {
|
||||
// doc should contain the _id and _rev
|
||||
const response = await db.put(doc)
|
||||
output = {
|
||||
|
@ -39,20 +44,27 @@ export async function put(
|
|||
_id: response.id,
|
||||
_rev: response.rev,
|
||||
}
|
||||
} catch (err: any) {
|
||||
// ignore 409s, some other high speed write has hit it first, just move straight to caching
|
||||
if (err.status !== 409) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
}
|
||||
// if we are updating the DB then need to set the lastWrite to now
|
||||
cacheItem = makeCacheItem(output, updateDb ? null : cacheItem?.lastWrite)
|
||||
await cache.store(key, cacheItem)
|
||||
await cache.store(makeCacheKey(db, key), cacheItem)
|
||||
return { ok: true, id: output._id, rev: output._rev }
|
||||
}
|
||||
|
||||
export async function get(db: PouchDB.Database, id: string): Promise<any> {
|
||||
const cache = await getCache()
|
||||
let cacheItem: CacheItem = await cache.get(id)
|
||||
const cacheKey = makeCacheKey(db, id)
|
||||
let cacheItem: CacheItem = await cache.get(cacheKey)
|
||||
if (!cacheItem) {
|
||||
const doc = await db.get(id)
|
||||
cacheItem = makeCacheItem(doc)
|
||||
await cache.store(id, cacheItem)
|
||||
await cache.store(cacheKey, cacheItem)
|
||||
}
|
||||
return cacheItem.doc
|
||||
}
|
||||
|
@ -69,7 +81,7 @@ export async function remove(
|
|||
const id = typeof docOrId === "string" ? docOrId : docOrId._id
|
||||
rev = typeof docOrId === "string" ? rev : docOrId._rev
|
||||
try {
|
||||
await cache.delete(id)
|
||||
await cache.delete(makeCacheKey(db, id))
|
||||
} finally {
|
||||
await db.remove(id, rev)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue