Further simplification.

This commit is contained in:
mike12345567 2024-07-03 16:59:31 +01:00
parent 378bf6d42f
commit e83c37263d
1 changed files with 6 additions and 30 deletions

View File

@ -82,32 +82,20 @@ class CouchDBIntegration implements IntegrationBase {
connected: false, connected: false,
} }
try { try {
response.connected = await this.query( response.connected = await this.client.exists()
() => 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<T>(operation: () => Promise<T>, errorMsg: string) {
try {
return await operation()
} catch (err) {
console.error(errorMsg, err)
throw err
}
}
private parse(query: { json: string | object }) { private parse(query: { json: string | object }) {
return typeof query.json === "string" ? JSON.parse(query.json) : query.json return typeof query.json === "string" ? JSON.parse(query.json) : query.json
} }
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(() => this.client.put(parsed), "Error writing to couchDB") return await this.client.put(parsed)
} }
async read(query: { json: string | object }) { async read(query: { json: string | object }) {
@ -116,10 +104,7 @@ class CouchDBIntegration implements IntegrationBase {
include_docs: true, include_docs: true,
...parsed, ...parsed,
} }
const result = await this.query( const result = await this.client.allDocs(params)
() => this.client.allDocs(params),
"Error querying couchDB"
)
return result.rows.map(row => row.doc) return result.rows.map(row => row.doc)
} }
@ -129,24 +114,15 @@ 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( return await this.client.put(parsed)
() => this.client.put(parsed),
"Error updating couchDB document"
)
} }
async get(query: { id: string }) { async get(query: { id: string }) {
return this.query( return await this.client.get(query.id)
() => this.client.get(query.id),
"Error retrieving couchDB document by ID"
)
} }
async delete(query: { id: string }) { async delete(query: { id: string }) {
return this.query( return await this.client.remove(query.id)
() => this.client.remove(query.id),
"Error deleting couchDB document"
)
} }
} }