Use overloads

This commit is contained in:
Adria Navarro 2024-03-01 13:59:51 +01:00
parent 82a6f9027e
commit 774ff42f0c
5 changed files with 35 additions and 31 deletions

View File

@ -35,7 +35,7 @@ describe("docWritethrough", () => {
beforeEach(() => {
resetTime()
documentId = structures.db.id()
documentId = structures.uuid()
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
})
@ -47,7 +47,7 @@ describe("docWritethrough", () => {
travelForward(WRITE_RATE_MS - 1)
await docWritethrough.patch(generatePatchObject(2))
expect(await db.docExists(documentId)).toBe(false)
expect(await db.exists(documentId)).toBe(false)
})
})
@ -136,7 +136,7 @@ describe("docWritethrough", () => {
travelForward(WRITE_RATE_MS)
expect(await db.docExists(documentId)).toBe(false)
expect(await db.exists(documentId)).toBe(false)
})
})

View File

@ -70,7 +70,15 @@ export class DatabaseImpl implements Database {
DatabaseImpl.nano = buildNano(couchInfo)
}
async exists() {
exists(docId?: string) {
if (docId === undefined) {
return this.dbExists()
}
return this.docExists(docId)
}
private async dbExists() {
const response = await directCouchUrlCall({
url: `${this.couchInfo.url}/${this.name}`,
method: "HEAD",
@ -79,6 +87,15 @@ export class DatabaseImpl implements Database {
return response.status === 200
}
private async docExists(id: string): Promise<boolean> {
try {
await this.performCall(db => () => db.head(id))
return true
} catch {
return false
}
}
private nano() {
return this.instanceNano || DatabaseImpl.nano
}
@ -135,15 +152,6 @@ export class DatabaseImpl implements Database {
})
}
async docExists(id: string): Promise<boolean> {
try {
await this.performCall(db => () => db.head(id))
return true
} catch {
return false
}
}
async getMultiple<T extends Document>(
ids: string[],
opts?: { allowMissing?: boolean }

View File

@ -24,9 +24,12 @@ export class DDInstrumentedDatabase implements Database {
return this.db.name
}
exists(): Promise<boolean> {
exists(docId?: string): Promise<boolean> {
return tracer.trace("db.exists", span => {
span?.addTags({ db_name: this.name })
span?.addTags({ db_name: this.name, doc_id: docId })
if (docId) {
return this.db.exists(docId)
}
return this.db.exists()
})
}
@ -38,13 +41,6 @@ export class DDInstrumentedDatabase implements Database {
})
}
docExists(id: string): Promise<boolean> {
return tracer.trace("db.docExists", span => {
span?.addTags({ db_name: this.name, doc_id: id })
return this.db.docExists(id)
})
}
getMultiple<T extends Document>(
ids: string[],
opts?: { allowMissing?: boolean | undefined } | undefined

View File

@ -17,16 +17,16 @@ describe("DatabaseImpl", () => {
documents.push(...createdDocs.map((x: any) => ({ _id: x.id, _rev: x.rev })))
})
describe("docExists", () => {
describe("document exists", () => {
it("can check existing docs by id", async () => {
const existingDoc = _.sample(documents)
const result = await database.docExists(existingDoc!._id!)
const result = await database.exists(existingDoc!._id!)
expect(result).toBe(true)
})
it("can check non existing docs by id", async () => {
const result = await database.docExists(newid())
const result = await database.exists(newid())
expect(result).toBe(false)
})
@ -36,9 +36,9 @@ describe("DatabaseImpl", () => {
const id = existingDoc!._id!
const results = []
results.push(await database.docExists(id))
results.push(await database.docExists(id))
results.push(await database.docExists(id))
results.push(await database.exists(id))
results.push(await database.exists(id))
results.push(await database.exists(id))
expect(results).toEqual([true, true, true])
})
@ -46,10 +46,10 @@ describe("DatabaseImpl", () => {
it("returns false after the doc is deleted", async () => {
const existingDoc = _.sample(documents)
const id = existingDoc!._id!
expect(await database.docExists(id)).toBe(true)
expect(await database.exists(id)).toBe(true)
await database.remove(existingDoc!)
expect(await database.docExists(id)).toBe(false)
expect(await database.exists(id)).toBe(false)
})
})
})

View File

@ -128,7 +128,7 @@ export interface Database {
exists(): Promise<boolean>
get<T extends Document>(id?: string): Promise<T>
docExists(id: string): Promise<boolean>
exists(docId: string): Promise<boolean>
getMultiple<T extends Document>(
ids: string[],
opts?: { allowMissing?: boolean }