Fix CouchDB datasource.
This commit is contained in:
parent
72074dfd60
commit
885873ef29
|
@ -5,10 +5,10 @@ import {
|
|||
SqlQuery,
|
||||
Table,
|
||||
TableSourceType,
|
||||
SEPARATOR,
|
||||
} from "@budibase/types"
|
||||
import { DEFAULT_BB_DATASOURCE_ID } from "../constants"
|
||||
import { Knex } from "knex"
|
||||
import { SEPARATOR } from "../db"
|
||||
import environment from "../environment"
|
||||
|
||||
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`
|
||||
|
|
|
@ -62,12 +62,16 @@ const SCHEMA: Integration = {
|
|||
type: DatasourceFieldType.STRING,
|
||||
required: true,
|
||||
},
|
||||
rev: {
|
||||
type: DatasourceFieldType.STRING,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
class CouchDBIntegration implements IntegrationBase {
|
||||
export class CouchDBIntegration implements IntegrationBase {
|
||||
private readonly client: Database
|
||||
|
||||
constructor(config: CouchDBConfig) {
|
||||
|
@ -82,7 +86,8 @@ class CouchDBIntegration implements IntegrationBase {
|
|||
connected: false,
|
||||
}
|
||||
try {
|
||||
response.connected = await this.client.exists()
|
||||
await this.client.allDocs({ limit: 1 })
|
||||
response.connected = true
|
||||
} catch (e: any) {
|
||||
response.error = e.message as string
|
||||
}
|
||||
|
@ -99,13 +104,9 @@ class CouchDBIntegration implements IntegrationBase {
|
|||
}
|
||||
|
||||
async read(query: { json: string | object }) {
|
||||
const parsed = this.parse(query)
|
||||
const params = {
|
||||
include_docs: true,
|
||||
...parsed,
|
||||
}
|
||||
const params = { include_docs: true, ...this.parse(query) }
|
||||
const result = await this.client.allDocs(params)
|
||||
return result.rows.map(row => row.doc)
|
||||
return result.rows.map(row => row.doc!)
|
||||
}
|
||||
|
||||
async update(query: { json: string | object }) {
|
||||
|
@ -121,8 +122,8 @@ class CouchDBIntegration implements IntegrationBase {
|
|||
return await this.client.get(query.id)
|
||||
}
|
||||
|
||||
async delete(query: { id: string }) {
|
||||
return await this.client.remove(query.id)
|
||||
async delete(query: { id: string; rev: string }) {
|
||||
return await this.client.remove(query.id, query.rev)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,84 +1,87 @@
|
|||
jest.mock("@budibase/backend-core", () => {
|
||||
const core = jest.requireActual("@budibase/backend-core")
|
||||
return {
|
||||
...core,
|
||||
db: {
|
||||
...core.db,
|
||||
DatabaseWithConnection: function () {
|
||||
return {
|
||||
allDocs: jest.fn().mockReturnValue({ rows: [] }),
|
||||
put: jest.fn(),
|
||||
get: jest.fn().mockReturnValue({ _rev: "a" }),
|
||||
remove: jest.fn(),
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
import { env } from "@budibase/backend-core"
|
||||
import { CouchDBIntegration } from "../couchdb"
|
||||
import { generator } from "@budibase/backend-core/tests"
|
||||
|
||||
import { default as CouchDBIntegration } from "../couchdb"
|
||||
function couchSafeID(): string {
|
||||
// CouchDB IDs must start with a letter, so we prepend an 'a'.
|
||||
return `a${generator.guid()}`
|
||||
}
|
||||
|
||||
class TestConfiguration {
|
||||
integration: any
|
||||
function doc(data: Record<string, any>): string {
|
||||
return JSON.stringify({ _id: couchSafeID(), ...data })
|
||||
}
|
||||
|
||||
constructor(
|
||||
config: any = { url: "http://somewhere", database: "something" }
|
||||
) {
|
||||
this.integration = new CouchDBIntegration.integration(config)
|
||||
}
|
||||
function query(data?: Record<string, any>): { json: string } {
|
||||
return { json: doc(data || {}) }
|
||||
}
|
||||
|
||||
describe("CouchDB Integration", () => {
|
||||
let config: any
|
||||
let couchdb: CouchDBIntegration
|
||||
|
||||
beforeEach(() => {
|
||||
config = new TestConfiguration()
|
||||
})
|
||||
|
||||
it("calls the create method with the correct params", async () => {
|
||||
const doc = {
|
||||
test: 1,
|
||||
}
|
||||
await config.integration.create({
|
||||
json: JSON.stringify(doc),
|
||||
})
|
||||
expect(config.integration.client.put).toHaveBeenCalledWith(doc)
|
||||
})
|
||||
|
||||
it("calls the read method with the correct params", async () => {
|
||||
const doc = {
|
||||
name: "search",
|
||||
}
|
||||
|
||||
await config.integration.read({
|
||||
json: JSON.stringify(doc),
|
||||
})
|
||||
|
||||
expect(config.integration.client.allDocs).toHaveBeenCalledWith({
|
||||
include_docs: true,
|
||||
name: "search",
|
||||
couchdb = new CouchDBIntegration({
|
||||
url: env.COUCH_DB_URL,
|
||||
database: couchSafeID(),
|
||||
})
|
||||
})
|
||||
|
||||
it("calls the update method with the correct params", async () => {
|
||||
const doc = {
|
||||
_id: "1234",
|
||||
name: "search",
|
||||
}
|
||||
|
||||
await config.integration.update({
|
||||
json: JSON.stringify(doc),
|
||||
})
|
||||
|
||||
expect(config.integration.client.put).toHaveBeenCalledWith({
|
||||
...doc,
|
||||
_rev: "a",
|
||||
})
|
||||
it("successfully connects", async () => {
|
||||
const { connected } = await couchdb.testConnection()
|
||||
expect(connected).toBe(true)
|
||||
})
|
||||
|
||||
it("calls the delete method with the correct params", async () => {
|
||||
const id = "1234"
|
||||
await config.integration.delete({ id })
|
||||
expect(config.integration.client.remove).toHaveBeenCalledWith(id)
|
||||
it("can create documents", async () => {
|
||||
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||
expect(id).toBeDefined()
|
||||
expect(ok).toBe(true)
|
||||
expect(rev).toBeDefined()
|
||||
})
|
||||
|
||||
it("can read created documents", async () => {
|
||||
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||
expect(id).toBeDefined()
|
||||
expect(ok).toBe(true)
|
||||
expect(rev).toBeDefined()
|
||||
|
||||
const docs = await couchdb.read(query())
|
||||
expect(docs).toEqual([
|
||||
{
|
||||
_id: id,
|
||||
_rev: rev,
|
||||
test: 1,
|
||||
createdAt: expect.any(String),
|
||||
updatedAt: expect.any(String),
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("can update documents", async () => {
|
||||
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||
expect(ok).toBe(true)
|
||||
|
||||
const { id: newId, rev: newRev } = await couchdb.update(
|
||||
query({ _id: id, _rev: rev, test: 2 })
|
||||
)
|
||||
const docs = await couchdb.read(query())
|
||||
expect(docs).toEqual([
|
||||
{
|
||||
_id: newId,
|
||||
_rev: newRev,
|
||||
test: 2,
|
||||
createdAt: expect.any(String),
|
||||
updatedAt: expect.any(String),
|
||||
},
|
||||
])
|
||||
})
|
||||
|
||||
it("can delete documents", async () => {
|
||||
const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
|
||||
expect(ok).toBe(true)
|
||||
|
||||
const deleteResponse = await couchdb.delete({ id, rev })
|
||||
expect(deleteResponse.ok).toBe(true)
|
||||
|
||||
const docs = await couchdb.read(query())
|
||||
expect(docs).toBeEmpty()
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue