Merge branch 'master' into dd-automations

This commit is contained in:
Sam Rose 2025-02-19 16:56:39 +00:00 committed by GitHub
commit 39e46bd961
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 84 additions and 80 deletions

View File

@ -5,10 +5,10 @@ import {
SqlQuery, SqlQuery,
Table, Table,
TableSourceType, TableSourceType,
SEPARATOR,
} from "@budibase/types" } from "@budibase/types"
import { DEFAULT_BB_DATASOURCE_ID } from "../constants" import { DEFAULT_BB_DATASOURCE_ID } from "../constants"
import { Knex } from "knex" import { Knex } from "knex"
import { SEPARATOR } from "../db"
import environment from "../environment" import environment from "../environment"
const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}` const DOUBLE_SEPARATOR = `${SEPARATOR}${SEPARATOR}`

View File

@ -62,12 +62,16 @@ const SCHEMA: Integration = {
type: DatasourceFieldType.STRING, type: DatasourceFieldType.STRING,
required: true, required: true,
}, },
rev: {
type: DatasourceFieldType.STRING,
required: true,
},
}, },
}, },
}, },
} }
class CouchDBIntegration implements IntegrationBase { export class CouchDBIntegration implements IntegrationBase {
private readonly client: Database private readonly client: Database
constructor(config: CouchDBConfig) { constructor(config: CouchDBConfig) {
@ -82,7 +86,8 @@ class CouchDBIntegration implements IntegrationBase {
connected: false, connected: false,
} }
try { try {
response.connected = await this.client.exists() await this.client.allDocs({ limit: 1 })
response.connected = true
} catch (e: any) { } catch (e: any) {
response.error = e.message as string response.error = e.message as string
} }
@ -99,13 +104,9 @@ class CouchDBIntegration implements IntegrationBase {
} }
async read(query: { json: string | object }) { async read(query: { json: string | object }) {
const parsed = this.parse(query) const params = { include_docs: true, ...this.parse(query) }
const params = {
include_docs: true,
...parsed,
}
const result = await this.client.allDocs(params) 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 }) { async update(query: { json: string | object }) {
@ -121,8 +122,8 @@ class CouchDBIntegration implements IntegrationBase {
return await this.client.get(query.id) return await this.client.get(query.id)
} }
async delete(query: { id: string }) { async delete(query: { id: string; rev: string }) {
return await this.client.remove(query.id) return await this.client.remove(query.id, query.rev)
} }
} }

View File

@ -1,84 +1,87 @@
jest.mock("@budibase/backend-core", () => { import { env } from "@budibase/backend-core"
const core = jest.requireActual("@budibase/backend-core") import { CouchDBIntegration } from "../couchdb"
return { import { generator } from "@budibase/backend-core/tests"
...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 { 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 { function doc(data: Record<string, any>): string {
integration: any return JSON.stringify({ _id: couchSafeID(), ...data })
}
constructor( function query(data?: Record<string, any>): { json: string } {
config: any = { url: "http://somewhere", database: "something" } return { json: doc(data || {}) }
) {
this.integration = new CouchDBIntegration.integration(config)
}
} }
describe("CouchDB Integration", () => { describe("CouchDB Integration", () => {
let config: any let couchdb: CouchDBIntegration
beforeEach(() => { beforeEach(() => {
config = new TestConfiguration() couchdb = new CouchDBIntegration({
}) url: env.COUCH_DB_URL,
database: couchSafeID(),
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",
}) })
}) })
it("calls the update method with the correct params", async () => { it("successfully connects", async () => {
const doc = { const { connected } = await couchdb.testConnection()
_id: "1234", expect(connected).toBe(true)
name: "search",
}
await config.integration.update({
json: JSON.stringify(doc),
})
expect(config.integration.client.put).toHaveBeenCalledWith({
...doc,
_rev: "a",
})
}) })
it("calls the delete method with the correct params", async () => { it("can create documents", async () => {
const id = "1234" const { id, ok, rev } = await couchdb.create(query({ test: 1 }))
await config.integration.delete({ id }) expect(id).toBeDefined()
expect(config.integration.client.remove).toHaveBeenCalledWith(id) 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()
}) })
}) })