2021-06-24 19:17:26 +02:00
|
|
|
import {
|
2023-05-15 18:36:16 +02:00
|
|
|
ConnectionInfo,
|
2023-05-05 16:47:55 +02:00
|
|
|
DatasourceFeature,
|
2022-08-11 14:50:05 +02:00
|
|
|
DatasourceFieldType,
|
2023-01-31 20:49:31 +01:00
|
|
|
Document,
|
|
|
|
Integration,
|
2022-08-11 12:48:58 +02:00
|
|
|
IntegrationBase,
|
2023-01-31 20:49:31 +01:00
|
|
|
QueryType,
|
2022-08-11 12:48:58 +02:00
|
|
|
} from "@budibase/types"
|
2023-01-31 20:49:31 +01:00
|
|
|
import { db as dbCore } from "@budibase/backend-core"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
interface CouchDBConfig {
|
|
|
|
url: string
|
|
|
|
database: string
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://docs.couchdb.org/en/stable/",
|
|
|
|
friendlyName: "CouchDB",
|
|
|
|
type: "Non-relational",
|
|
|
|
description:
|
|
|
|
"Apache CouchDB is an open-source document-oriented NoSQL database, implemented in Erlang.",
|
2023-05-24 10:50:51 +02:00
|
|
|
features: {
|
|
|
|
[DatasourceFeature.CONNECTION_CHECKING]: true,
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
datasource: {
|
|
|
|
url: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
default: "http://localhost:5984",
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
database: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
read: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
|
|
|
update: {
|
|
|
|
type: QueryType.JSON,
|
|
|
|
},
|
2023-01-31 20:49:31 +01:00
|
|
|
get: {
|
|
|
|
type: QueryType.FIELDS,
|
|
|
|
fields: {
|
|
|
|
id: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
delete: {
|
|
|
|
type: QueryType.FIELDS,
|
|
|
|
fields: {
|
|
|
|
id: {
|
|
|
|
type: DatasourceFieldType.STRING,
|
|
|
|
required: true,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
class CouchDBIntegration implements IntegrationBase {
|
2023-05-11 15:58:51 +02:00
|
|
|
private readonly client: dbCore.DatabaseImpl
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
constructor(config: CouchDBConfig) {
|
2023-02-01 14:01:38 +01:00
|
|
|
this.client = dbCore.DatabaseWithConnection(config.database, config.url)
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2023-05-12 12:35:07 +02:00
|
|
|
async testConnection() {
|
2023-05-15 18:36:16 +02:00
|
|
|
const response: ConnectionInfo = {
|
|
|
|
connected: false,
|
|
|
|
}
|
2023-05-12 12:35:07 +02:00
|
|
|
try {
|
|
|
|
const result = await this.query("exists", "validation error", {})
|
2023-05-15 18:36:16 +02:00
|
|
|
response.connected = result === true
|
2023-05-12 12:35:07 +02:00
|
|
|
} catch (e: any) {
|
2023-05-15 18:36:16 +02:00
|
|
|
response.error = e.message as string
|
2023-05-12 12:35:07 +02:00
|
|
|
}
|
2023-05-15 18:36:16 +02:00
|
|
|
return response
|
2023-05-12 12:35:07 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async query(
|
|
|
|
command: string,
|
|
|
|
errorMsg: string,
|
|
|
|
query: { json?: object; id?: string }
|
|
|
|
) {
|
|
|
|
try {
|
2023-05-11 15:58:51 +02:00
|
|
|
return await (this.client as any)[command](query.id || query.json)
|
2022-08-12 18:03:06 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(errorMsg, err)
|
|
|
|
throw err
|
2022-04-19 20:42:52 +02:00
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2022-04-19 20:42:52 +02:00
|
|
|
|
2023-01-31 20:49:31 +01:00
|
|
|
private parse(query: { json: string | object }) {
|
|
|
|
return typeof query.json === "string" ? JSON.parse(query.json) : query.json
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(query: { json: string | object }) {
|
|
|
|
const parsed = this.parse(query)
|
|
|
|
return this.query("post", "Error writing to couchDB", { json: parsed })
|
2022-08-12 18:03:06 +02:00
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2023-01-31 20:49:31 +01:00
|
|
|
async read(query: { json: string | object }) {
|
|
|
|
const parsed = this.parse(query)
|
2022-08-12 18:03:06 +02:00
|
|
|
const result = await this.query("allDocs", "Error querying couchDB", {
|
|
|
|
json: {
|
|
|
|
include_docs: true,
|
2023-01-31 20:49:31 +01:00
|
|
|
...parsed,
|
2022-08-12 18:03:06 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return result.rows.map((row: { doc: object }) => row.doc)
|
|
|
|
}
|
2021-06-24 19:16:48 +02:00
|
|
|
|
2023-01-31 20:49:31 +01:00
|
|
|
async update(query: { json: string | object }) {
|
|
|
|
const parsed: Document = this.parse(query)
|
|
|
|
if (!parsed?._rev && parsed?._id) {
|
|
|
|
const oldDoc = await this.get({ id: parsed._id })
|
|
|
|
parsed._rev = oldDoc._rev
|
|
|
|
}
|
|
|
|
return this.query("put", "Error updating couchDB document", {
|
|
|
|
json: parsed,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(query: { id: string }) {
|
|
|
|
return this.query("get", "Error retrieving couchDB document by ID", {
|
|
|
|
id: query.id,
|
|
|
|
})
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-12 18:03:06 +02:00
|
|
|
async delete(query: { id: string }) {
|
|
|
|
const doc = await this.query("get", "Cannot find doc to be deleted", query)
|
|
|
|
return this.query("remove", "Error deleting couchDB document", {
|
|
|
|
json: doc,
|
|
|
|
})
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-12 18:03:06 +02:00
|
|
|
|
|
|
|
export default {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: CouchDBIntegration,
|
|
|
|
}
|