Merge branch 'master' of github.com:Budibase/budibase into labday/sqs

This commit is contained in:
mike12345567 2024-04-08 16:34:31 +01:00
commit 0afb697653
10 changed files with 81 additions and 63 deletions

View File

@ -6,6 +6,7 @@ import {
UIFieldMetadata, UIFieldMetadata,
UpdateViewRequest, UpdateViewRequest,
ViewResponse, ViewResponse,
ViewResponseEnriched,
ViewV2, ViewV2,
} from "@budibase/types" } from "@budibase/types"
import { builderSocket, gridSocket } from "../../../websockets" import { builderSocket, gridSocket } from "../../../websockets"
@ -39,9 +40,9 @@ async function parseSchema(view: CreateViewRequest) {
return finalViewSchema return finalViewSchema
} }
export async function get(ctx: Ctx<void, ViewResponse>) { export async function get(ctx: Ctx<void, ViewResponseEnriched>) {
ctx.body = { ctx.body = {
data: await sdk.views.get(ctx.params.viewId, { enriched: true }), data: await sdk.views.getEnriched(ctx.params.viewId),
} }
} }

View File

@ -181,7 +181,7 @@ describe.each([
const createdView = await config.api.viewV2.create(newView) const createdView = await config.api.viewV2.create(newView)
expect(await config.api.viewV2.get(createdView.id)).toEqual({ expect(createdView).toEqual({
...newView, ...newView,
schema: { schema: {
Price: { Price: {
@ -398,7 +398,7 @@ describe.each([
}) })
it("updates only UI schema overrides", async () => { it("updates only UI schema overrides", async () => {
await config.api.viewV2.update({ const updatedView = await config.api.viewV2.update({
...view, ...view,
schema: { schema: {
Price: { Price: {
@ -417,7 +417,7 @@ describe.each([
} as Record<string, FieldSchema>, } as Record<string, FieldSchema>,
}) })
expect(await config.api.viewV2.get(view.id)).toEqual({ expect(updatedView).toEqual({
...view, ...view,
schema: { schema: {
Price: { Price: {
@ -479,17 +479,17 @@ describe.each([
describe("fetch view (through table)", () => { describe("fetch view (through table)", () => {
it("should be able to fetch a view V2", async () => { it("should be able to fetch a view V2", async () => {
const newView: CreateViewRequest = { const res = await config.api.viewV2.create({
name: generator.name(), name: generator.name(),
tableId: table._id!, tableId: table._id!,
schema: { schema: {
Price: { visible: false }, Price: { visible: false },
Category: { visible: true }, Category: { visible: true },
}, },
} })
const res = await config.api.viewV2.create(newView) expect(res.schema?.Price).toBeUndefined()
const view = await config.api.viewV2.get(res.id) const view = await config.api.viewV2.get(res.id)
expect(view!.schema?.Price).toBeUndefined()
const updatedTable = await config.api.table.get(table._id!) const updatedTable = await config.api.table.get(table._id!)
const viewSchema = updatedTable.views![view!.name!].schema as Record< const viewSchema = updatedTable.views![view!.name!].schema as Record<
string, string,

View File

@ -142,7 +142,9 @@ export function enrichViewSchemas(table: Table): TableResponse {
return { return {
...table, ...table,
views: Object.values(table.views ?? []) views: Object.values(table.views ?? [])
.map(v => sdk.views.enrichSchema(v, table.schema)) .map(v =>
sdk.views.isV2(v) ? sdk.views.enrichSchema(v, table.schema) : v
)
.reduce((p, v) => { .reduce((p, v) => {
p[v.name!] = v p[v.name!] = v
return p return p

View File

@ -1,4 +1,4 @@
import { ViewV2 } from "@budibase/types" import { ViewV2, ViewV2Enriched } from "@budibase/types"
import { context, HTTPError } from "@budibase/backend-core" import { context, HTTPError } from "@budibase/backend-core"
import sdk from "../../../sdk" import sdk from "../../../sdk"
@ -6,26 +6,34 @@ import * as utils from "../../../db/utils"
import { enrichSchema, isV2 } from "." import { enrichSchema, isV2 } from "."
import { breakExternalTableId } from "../../../integrations/utils" import { breakExternalTableId } from "../../../integrations/utils"
export async function get( export async function get(viewId: string): Promise<ViewV2> {
viewId: string,
opts?: { enriched: boolean }
): Promise<ViewV2> {
const { tableId } = utils.extractViewInfoFromID(viewId) const { tableId } = utils.extractViewInfoFromID(viewId)
const { datasourceId, tableName } = breakExternalTableId(tableId) const { datasourceId, tableName } = breakExternalTableId(tableId)
const ds = await sdk.datasources.get(datasourceId!) const ds = await sdk.datasources.get(datasourceId!)
const table = ds.entities![tableName!] const table = ds.entities![tableName!]
const views = Object.values(table.views!) const views = Object.values(table.views!).filter(isV2)
const found = views.find(v => isV2(v) && v.id === viewId) const found = views.find(v => v.id === viewId)
if (!found) { if (!found) {
throw new Error("No view found") throw new Error("No view found")
} }
if (opts?.enriched) { return found
return enrichSchema(found, table.schema) as ViewV2
} else {
return found as ViewV2
} }
export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
const { tableId } = utils.extractViewInfoFromID(viewId)
const { datasourceId, tableName } = breakExternalTableId(tableId)
const ds = await sdk.datasources.get(datasourceId!)
const table = ds.entities![tableName!]
const views = Object.values(table.views!).filter(isV2)
const found = views.find(v => v.id === viewId)
if (!found) {
throw new Error("No view found")
}
return enrichSchema(found, table.schema)
} }
export async function create( export async function create(

View File

@ -1,8 +1,13 @@
import { RenameColumn, TableSchema, View, ViewV2 } from "@budibase/types" import {
RenameColumn,
TableSchema,
View,
ViewV2,
ViewV2Enriched,
} from "@budibase/types"
import { db as dbCore } from "@budibase/backend-core" import { db as dbCore } from "@budibase/backend-core"
import { cloneDeep } from "lodash" import { cloneDeep } from "lodash"
import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
import { isExternalTableID } from "../../../integrations/utils" import { isExternalTableID } from "../../../integrations/utils"
@ -16,12 +21,14 @@ function pickApi(tableId: any) {
return internal return internal
} }
export async function get( export async function get(viewId: string): Promise<ViewV2> {
viewId: string,
opts?: { enriched: boolean }
): Promise<ViewV2> {
const { tableId } = utils.extractViewInfoFromID(viewId) const { tableId } = utils.extractViewInfoFromID(viewId)
return pickApi(tableId).get(viewId, opts) return pickApi(tableId).get(viewId)
}
export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
const { tableId } = utils.extractViewInfoFromID(viewId)
return pickApi(tableId).getEnriched(viewId)
} }
export async function create( export async function create(
@ -52,11 +59,10 @@ export function allowedFields(view: View | ViewV2) {
] ]
} }
export function enrichSchema(view: View | ViewV2, tableSchema: TableSchema) { export function enrichSchema(
if (!sdk.views.isV2(view)) { view: ViewV2,
return view tableSchema: TableSchema
} ): ViewV2Enriched {
let schema = cloneDeep(tableSchema) let schema = cloneDeep(tableSchema)
const anyViewOrder = Object.values(view.schema || {}).some( const anyViewOrder = Object.values(view.schema || {}).some(
ui => ui.order != null ui => ui.order != null

View File

@ -1,26 +1,30 @@
import { ViewV2 } from "@budibase/types" import { ViewV2, ViewV2Enriched } from "@budibase/types"
import { context, HTTPError } from "@budibase/backend-core" import { context, HTTPError } from "@budibase/backend-core"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import * as utils from "../../../db/utils" import * as utils from "../../../db/utils"
import { enrichSchema, isV2 } from "." import { enrichSchema, isV2 } from "."
export async function get( export async function get(viewId: string): Promise<ViewV2> {
viewId: string,
opts?: { enriched: boolean }
): Promise<ViewV2> {
const { tableId } = utils.extractViewInfoFromID(viewId) const { tableId } = utils.extractViewInfoFromID(viewId)
const table = await sdk.tables.getTable(tableId) const table = await sdk.tables.getTable(tableId)
const views = Object.values(table.views!) const views = Object.values(table.views!).filter(isV2)
const found = views.find(v => isV2(v) && v.id === viewId) const found = views.find(v => v.id === viewId)
if (!found) { if (!found) {
throw new Error("No view found") throw new Error("No view found")
} }
if (opts?.enriched) { return found
return enrichSchema(found, table.schema) as ViewV2
} else {
return found as ViewV2
} }
export async function getEnriched(viewId: string): Promise<ViewV2Enriched> {
const { tableId } = utils.extractViewInfoFromID(viewId)
const table = await sdk.tables.getTable(tableId)
const views = Object.values(table.views!).filter(isV2)
const found = views.find(v => v.id === viewId)
if (!found) {
throw new Error("No view found")
}
return enrichSchema(found, table.schema)
} }
export async function create( export async function create(

View File

@ -4,9 +4,9 @@ import {
ViewV2, ViewV2,
SearchViewRowRequest, SearchViewRowRequest,
PaginatedSearchRowResponse, PaginatedSearchRowResponse,
ViewResponseEnriched,
} from "@budibase/types" } from "@budibase/types"
import { Expectations, TestAPI } from "./base" import { Expectations, TestAPI } from "./base"
import sdk from "../../../sdk"
export class ViewV2API extends TestAPI { export class ViewV2API extends TestAPI {
create = async ( create = async (
@ -45,9 +45,8 @@ export class ViewV2API extends TestAPI {
} }
get = async (viewId: string) => { get = async (viewId: string) => {
return await this.config.doInContext(this.config.appId, () => return (await this._get<ViewResponseEnriched>(`/api/v2/views/${viewId}`))
sdk.views.get(viewId) .data
)
} }
search = async ( search = async (

View File

@ -3,16 +3,11 @@ import {
Row, Row,
Table, Table,
TableRequest, TableRequest,
TableSchema,
View, View,
ViewV2, ViewV2Enriched,
} from "../../../documents" } from "../../../documents"
interface ViewV2Response extends ViewV2 { export type TableViewsResponse = { [key: string]: View | ViewV2Enriched }
schema: TableSchema
}
export type TableViewsResponse = { [key: string]: View | ViewV2Response }
export interface TableResponse extends Table { export interface TableResponse extends Table {
views?: TableViewsResponse views?: TableViewsResponse

View File

@ -1,14 +1,13 @@
import { ViewV2, UIFieldMetadata } from "../../../documents" import { ViewV2, ViewV2Enriched } from "../../../documents"
export interface ViewResponse { export interface ViewResponse {
data: ViewV2 data: ViewV2
} }
export interface CreateViewRequest export interface ViewResponseEnriched {
extends Omit<ViewV2, "version" | "id" | "schema"> { data: ViewV2Enriched
schema?: Record<string, UIFieldMetadata>
} }
export interface UpdateViewRequest extends Omit<ViewV2, "schema"> { export interface CreateViewRequest extends Omit<ViewV2, "version" | "id"> {}
schema?: Record<string, UIFieldMetadata>
} export interface UpdateViewRequest extends ViewV2 {}

View File

@ -1,5 +1,5 @@
import { SearchFilter, SortOrder, SortType } from "../../api" import { SearchFilter, SortOrder, SortType } from "../../api"
import { UIFieldMetadata } from "./table" import { TableSchema, UIFieldMetadata } from "./table"
import { Document } from "../document" import { Document } from "../document"
import { DBView } from "../../sdk" import { DBView } from "../../sdk"
@ -48,6 +48,10 @@ export interface ViewV2 {
schema?: Record<string, UIFieldMetadata> schema?: Record<string, UIFieldMetadata>
} }
export interface ViewV2Enriched extends ViewV2 {
schema?: TableSchema
}
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
export interface ViewCountOrSumSchema { export interface ViewCountOrSumSchema {