2021-06-24 19:17:26 +02:00
|
|
|
import {
|
|
|
|
Integration,
|
2022-08-11 14:50:05 +02:00
|
|
|
DatasourceFieldType,
|
|
|
|
QueryType,
|
2022-08-11 12:48:58 +02:00
|
|
|
IntegrationBase,
|
|
|
|
} from "@budibase/types"
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
module CouchDBModule {
|
|
|
|
const PouchDB = require("pouchdb")
|
|
|
|
|
|
|
|
interface CouchDBConfig {
|
|
|
|
url: string
|
|
|
|
database: string
|
|
|
|
}
|
|
|
|
|
|
|
|
const SCHEMA: Integration = {
|
|
|
|
docs: "https://docs.couchdb.org/en/stable/",
|
|
|
|
friendlyName: "CouchDB",
|
2022-06-23 12:35:57 +02:00
|
|
|
type: "Non-relational",
|
2021-06-24 19:16:48 +02:00
|
|
|
description:
|
|
|
|
"Apache CouchDB is an open-source document-oriented NoSQL database, implemented in Erlang.",
|
|
|
|
datasource: {
|
|
|
|
url: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
default: "http://localhost:5984",
|
|
|
|
},
|
|
|
|
database: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
create: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.JSON,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
read: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.JSON,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
update: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.JSON,
|
2021-06-24 19:16:48 +02:00
|
|
|
},
|
|
|
|
delete: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: QueryType.FIELDS,
|
2021-06-24 19:16:48 +02:00
|
|
|
fields: {
|
|
|
|
id: {
|
2022-08-11 14:50:05 +02:00
|
|
|
type: DatasourceFieldType.STRING,
|
2021-06-24 19:16:48 +02:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:35:09 +01:00
|
|
|
class CouchDBIntegration implements IntegrationBase {
|
2021-06-24 19:16:48 +02:00
|
|
|
private config: CouchDBConfig
|
2022-04-19 20:42:52 +02:00
|
|
|
private readonly client: any
|
2021-06-24 19:16:48 +02:00
|
|
|
|
|
|
|
constructor(config: CouchDBConfig) {
|
|
|
|
this.config = config
|
|
|
|
this.client = new PouchDB(`${config.url}/${config.database}`)
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:42:52 +02:00
|
|
|
async query(
|
|
|
|
command: string,
|
|
|
|
errorMsg: string,
|
|
|
|
query: { json?: object; id?: string }
|
|
|
|
) {
|
2021-06-24 19:16:48 +02:00
|
|
|
try {
|
2022-04-19 20:42:52 +02:00
|
|
|
const response = await this.client[command](query.id || query.json)
|
|
|
|
await this.client.close()
|
|
|
|
return response
|
2021-06-24 19:16:48 +02:00
|
|
|
} catch (err) {
|
2022-04-19 20:42:52 +02:00
|
|
|
console.error(errorMsg, err)
|
2021-06-24 19:16:48 +02:00
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 20:42:52 +02:00
|
|
|
async create(query: { json: object }) {
|
|
|
|
return this.query("post", "Error writing to couchDB", query)
|
|
|
|
}
|
|
|
|
|
2021-06-24 19:16:48 +02:00
|
|
|
async read(query: { json: object }) {
|
2022-04-19 20:42:52 +02:00
|
|
|
const result = await this.query("allDocs", "Error querying couchDB", {
|
|
|
|
json: {
|
2021-06-24 19:16:48 +02:00
|
|
|
include_docs: true,
|
|
|
|
...query.json,
|
2022-04-19 20:42:52 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
return result.rows.map((row: { doc: object }) => row.doc)
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async update(query: { json: object }) {
|
2022-04-19 20:42:52 +02:00
|
|
|
return this.query("put", "Error updating couchDB document", query)
|
2021-06-24 19:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async delete(query: { id: string }) {
|
2022-04-19 20:42:52 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
schema: SCHEMA,
|
|
|
|
integration: CouchDBIntegration,
|
|
|
|
}
|
|
|
|
}
|