1
0
Fork 0
mirror of synced 2024-07-09 00:06:05 +12:00

Use overloads

This commit is contained in:
Adria Navarro 2024-03-01 13:59:51 +01:00
parent eb78103764
commit 3c944073eb
5 changed files with 35 additions and 31 deletions

View file

@ -35,7 +35,7 @@ describe("docWritethrough", () => {
beforeEach(() => { beforeEach(() => {
resetTime() resetTime()
documentId = structures.db.id() documentId = structures.uuid()
docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS) docWritethrough = new DocWritethrough(db, documentId, WRITE_RATE_MS)
}) })
@ -47,7 +47,7 @@ describe("docWritethrough", () => {
travelForward(WRITE_RATE_MS - 1) travelForward(WRITE_RATE_MS - 1)
await docWritethrough.patch(generatePatchObject(2)) 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) 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) 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({ const response = await directCouchUrlCall({
url: `${this.couchInfo.url}/${this.name}`, url: `${this.couchInfo.url}/${this.name}`,
method: "HEAD", method: "HEAD",
@ -79,6 +87,15 @@ export class DatabaseImpl implements Database {
return response.status === 200 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() { private nano() {
return this.instanceNano || DatabaseImpl.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>( async getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean }

View file

@ -24,9 +24,12 @@ export class DDInstrumentedDatabase implements Database {
return this.db.name return this.db.name
} }
exists(): Promise<boolean> { exists(docId?: string): Promise<boolean> {
return tracer.trace("db.exists", span => { 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() 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>( getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean | undefined } | undefined 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 }))) 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 () => { it("can check existing docs by id", async () => {
const existingDoc = _.sample(documents) const existingDoc = _.sample(documents)
const result = await database.docExists(existingDoc!._id!) const result = await database.exists(existingDoc!._id!)
expect(result).toBe(true) expect(result).toBe(true)
}) })
it("can check non existing docs by id", async () => { 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) expect(result).toBe(false)
}) })
@ -36,9 +36,9 @@ describe("DatabaseImpl", () => {
const id = existingDoc!._id! const id = existingDoc!._id!
const results = [] const results = []
results.push(await database.docExists(id)) results.push(await database.exists(id))
results.push(await database.docExists(id)) results.push(await database.exists(id))
results.push(await database.docExists(id)) results.push(await database.exists(id))
expect(results).toEqual([true, true, true]) expect(results).toEqual([true, true, true])
}) })
@ -46,10 +46,10 @@ describe("DatabaseImpl", () => {
it("returns false after the doc is deleted", async () => { it("returns false after the doc is deleted", async () => {
const existingDoc = _.sample(documents) const existingDoc = _.sample(documents)
const id = existingDoc!._id! const id = existingDoc!._id!
expect(await database.docExists(id)).toBe(true) expect(await database.exists(id)).toBe(true)
await database.remove(existingDoc!) 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> exists(): Promise<boolean>
get<T extends Document>(id?: string): Promise<T> get<T extends Document>(id?: string): Promise<T>
docExists(id: string): Promise<boolean> exists(docId: string): Promise<boolean>
getMultiple<T extends Document>( getMultiple<T extends Document>(
ids: string[], ids: string[],
opts?: { allowMissing?: boolean } opts?: { allowMissing?: boolean }