budibase/packages/server/src/integrations/couchdb.js

96 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-11-26 18:34:15 +01:00
const PouchDB = require("pouchdb")
2021-01-13 19:29:51 +01:00
const { FIELD_TYPES, QUERY_TYPES } = require("./Integration")
2020-11-26 18:03:18 +01:00
2021-01-11 18:18:22 +01:00
const SCHEMA = {
2021-01-15 18:29:46 +01:00
docs: "https://docs.couchdb.org/en/stable/",
friendlyName: "CouchDB",
description:
"Apache CouchDB is an open-source document-oriented NoSQL database, implemented in Erlang.",
2021-01-11 18:18:22 +01:00
datasource: {
url: {
2021-01-13 19:29:51 +01:00
type: FIELD_TYPES.STRING,
2021-01-11 18:18:22 +01:00
required: true,
default: "http://localhost:5984",
2021-01-11 18:18:22 +01:00
},
database: {
2021-01-13 19:29:51 +01:00
type: FIELD_TYPES.STRING,
2021-01-11 18:18:22 +01:00
required: true,
},
2020-11-26 18:34:15 +01:00
},
2021-01-11 18:18:22 +01:00
query: {
create: {
type: QUERY_TYPES.JSON,
},
read: {
type: QUERY_TYPES.JSON,
},
update: {
type: QUERY_TYPES.JSON,
},
delete: {
type: QUERY_TYPES.FIELDS,
fields: {
id: {
type: FIELD_TYPES.STRING,
required: true,
},
},
2021-01-11 18:18:22 +01:00
},
2020-11-26 18:34:15 +01:00
},
}
2020-11-26 18:03:18 +01:00
2020-11-26 18:34:15 +01:00
class CouchDBIntegration {
constructor(config) {
this.config = config
this.client = new PouchDB(`${config.url}/${config.database}`)
}
2020-11-26 18:03:18 +01:00
async create(query) {
try {
const result = await this.client.post(query.json)
return result
} catch (err) {
console.error("Error writing to couchDB", err)
throw err
}
}
async read(query) {
2020-11-26 18:34:15 +01:00
try {
const result = await this.client.allDocs({
include_docs: true,
...query.json,
2020-11-26 18:34:15 +01:00
})
2021-05-04 12:32:22 +02:00
return result.rows.map(row => row.doc)
2020-11-26 18:34:15 +01:00
} catch (err) {
console.error("Error querying couchDB", err)
throw err
}
}
async update(query) {
try {
const result = await this.client.put(query.json)
return result
} catch (err) {
console.error("Error updating couchDB document", err)
throw err
}
}
async delete(query) {
try {
const result = await this.client.remove(query.id)
return result
} catch (err) {
console.error("Error deleting couchDB document", err)
throw err
}
}
2020-11-26 18:34:15 +01:00
}
2020-11-26 18:03:18 +01:00
2020-11-26 18:34:15 +01:00
module.exports = {
2021-01-11 18:18:22 +01:00
schema: SCHEMA,
2020-11-26 18:34:15 +01:00
integration: CouchDBIntegration,
}