Fetch schema

This commit is contained in:
Adria Navarro 2023-07-21 12:30:21 +02:00
parent 30138570d5
commit 22dd218b1a
4 changed files with 77 additions and 4 deletions

View File

@ -11,6 +11,7 @@ import { FetchTablesResponse, Table, UserCtx } from "@budibase/types"
import sdk from "../../../sdk" import sdk from "../../../sdk"
import { jsonFromCsvString } from "../../../utilities/csv" import { jsonFromCsvString } from "../../../utilities/csv"
import { builderSocket } from "../../../websockets" import { builderSocket } from "../../../websockets"
import _ from "lodash"
function pickApi({ tableId, table }: { tableId?: string; table?: Table }) { function pickApi({ tableId, table }: { tableId?: string; table?: Table }) {
if (table && !tableId) { if (table && !tableId) {
@ -44,7 +45,22 @@ export async function fetch(ctx: UserCtx<void, FetchTablesResponse>) {
} }
}) })
ctx.body = [...internal, ...external] const tables = [...internal, ...external]
for (const t of tables.filter(t => t.views)) {
for (const [viewName, view] of Object.entries(t.views!)) {
if (sdk.views.isV2(view)) {
t.views![viewName] = {
...view,
schema: !view?.columns?.length
? t.schema
: _.pick(t.schema, ...view.columns),
}
}
}
}
ctx.body = tables as FetchTablesResponse
} }
export async function find(ctx: UserCtx) { export async function find(ctx: UserCtx) {

View File

@ -228,13 +228,49 @@ describe("/tables", () => {
expect.objectContaining({ expect.objectContaining({
_id: tableId, _id: tableId,
views: views.reduce((p, c) => { views: views.reduce((p, c) => {
p[c.name] = c p[c.name] = { ...c, schema: expect.anything() }
return p return p
}, {} as any), }, {} as any),
}), }),
]) ])
) )
}) })
it("should fetch the default schema if not overriden", async () => {
const tableId = config.table!._id!
const view = await config.api.viewV2.create({ tableId })
const res = await config.api.table.fetch()
expect(res).toEqual(
expect.arrayContaining([
expect.objectContaining({
_id: tableId,
views: {
[view.name]: {
...view,
schema: {
name: {
type: "string",
name: "name",
constraints: {
type: "string",
},
},
description: {
type: "string",
name: "description",
constraints: {
type: "string",
},
},
},
},
},
}),
])
)
})
}) })
describe("indexing", () => { describe("indexing", () => {

View File

@ -7,6 +7,17 @@ export class TableAPI extends TestAPI {
super(config) super(config)
} }
fetch = async (
{ expectStatus } = { expectStatus: 200 }
): Promise<Table[]> => {
const res = await this.request
.get(`/api/tables`)
.set(this.config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(expectStatus)
return res.body
}
get = async ( get = async (
tableId: string, tableId: string,
{ expectStatus } = { expectStatus: 200 } { expectStatus } = { expectStatus: 200 }

View File

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