1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00

Implement and test fetch

This commit is contained in:
Adria Navarro 2023-07-12 16:33:06 +02:00
parent 899c8a14fb
commit 4a5a3e2c33
3 changed files with 54 additions and 6 deletions

View file

@ -2,7 +2,7 @@ import sdk from "../../../sdk"
import { Ctx, ViewV2 } from "@budibase/types"
export async function fetch(ctx: Ctx) {
ctx.body = await sdk.views.get(ctx.params.viewId)
ctx.body = { views: await sdk.views.fetch() }
}
export async function save(ctx: Ctx<ViewV2>) {

View file

@ -49,12 +49,43 @@ describe("/views/v2", () => {
.expect(200)
}
function createView(): ViewV2 {
return {
name: generator.guid(),
tableId: table._id!,
}
}
describe("fetch", () => {
const views = []
beforeAll(async () => {
table = await config.createTable(priceTable())
for (let id = 0; id < 10; id++) {
const view = createView()
const res = await saveView(view)
await context.doInAppContext(config.appId, async () => {
views.push(await sdk.views.get(res.body._id))
})
}
})
it("returns all views", async () => {
const res = await request
.get(`/api/views/v2`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.views.length).toBe(10)
expect(res.body.views).toEqual(expect.arrayContaining([]))
})
})
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
const view = {
name: generator.guid(),
tableId: table._id!,
}
const view = createView()
const res = await saveView(view)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()

View file

@ -1,7 +1,24 @@
import { context } from "@budibase/backend-core"
import {
DocumentType,
SEPARATOR,
UNICODE_MAX,
context,
} from "@budibase/backend-core"
import { ViewV2 } from "@budibase/types"
import { generateViewID } from "../../../db/utils"
export async function fetch() {
const db = context.getAppDB()
const startKey = `${DocumentType.VIEW}${SEPARATOR}`
const response = await db.allDocs({
startkey: startKey,
endkey: `${startKey}${UNICODE_MAX}`,
})
return response.rows
}
export async function get(viewId: string) {
const db = context.getAppDB()
const result = await db.get(viewId)