Remove view fetch logic
This commit is contained in:
parent
49980dadf1
commit
b82876b147
|
@ -1,24 +1,5 @@
|
||||||
import sdk from "../../../sdk"
|
import sdk from "../../../sdk"
|
||||||
import {
|
import { CreateViewRequest, Ctx, ViewResponse } from "@budibase/types"
|
||||||
CreateViewRequest,
|
|
||||||
Ctx,
|
|
||||||
FetchViewResponse,
|
|
||||||
ViewResponse,
|
|
||||||
} from "@budibase/types"
|
|
||||||
|
|
||||||
export async function fetch(ctx: Ctx<void, FetchViewResponse>) {
|
|
||||||
const { tableId } = ctx.query
|
|
||||||
|
|
||||||
if (tableId && typeof tableId !== "string") {
|
|
||||||
ctx.throw(400, "tableId type is not valid")
|
|
||||||
}
|
|
||||||
|
|
||||||
const views = tableId
|
|
||||||
? await sdk.views.findByTable(tableId)
|
|
||||||
: await sdk.views.fetch()
|
|
||||||
|
|
||||||
ctx.body = { views }
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function find(ctx: Ctx<void, ViewResponse>) {
|
export async function find(ctx: Ctx<void, ViewResponse>) {
|
||||||
const { viewId } = ctx.params
|
const { viewId } = ctx.params
|
||||||
|
|
|
@ -43,72 +43,6 @@ describe("/v2/views", () => {
|
||||||
await config.createTable(priceTable())
|
await config.createTable(priceTable())
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("fetch", () => {
|
|
||||||
const views: ViewV2[] = []
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
await config.createTable(priceTable())
|
|
||||||
for (let id = 0; id < 10; id++) {
|
|
||||||
views.push(await config.api.viewV2.create())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns all views", async () => {
|
|
||||||
const res = await config.api.viewV2.fetch()
|
|
||||||
|
|
||||||
expect(res.body.views.length).toBe(10)
|
|
||||||
expect(res.body.views).toEqual(
|
|
||||||
expect.arrayContaining(views.map(v => expect.objectContaining(v)))
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("can filter by table id", async () => {
|
|
||||||
const newTable = await config.createTable(priceTable(), {
|
|
||||||
skipReassigning: true,
|
|
||||||
})
|
|
||||||
const newViews = []
|
|
||||||
for (let id = 0; id < 5; id++) {
|
|
||||||
newViews.push(await config.api.viewV2.create({ tableId: newTable._id }))
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await config.api.viewV2.fetch(newTable._id)
|
|
||||||
|
|
||||||
expect(res.body.views.length).toBe(5)
|
|
||||||
expect(res.body.views).toEqual(
|
|
||||||
expect.arrayContaining(newViews.map(v => expect.objectContaining(v)))
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("can not filter by multiple table ids", async () => {
|
|
||||||
const res = await config
|
|
||||||
.getRequest()!
|
|
||||||
.get(
|
|
||||||
`/api/v2/views?tableId=${structures.generator.guid()}&tableId=${structures.generator.guid()}`
|
|
||||||
)
|
|
||||||
.set(config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(400)
|
|
||||||
|
|
||||||
expect(res.body.message).toBe("tableId type is not valid")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("returns views with query info", async () => {
|
|
||||||
const newView = await config.api.viewV2.create({ ...viewFilters })
|
|
||||||
const res = await config.api.viewV2.fetch(config.table!._id)
|
|
||||||
|
|
||||||
expect(res.body.views.length).toBe(11)
|
|
||||||
expect(newView.query).toEqual({ allOr: false, equal: { field: "value" } })
|
|
||||||
expect(res.body.views).toEqual(
|
|
||||||
expect.arrayContaining([
|
|
||||||
expect.objectContaining({
|
|
||||||
...newView,
|
|
||||||
...viewFilters,
|
|
||||||
}),
|
|
||||||
])
|
|
||||||
)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
describe("getView", () => {
|
describe("getView", () => {
|
||||||
let view: ViewV2
|
let view: ViewV2
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
|
|
@ -8,11 +8,6 @@ import { DocumentType, SEPARATOR, permissions } from "@budibase/backend-core"
|
||||||
const router: Router = new Router()
|
const router: Router = new Router()
|
||||||
|
|
||||||
router
|
router
|
||||||
.get(
|
|
||||||
"/api/v2/views",
|
|
||||||
authorized(permissions.BUILDER),
|
|
||||||
viewController.v2.fetch
|
|
||||||
)
|
|
||||||
.get(
|
.get(
|
||||||
`/api/v2/views/:viewId`,
|
`/api/v2/views/:viewId`,
|
||||||
authorized(permissions.BUILDER),
|
authorized(permissions.BUILDER),
|
||||||
|
|
|
@ -1,38 +1,7 @@
|
||||||
import {
|
import { context } from "@budibase/backend-core"
|
||||||
DocumentType,
|
|
||||||
SEPARATOR,
|
|
||||||
UNICODE_MAX,
|
|
||||||
context,
|
|
||||||
} from "@budibase/backend-core"
|
|
||||||
import { ViewV2 } from "@budibase/types"
|
import { ViewV2 } from "@budibase/types"
|
||||||
import * as utils from "../../../db/utils"
|
import * as utils from "../../../db/utils"
|
||||||
|
|
||||||
export async function fetch(): Promise<ViewV2[]> {
|
|
||||||
const db = context.getAppDB()
|
|
||||||
|
|
||||||
const startKey = `${DocumentType.VIEW}${SEPARATOR}`
|
|
||||||
const response = await db.allDocs({
|
|
||||||
startkey: startKey,
|
|
||||||
endkey: `${startKey}${UNICODE_MAX}`,
|
|
||||||
include_docs: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
return response.rows.map(r => r.doc)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function findByTable(tableId: string): Promise<ViewV2[]> {
|
|
||||||
const db = context.getAppDB()
|
|
||||||
|
|
||||||
const startKey = utils.viewIDPrefix(tableId)
|
|
||||||
const response = await db.allDocs({
|
|
||||||
startkey: startKey,
|
|
||||||
endkey: `${startKey}${UNICODE_MAX}`,
|
|
||||||
include_docs: true,
|
|
||||||
})
|
|
||||||
|
|
||||||
return response.rows.map(r => r.doc)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function get(viewId: string): Promise<ViewV2 | undefined> {
|
export async function get(viewId: string): Promise<ViewV2 | undefined> {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -41,21 +41,6 @@ export class ViewV2API extends TestAPI {
|
||||||
.expect(expectStatus)
|
.expect(expectStatus)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetch = async (
|
|
||||||
tableId?: string,
|
|
||||||
{ expectStatus } = { expectStatus: 200 }
|
|
||||||
) => {
|
|
||||||
let url = `/api/v2/views?`
|
|
||||||
if (tableId) {
|
|
||||||
url += `tableId=${tableId}&`
|
|
||||||
}
|
|
||||||
return this.request
|
|
||||||
.get(url)
|
|
||||||
.set(this.config.defaultHeaders())
|
|
||||||
.expect("Content-Type", /json/)
|
|
||||||
.expect(expectStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
delete = async (viewId: string, { expectStatus } = { expectStatus: 204 }) => {
|
delete = async (viewId: string, { expectStatus } = { expectStatus: 204 }) => {
|
||||||
return this.request
|
return this.request
|
||||||
.delete(`/api/v2/views/${viewId}`)
|
.delete(`/api/v2/views/${viewId}`)
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
import { ViewV2 } from "../../../documents"
|
import { ViewV2 } from "../../../documents"
|
||||||
|
|
||||||
export interface FetchViewResponse {
|
|
||||||
views: ViewV2[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ViewResponse {
|
export interface ViewResponse {
|
||||||
data: ViewV2
|
data: ViewV2
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue