Implement and test create

This commit is contained in:
Adria Navarro 2023-07-12 16:13:00 +02:00
parent 81e847daeb
commit 899c8a14fb
9 changed files with 126 additions and 6 deletions

View File

@ -70,6 +70,7 @@ export enum DocumentType {
USER_FLAG = "flag",
AUTOMATION_METADATA = "meta_au",
AUDIT_LOG = "al",
VIEW = "vi",
}
export const StaticDatabases = {

View File

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

View File

@ -0,0 +1,74 @@
import * as setup from "./utilities"
import { FieldType, Table, ViewV2 } from "@budibase/types"
import { generator } from "@budibase/backend-core/tests"
import sdk from "../../../sdk"
import { context } from "@budibase/backend-core"
function priceTable(): Table {
return {
name: "table",
type: "table",
schema: {
Price: {
type: FieldType.NUMBER,
name: "Price",
constraints: {},
},
Category: {
type: FieldType.STRING,
name: "Category",
constraints: {
type: "string",
},
},
},
}
}
describe("/views/v2", () => {
const request = setup.getRequest()
const config = setup.getConfig()
let table: Table
afterAll(setup.afterAll)
beforeAll(async () => {
await config.init()
})
beforeEach(async () => {
table = await config.createTable(priceTable())
})
const saveView = async (view: ViewV2) => {
return request
.post(`/api/views/v2`)
.send(view)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
}
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
const view = {
name: generator.guid(),
tableId: table._id!,
}
const res = await saveView(view)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
await context.doInAppContext(config.appId, async () => {
const persisted = await sdk.views.get(res.body._id)
expect(persisted).toEqual({
_id: res.body._id,
_rev: res.body._rev,
...view,
createdAt: expect.any(String),
updatedAt: expect.any(String),
})
})
})
})
})

View File

@ -13,6 +13,11 @@ router
authorized(permissions.BUILDER),
viewController.v2.fetch
)
.post(
"/api/views/v2",
authorized(permissions.BUILDER),
viewController.v2.save
)
router
.get(

View File

@ -271,3 +271,11 @@ export function getMultiIDParams(ids: string[]) {
include_docs: true,
}
}
/**
* 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()}`
}

View File

@ -0,0 +1,25 @@
import { context } from "@budibase/backend-core"
import { ViewV2 } from "@budibase/types"
import { generateViewID } from "../../../db/utils"
export async function get(viewId: string) {
const db = context.getAppDB()
const result = await db.get(viewId)
return result
}
export async function save(view: ViewV2) {
const db = context.getAppDB()
const response = await db.put(
{
_id: generateViewID(),
...view,
},
{}
)
return {
_id: response.id,
_rev: response.rev,
}
}

View File

@ -7,7 +7,7 @@ import { default as queries } from "./app/queries"
import { default as rows } from "./app/rows"
import { default as users } from "./users"
import { default as plugins } from "./plugins"
import * as views from "./views"
import * as views from "./app/views"
const sdk = {
backups,

View File

@ -1,3 +0,0 @@
export async function get() {
return []
}

View File

@ -10,6 +10,11 @@ export interface View {
meta?: Record<string, any>
}
export interface ViewV2 {
name: string
tableId: string
}
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
export interface ViewCountOrSumSchema {