Implement and test fetch
This commit is contained in:
parent
899c8a14fb
commit
4a5a3e2c33
3 changed files with 54 additions and 6 deletions
|
@ -2,7 +2,7 @@ import sdk from "../../../sdk"
|
||||||
import { Ctx, ViewV2 } from "@budibase/types"
|
import { Ctx, ViewV2 } from "@budibase/types"
|
||||||
|
|
||||||
export async function fetch(ctx: Ctx) {
|
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>) {
|
export async function save(ctx: Ctx<ViewV2>) {
|
||||||
|
|
|
@ -49,12 +49,43 @@ describe("/views/v2", () => {
|
||||||
.expect(200)
|
.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", () => {
|
describe("create", () => {
|
||||||
it("persist the view when the view is successfully created", async () => {
|
it("persist the view when the view is successfully created", async () => {
|
||||||
const view = {
|
const view = createView()
|
||||||
name: generator.guid(),
|
|
||||||
tableId: table._id!,
|
|
||||||
}
|
|
||||||
const res = await saveView(view)
|
const res = await saveView(view)
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body._id).toBeDefined()
|
expect(res.body._id).toBeDefined()
|
||||||
|
|
|
@ -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 { ViewV2 } from "@budibase/types"
|
||||||
import { generateViewID } from "../../../db/utils"
|
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) {
|
export async function get(viewId: string) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
const result = await db.get(viewId)
|
const result = await db.get(viewId)
|
||||||
|
|
Loading…
Reference in a new issue