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

50 lines
853 B
JavaScript
Raw Normal View History

2020-11-26 18:34:15 +01:00
const PouchDB = require("pouchdb")
2020-11-26 18:03:18 +01:00
2021-01-11 18:18:22 +01:00
const SCHEMA = {
datasource: {
url: {
type: "string",
required: true,
default: "localhost",
},
database: {
type: "string",
required: true,
},
view: {
type: "string",
required: true,
},
2020-11-26 18:34:15 +01:00
},
2021-01-11 18:18:22 +01:00
query: {
json: {
type: "json",
required: true,
},
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
2021-01-12 17:49:11 +01:00
async read() {
2020-11-26 18:34:15 +01:00
try {
const result = await this.client.allDocs({
include_docs: true,
})
return result.rows.map(row => row.doc)
} catch (err) {
console.error("Error querying couchDB", err)
throw err
}
}
}
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,
}