Validate couchdb

This commit is contained in:
Adria Navarro 2023-05-11 15:58:51 +02:00
parent d9a38b0908
commit 209ada0c38
3 changed files with 39 additions and 10 deletions

View File

@ -12,7 +12,7 @@ import {
isDocument, isDocument,
} from "@budibase/types" } from "@budibase/types"
import { getCouchInfo } from "./connections" import { getCouchInfo } from "./connections"
import { directCouchCall } from "./utils" import { directCouchUrlCall } from "./utils"
import { getPouchDB } from "./pouchDB" import { getPouchDB } from "./pouchDB"
import { WriteStream, ReadStream } from "fs" import { WriteStream, ReadStream } from "fs"
import { newid } from "../../docIds/newid" import { newid } from "../../docIds/newid"
@ -46,6 +46,8 @@ export class DatabaseImpl implements Database {
private readonly instanceNano?: Nano.ServerScope private readonly instanceNano?: Nano.ServerScope
private readonly pouchOpts: DatabaseOpts private readonly pouchOpts: DatabaseOpts
private readonly couchInfo = getCouchInfo()
constructor(dbName?: string, opts?: DatabaseOpts, connection?: string) { constructor(dbName?: string, opts?: DatabaseOpts, connection?: string) {
if (dbName == null) { if (dbName == null) {
throw new Error("Database name cannot be undefined.") throw new Error("Database name cannot be undefined.")
@ -53,8 +55,8 @@ export class DatabaseImpl implements Database {
this.name = dbName this.name = dbName
this.pouchOpts = opts || {} this.pouchOpts = opts || {}
if (connection) { if (connection) {
const couchInfo = getCouchInfo(connection) this.couchInfo = getCouchInfo(connection)
this.instanceNano = buildNano(couchInfo) this.instanceNano = buildNano(this.couchInfo)
} }
if (!DatabaseImpl.nano) { if (!DatabaseImpl.nano) {
DatabaseImpl.init() DatabaseImpl.init()
@ -67,7 +69,11 @@ export class DatabaseImpl implements Database {
} }
async exists() { async exists() {
let response = await directCouchCall(`/${this.name}`, "HEAD") const response = await directCouchUrlCall({
url: `${this.couchInfo.url}/${this.name}`,
method: "HEAD",
cookie: this.couchInfo.cookie,
})
return response.status === 200 return response.status === 200
} }

View File

@ -9,6 +9,20 @@ export async function directCouchCall(
) { ) {
let { url, cookie } = getCouchInfo() let { url, cookie } = getCouchInfo()
const couchUrl = `${url}/${path}` const couchUrl = `${url}/${path}`
return await directCouchUrlCall({ url: couchUrl, cookie, method, body })
}
export async function directCouchUrlCall({
url,
cookie,
method,
body,
}: {
url: string
cookie: string
method: string
body?: any
}) {
const params: any = { const params: any = {
method: method, method: method,
headers: { headers: {
@ -19,7 +33,7 @@ export async function directCouchCall(
params.body = JSON.stringify(body) params.body = JSON.stringify(body)
params.headers["Content-Type"] = "application/json" params.headers["Content-Type"] = "application/json"
} }
return await fetch(checkSlashesInUrl(encodeURI(couchUrl)), params) return await fetch(checkSlashesInUrl(encodeURI(url)), params)
} }
export async function directCouchQuery( export async function directCouchQuery(

View File

@ -7,7 +7,7 @@ import {
} from "@budibase/types" } from "@budibase/types"
import { db as dbCore } from "@budibase/backend-core" import { db as dbCore } from "@budibase/backend-core"
interface CouchDBConfig { export interface CouchDBConfig {
url: string url: string
database: string database: string
} }
@ -61,11 +61,9 @@ const SCHEMA: Integration = {
} }
class CouchDBIntegration implements IntegrationBase { class CouchDBIntegration implements IntegrationBase {
private config: CouchDBConfig private readonly client: dbCore.DatabaseImpl
private readonly client: any
constructor(config: CouchDBConfig) { constructor(config: CouchDBConfig) {
this.config = config
this.client = dbCore.DatabaseWithConnection(config.database, config.url) this.client = dbCore.DatabaseWithConnection(config.database, config.url)
} }
@ -75,7 +73,7 @@ class CouchDBIntegration implements IntegrationBase {
query: { json?: object; id?: string } query: { json?: object; id?: string }
) { ) {
try { try {
return await this.client[command](query.id || query.json) return await (this.client as any)[command](query.id || query.json)
} catch (err) { } catch (err) {
console.error(errorMsg, err) console.error(errorMsg, err)
throw err throw err
@ -127,7 +125,18 @@ class CouchDBIntegration implements IntegrationBase {
} }
} }
async function validateConnection(config: CouchDBConfig) {
const integration = new CouchDBIntegration(config)
try {
const result = await integration.query("exists", "validation error", {})
return result === true
} catch (e: any) {
return { error: e.message as string }
}
}
export default { export default {
schema: SCHEMA, schema: SCHEMA,
integration: CouchDBIntegration, integration: CouchDBIntegration,
validateConnection,
} }