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

51 lines
955 B
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 = {
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: "localhost",
},
database: {
2021-01-13 19:29:51 +01:00
type: FIELD_TYPES.STRING,
2021-01-11 18:18:22 +01:00
required: true,
},
view: {
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: {
json: {
2021-01-13 19:29:51 +01:00
type: QUERY_TYPES.JSON,
2021-01-11 18:18:22 +01:00
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,
}