Fix for CouchDB integration, the typing in it was very poor and wise hiding issues, I've updated how this is implemented so that static typing can catch issues with it.
This commit is contained in:
parent
00e1d5ff2a
commit
cb41e4d5a1
|
@ -80,6 +80,11 @@ export function DatabaseWithConnection(
|
||||||
connection: string,
|
connection: string,
|
||||||
opts?: DatabaseOpts
|
opts?: DatabaseOpts
|
||||||
) {
|
) {
|
||||||
|
if (!dbName || !connection) {
|
||||||
|
throw new Error(
|
||||||
|
"Unable to create database without database name or connection"
|
||||||
|
)
|
||||||
|
}
|
||||||
const db = new DatabaseImpl(dbName, opts, connection)
|
const db = new DatabaseImpl(dbName, opts, connection)
|
||||||
return new DDInstrumentedDatabase(db)
|
return new DDInstrumentedDatabase(db)
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,6 +71,9 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
private readonly client: Database
|
private readonly client: Database
|
||||||
|
|
||||||
constructor(config: CouchDBConfig) {
|
constructor(config: CouchDBConfig) {
|
||||||
|
if (!config.url || !config.database) {
|
||||||
|
throw new Error("Unable to connect without URL or database")
|
||||||
|
}
|
||||||
this.client = dbCore.DatabaseWithConnection(config.database, config.url)
|
this.client = dbCore.DatabaseWithConnection(config.database, config.url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,21 +82,19 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
connected: false,
|
connected: false,
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const result = await this.query("exists", "validation error", {})
|
response.connected = await this.query(
|
||||||
response.connected = result === true
|
() => this.client.exists(),
|
||||||
|
"validation error"
|
||||||
|
)
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
response.error = e.message as string
|
response.error = e.message as string
|
||||||
}
|
}
|
||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
async query(
|
async query<T>(operation: () => Promise<T>, errorMsg: string) {
|
||||||
command: string,
|
|
||||||
errorMsg: string,
|
|
||||||
query: { json?: object; id?: string }
|
|
||||||
) {
|
|
||||||
try {
|
try {
|
||||||
return await (this.client as any)[command](query.id || query.json)
|
return await operation()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(errorMsg, err)
|
console.error(errorMsg, err)
|
||||||
throw err
|
throw err
|
||||||
|
@ -106,18 +107,20 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
|
|
||||||
async create(query: { json: string | object }) {
|
async create(query: { json: string | object }) {
|
||||||
const parsed = this.parse(query)
|
const parsed = this.parse(query)
|
||||||
return this.query("post", "Error writing to couchDB", { json: parsed })
|
return this.query(() => this.client.put(parsed), "Error writing to couchDB")
|
||||||
}
|
}
|
||||||
|
|
||||||
async read(query: { json: string | object }) {
|
async read(query: { json: string | object }) {
|
||||||
const parsed = this.parse(query)
|
const parsed = this.parse(query)
|
||||||
const result = await this.query("allDocs", "Error querying couchDB", {
|
const params = {
|
||||||
json: {
|
include_docs: true,
|
||||||
include_docs: true,
|
...parsed,
|
||||||
...parsed,
|
}
|
||||||
},
|
const result = await this.query(
|
||||||
})
|
() => this.client.allDocs(params),
|
||||||
return result.rows.map((row: { doc: object }) => row.doc)
|
"Error querying couchDB"
|
||||||
|
)
|
||||||
|
return result.rows.map(row => row.doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(query: { json: string | object }) {
|
async update(query: { json: string | object }) {
|
||||||
|
@ -126,22 +129,24 @@ class CouchDBIntegration implements IntegrationBase {
|
||||||
const oldDoc = await this.get({ id: parsed._id })
|
const oldDoc = await this.get({ id: parsed._id })
|
||||||
parsed._rev = oldDoc._rev
|
parsed._rev = oldDoc._rev
|
||||||
}
|
}
|
||||||
return this.query("put", "Error updating couchDB document", {
|
return this.query(
|
||||||
json: parsed,
|
() => this.client.put(parsed),
|
||||||
})
|
"Error updating couchDB document"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(query: { id: string }) {
|
async get(query: { id: string }) {
|
||||||
return this.query("get", "Error retrieving couchDB document by ID", {
|
return this.query(
|
||||||
id: query.id,
|
() => this.client.get(query.id),
|
||||||
})
|
"Error retrieving couchDB document by ID"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(query: { id: string }) {
|
async delete(query: { id: string }) {
|
||||||
const doc = await this.query("get", "Cannot find doc to be deleted", query)
|
return this.query(
|
||||||
return this.query("remove", "Error deleting couchDB document", {
|
() => this.client.remove(query.id),
|
||||||
json: doc,
|
"Error deleting couchDB document"
|
||||||
})
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue