Find by table

This commit is contained in:
Adria Navarro 2023-07-12 17:02:54 +02:00
parent f395b79cac
commit e2c1a549de
5 changed files with 61 additions and 12 deletions

View File

@ -1,3 +1,4 @@
import { DocumentType, SEPARATOR } from "@budibase/backend-core"
import sdk from "../../../sdk"
import { Ctx, ViewV2 } from "@budibase/types"
@ -5,6 +6,12 @@ export async function fetch(ctx: Ctx) {
ctx.body = { views: await sdk.views.fetch() }
}
export async function findByTable(ctx: Ctx) {
const tableId = `${DocumentType.TABLE}${SEPARATOR}${ctx.params.tableId}`
console.error(tableId)
ctx.body = { views: await sdk.views.findByTable(tableId) }
}
export async function save(ctx: Ctx<ViewV2>) {
const view = ctx.request.body
const result = await sdk.views.save(view)

View File

@ -33,9 +33,6 @@ describe("/views/v2", () => {
beforeAll(async () => {
await config.init()
})
beforeEach(async () => {
table = await config.createTable(priceTable())
})
@ -48,10 +45,10 @@ describe("/views/v2", () => {
.expect(200)
}
function createView(): ViewV2 {
function createView(tableId: string): ViewV2 {
return {
name: generator.guid(),
tableId: table._id!,
tableId,
}
}
@ -61,7 +58,7 @@ describe("/views/v2", () => {
beforeAll(async () => {
table = await config.createTable(priceTable())
for (let id = 0; id < 10; id++) {
const res = await saveView(createView())
const res = await saveView(createView(table._id!))
views.push(res.body)
}
})
@ -80,9 +77,32 @@ describe("/views/v2", () => {
})
})
describe("findByTable", () => {
const views: any[] = []
beforeAll(async () => {
table = await config.createTable(priceTable())
for (let id = 0; id < 5; id++) {
const res = await saveView(createView(table._id!))
views.push(res.body)
}
})
it("returns all views", async () => {
const res = await request
.get(`/api/views/v2/${table._id}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.views.length).toBe(5)
expect(res.body.views).toEqual(expect.arrayContaining([]))
})
})
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
const view = createView()
const view = createView(table._id!)
const res = await saveView(view)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()

View File

@ -3,7 +3,7 @@ import * as viewController from "../controllers/view"
import * as rowController from "../controllers/row"
import authorized from "../../middleware/authorized"
import { paramResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core"
import { DocumentType, SEPARATOR, permissions } from "@budibase/backend-core"
const router: Router = new Router()
@ -13,6 +13,11 @@ router
authorized(permissions.BUILDER),
viewController.v2.fetch
)
.get(
`/api/views/v2/${DocumentType.TABLE}${SEPARATOR}:tableId`,
authorized(permissions.BUILDER),
viewController.v2.findByTable
)
.post(
"/api/views/v2",
authorized(permissions.BUILDER),

View File

@ -276,6 +276,10 @@ export function getMultiIDParams(ids: string[]) {
* Generates a new view ID.
* @returns {string} The new view ID which the view doc can be stored under.
*/
export function generateViewID(): string {
return `${DocumentType.VIEW}${SEPARATOR}${newid()}`
export function generateViewID(tableId: string) {
return `${viewIDPrefix(tableId)}${newid()}`
}
export function viewIDPrefix(tableId: string) {
return `${DocumentType.VIEW}${SEPARATOR}${tableId}${SEPARATOR}`
}

View File

@ -5,7 +5,7 @@ import {
context,
} from "@budibase/backend-core"
import { ViewV2 } from "@budibase/types"
import { generateViewID } from "../../../db/utils"
import * as utils from "../../../db/utils"
export async function fetch() {
const db = context.getAppDB()
@ -20,6 +20,19 @@ export async function fetch() {
return response.rows.map(r => r.doc)
}
export async function findByTable(tableId: string) {
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) {
const db = context.getAppDB()
const result = await db.get(viewId)
@ -31,7 +44,7 @@ export async function save(view: ViewV2) {
const response = await db.put(
{
_id: generateViewID(),
_id: utils.generateViewID(view.tableId),
...view,
},
{}