Merge branch 'master' of github.com:Budibase/budibase into v3-ui
This commit is contained in:
commit
edbb0b59f7
|
@ -23,6 +23,7 @@ jobs:
|
|||
PAYLOAD_BRANCH: ${{ github.head_ref }}
|
||||
PAYLOAD_PR_NUMBER: ${{ github.event.pull_request.number }}
|
||||
PAYLOAD_LICENSE_TYPE: "free"
|
||||
PAYLOAD_DEPLOY: "true"
|
||||
with:
|
||||
repository: budibase/budibase-deploys
|
||||
event: featurebranch-qa-deploy
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
|
||||
"version": "2.32.10",
|
||||
"version": "2.32.11",
|
||||
"npmClient": "yarn",
|
||||
"packages": [
|
||||
"packages/*",
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
export * as utils from "./utils"
|
||||
|
||||
export { default as Sql } from "./sql"
|
||||
export { default as Sql, COUNT_FIELD_NAME } from "./sql"
|
||||
export { default as SqlTable } from "./sqlTable"
|
||||
export * as designDoc from "./designDoc"
|
||||
|
|
|
@ -43,6 +43,8 @@ import { cloneDeep } from "lodash"
|
|||
|
||||
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
|
||||
|
||||
export const COUNT_FIELD_NAME = "__bb_total"
|
||||
|
||||
function getBaseLimit() {
|
||||
const envLimit = environment.SQL_MAX_ROWS
|
||||
? parseInt(environment.SQL_MAX_ROWS)
|
||||
|
@ -846,7 +848,7 @@ class InternalBuilder {
|
|||
throw new Error("SQL counting requires primary key to be supplied")
|
||||
}
|
||||
return query.countDistinct(
|
||||
`${this.getTableName()}.${this.table.primary[0]} as __bb_total`
|
||||
`${this.getTableName()}.${this.table.primary[0]} as ${COUNT_FIELD_NAME}`
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,7 @@
|
|||
// Look up the component tree and find something that is provided by an
|
||||
// ancestor that matches our datasource. This is for backwards compatibility
|
||||
// as previously we could use the "closest" context.
|
||||
for (let id of path.reverse().slice(1)) {
|
||||
for (let id of path.toReversed().slice(1)) {
|
||||
// Check for matching view datasource
|
||||
if (
|
||||
dataSource.type === "viewV2" &&
|
||||
|
|
|
@ -124,9 +124,11 @@ export async function buildSqlFieldList(
|
|||
([columnName, column]) =>
|
||||
column.type !== FieldType.LINK &&
|
||||
column.type !== FieldType.FORMULA &&
|
||||
!existing.find((field: string) => field === columnName)
|
||||
!existing.find(
|
||||
(field: string) => field === `${table.name}.${columnName}`
|
||||
)
|
||||
)
|
||||
.map(column => `${table.name}.${column[0]}`)
|
||||
.map(([columnName]) => `${table.name}.${columnName}`)
|
||||
}
|
||||
|
||||
let fields: string[] = []
|
||||
|
|
|
@ -695,6 +695,69 @@ describe.each([
|
|||
})
|
||||
})
|
||||
|
||||
describe("options column", () => {
|
||||
beforeAll(async () => {
|
||||
table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
status: {
|
||||
name: "status",
|
||||
type: FieldType.OPTIONS,
|
||||
default: "requested",
|
||||
constraints: {
|
||||
inclusion: ["requested", "approved"],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates a new row with a default value successfully", async () => {
|
||||
const row = await config.api.row.save(table._id!, {})
|
||||
expect(row.status).toEqual("requested")
|
||||
})
|
||||
|
||||
it("does not use default value if value specified", async () => {
|
||||
const row = await config.api.row.save(table._id!, {
|
||||
status: "approved",
|
||||
})
|
||||
expect(row.status).toEqual("approved")
|
||||
})
|
||||
})
|
||||
|
||||
describe("array column", () => {
|
||||
beforeAll(async () => {
|
||||
table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
food: {
|
||||
name: "food",
|
||||
type: FieldType.ARRAY,
|
||||
default: ["apple", "orange"],
|
||||
constraints: {
|
||||
type: JsonFieldSubType.ARRAY,
|
||||
inclusion: ["apple", "orange", "banana"],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
})
|
||||
|
||||
it("creates a new row with a default value successfully", async () => {
|
||||
const row = await config.api.row.save(table._id!, {})
|
||||
expect(row.food).toEqual(["apple", "orange"])
|
||||
})
|
||||
|
||||
it("does not use default value if value specified", async () => {
|
||||
const row = await config.api.row.save(table._id!, {
|
||||
food: ["orange"],
|
||||
})
|
||||
expect(row.food).toEqual(["orange"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("bindings", () => {
|
||||
describe("string column", () => {
|
||||
beforeAll(async () => {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -22,9 +22,10 @@ import {
|
|||
RelationshipType,
|
||||
TableSchema,
|
||||
RenameColumn,
|
||||
ViewFieldMetadata,
|
||||
FeatureFlag,
|
||||
BBReferenceFieldSubType,
|
||||
ViewV2Schema,
|
||||
ViewCalculationFieldMetadata,
|
||||
} from "@budibase/types"
|
||||
import { generator, mocks } from "@budibase/backend-core/tests"
|
||||
import { DatabaseName, getDatasource } from "../../../integrations/tests/utils"
|
||||
|
@ -540,6 +541,33 @@ describe.each([
|
|||
status: 201,
|
||||
})
|
||||
})
|
||||
|
||||
it("can create a view with calculation fields", async () => {
|
||||
let view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
calculationType: CalculationType.SUM,
|
||||
field: "Price",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
expect(Object.keys(view.schema!)).toHaveLength(1)
|
||||
|
||||
let sum = view.schema!.sum as ViewCalculationFieldMetadata
|
||||
expect(sum).toBeDefined()
|
||||
expect(sum.calculationType).toEqual(CalculationType.SUM)
|
||||
expect(sum.field).toEqual("Price")
|
||||
|
||||
view = await config.api.viewV2.get(view.id)
|
||||
sum = view.schema!.sum as ViewCalculationFieldMetadata
|
||||
expect(sum).toBeDefined()
|
||||
expect(sum.calculationType).toEqual(CalculationType.SUM)
|
||||
expect(sum.field).toEqual("Price")
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", () => {
|
||||
|
@ -1152,10 +1180,7 @@ describe.each([
|
|||
return table
|
||||
}
|
||||
|
||||
const createView = async (
|
||||
tableId: string,
|
||||
schema: Record<string, ViewFieldMetadata>
|
||||
) =>
|
||||
const createView = async (tableId: string, schema: ViewV2Schema) =>
|
||||
await config.api.viewV2.create({
|
||||
name: generator.guid(),
|
||||
tableId,
|
||||
|
@ -2546,6 +2571,51 @@ describe.each([
|
|||
}
|
||||
})
|
||||
})
|
||||
|
||||
!isLucene &&
|
||||
it("should not need required fields to be present", async () => {
|
||||
const table = await config.api.table.save(
|
||||
saveTableRequest({
|
||||
schema: {
|
||||
name: {
|
||||
name: "name",
|
||||
type: FieldType.STRING,
|
||||
constraints: {
|
||||
presence: true,
|
||||
},
|
||||
},
|
||||
age: {
|
||||
name: "age",
|
||||
type: FieldType.NUMBER,
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
await Promise.all([
|
||||
config.api.row.save(table._id!, { name: "Steve", age: 30 }),
|
||||
config.api.row.save(table._id!, { name: "Jane", age: 31 }),
|
||||
])
|
||||
|
||||
const view = await config.api.viewV2.create({
|
||||
tableId: table._id!,
|
||||
name: generator.guid(),
|
||||
schema: {
|
||||
sum: {
|
||||
visible: true,
|
||||
calculationType: CalculationType.SUM,
|
||||
field: "age",
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const response = await config.api.viewV2.search(view.id, {
|
||||
query: {},
|
||||
})
|
||||
|
||||
expect(response.rows).toHaveLength(1)
|
||||
expect(response.rows[0].sum).toEqual(61)
|
||||
})
|
||||
})
|
||||
|
||||
describe("permissions", () => {
|
||||
|
|
|
@ -23,8 +23,8 @@ import {
|
|||
Row,
|
||||
Table,
|
||||
TableSchema,
|
||||
ViewFieldMetadata,
|
||||
ViewV2,
|
||||
ViewV2Schema,
|
||||
} from "@budibase/types"
|
||||
import sdk from "../../sdk"
|
||||
import { helpers } from "@budibase/shared-core"
|
||||
|
@ -262,7 +262,7 @@ export async function squashLinks<T = Row[] | Row>(
|
|||
FeatureFlag.ENRICHED_RELATIONSHIPS
|
||||
)
|
||||
|
||||
let viewSchema: Record<string, ViewFieldMetadata> = {}
|
||||
let viewSchema: ViewV2Schema = {}
|
||||
if (sdk.views.isView(source)) {
|
||||
if (helpers.views.isCalculationView(source)) {
|
||||
return enriched
|
||||
|
|
|
@ -96,6 +96,8 @@ export async function search(
|
|||
const query: SearchFilters = viewQuery || {}
|
||||
const viewFilters = view.query as SearchFilter[]
|
||||
|
||||
delete options.query.onEmptyFilter
|
||||
|
||||
// Extract existing fields
|
||||
const existingFields =
|
||||
viewFilters
|
||||
|
@ -118,6 +120,9 @@ export async function search(
|
|||
conditions: [viewQuery as SearchFilterGroup, options.query],
|
||||
},
|
||||
}
|
||||
if (viewQuery.onEmptyFilter) {
|
||||
options.query.onEmptyFilter = viewQuery.onEmptyFilter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,7 +20,7 @@ import { Format } from "../../../api/controllers/view/exporters"
|
|||
import sdk from "../.."
|
||||
import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils"
|
||||
import { isSQL } from "../../../integrations/utils"
|
||||
import { docIds } from "@budibase/backend-core"
|
||||
import { docIds, sql } from "@budibase/backend-core"
|
||||
import { getTableFromSource } from "../../../api/controllers/row/utils"
|
||||
|
||||
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
|
||||
|
@ -57,8 +57,12 @@ export function getSQLClient(datasource: Datasource): SqlClient {
|
|||
export function processRowCountResponse(
|
||||
response: DatasourcePlusQueryResponse
|
||||
): number {
|
||||
if (response && response.length === 1 && "__bb_total" in response[0]) {
|
||||
const total = response[0].__bb_total
|
||||
if (
|
||||
response &&
|
||||
response.length === 1 &&
|
||||
sql.COUNT_FIELD_NAME in response[0]
|
||||
) {
|
||||
const total = response[0][sql.COUNT_FIELD_NAME]
|
||||
return typeof total === "number" ? total : parseInt(total)
|
||||
} else {
|
||||
throw new Error("Unable to count rows in query - no count response")
|
||||
|
|
|
@ -255,19 +255,12 @@ export async function enrichSchema(
|
|||
view: ViewV2,
|
||||
tableSchema: TableSchema
|
||||
): Promise<ViewV2Enriched> {
|
||||
const tableCache: Record<string, Table> = {}
|
||||
|
||||
async function populateRelTableSchema(
|
||||
tableId: string,
|
||||
viewFields: Record<string, RelationSchemaField>
|
||||
) {
|
||||
if (!tableCache[tableId]) {
|
||||
tableCache[tableId] = await sdk.tables.getTable(tableId)
|
||||
}
|
||||
const relTable = tableCache[tableId]
|
||||
|
||||
const relTable = await sdk.tables.getTable(tableId)
|
||||
const result: Record<string, ViewV2ColumnEnriched> = {}
|
||||
|
||||
for (const relTableFieldName of Object.keys(relTable.schema)) {
|
||||
const relTableField = relTable.schema[relTableFieldName]
|
||||
if ([FieldType.LINK, FieldType.FORMULA].includes(relTableField.type)) {
|
||||
|
@ -296,15 +289,24 @@ export async function enrichSchema(
|
|||
|
||||
const viewSchema = view.schema || {}
|
||||
const anyViewOrder = Object.values(viewSchema).some(ui => ui.order != null)
|
||||
for (const key of Object.keys(tableSchema).filter(
|
||||
k => tableSchema[k].visible !== false
|
||||
)) {
|
||||
|
||||
const visibleSchemaFields = Object.keys(viewSchema).filter(key => {
|
||||
if (helpers.views.isCalculationField(viewSchema[key])) {
|
||||
return viewSchema[key].visible !== false
|
||||
}
|
||||
return key in tableSchema && tableSchema[key].visible !== false
|
||||
})
|
||||
const visibleTableFields = Object.keys(tableSchema).filter(
|
||||
key => tableSchema[key].visible !== false
|
||||
)
|
||||
const visibleFields = new Set([...visibleSchemaFields, ...visibleTableFields])
|
||||
for (const key of visibleFields) {
|
||||
// if nothing specified in view, then it is not visible
|
||||
const ui = viewSchema[key] || { visible: false }
|
||||
schema[key] = {
|
||||
...tableSchema[key],
|
||||
...ui,
|
||||
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key].order,
|
||||
order: anyViewOrder ? ui?.order ?? undefined : tableSchema[key]?.order,
|
||||
columns: undefined,
|
||||
}
|
||||
|
||||
|
@ -316,10 +318,7 @@ export async function enrichSchema(
|
|||
}
|
||||
}
|
||||
|
||||
return {
|
||||
...view,
|
||||
schema: schema,
|
||||
}
|
||||
return { ...view, schema }
|
||||
}
|
||||
|
||||
export function syncSchema(
|
||||
|
|
|
@ -7,11 +7,11 @@ import {
|
|||
BulkImportRequest,
|
||||
BulkImportResponse,
|
||||
SearchRowResponse,
|
||||
RowSearchParams,
|
||||
DeleteRows,
|
||||
DeleteRow,
|
||||
PaginatedSearchRowResponse,
|
||||
RowExportFormat,
|
||||
SearchRowRequest,
|
||||
} from "@budibase/types"
|
||||
import { Expectations, TestAPI } from "./base"
|
||||
|
||||
|
@ -136,7 +136,7 @@ export class RowAPI extends TestAPI {
|
|||
)
|
||||
}
|
||||
|
||||
search = async <T extends RowSearchParams>(
|
||||
search = async <T extends SearchRowRequest>(
|
||||
sourceId: string,
|
||||
params?: T,
|
||||
expectations?: Expectations
|
||||
|
|
|
@ -134,8 +134,10 @@ async function processDefaultValues(table: Table, row: Row) {
|
|||
|
||||
for (const [key, schema] of Object.entries(table.schema)) {
|
||||
if ("default" in schema && schema.default != null && row[key] == null) {
|
||||
const processed = await processString(schema.default, ctx)
|
||||
|
||||
const processed =
|
||||
typeof schema.default === "string"
|
||||
? await processString(schema.default, ctx)
|
||||
: schema.default
|
||||
try {
|
||||
row[key] = coerce(processed, schema.type)
|
||||
} catch (err: any) {
|
||||
|
|
|
@ -53,8 +53,9 @@ const allowDefaultColumnByType: Record<FieldType, boolean> = {
|
|||
[FieldType.DATETIME]: true,
|
||||
[FieldType.LONGFORM]: true,
|
||||
[FieldType.STRING]: true,
|
||||
[FieldType.OPTIONS]: true,
|
||||
[FieldType.ARRAY]: true,
|
||||
|
||||
[FieldType.OPTIONS]: false,
|
||||
[FieldType.AUTO]: false,
|
||||
[FieldType.INTERNAL]: false,
|
||||
[FieldType.BARCODEQR]: false,
|
||||
|
@ -64,7 +65,6 @@ const allowDefaultColumnByType: Record<FieldType, boolean> = {
|
|||
[FieldType.ATTACHMENTS]: false,
|
||||
[FieldType.ATTACHMENT_SINGLE]: false,
|
||||
[FieldType.SIGNATURE_SINGLE]: false,
|
||||
[FieldType.ARRAY]: false,
|
||||
[FieldType.LINK]: false,
|
||||
[FieldType.BB_REFERENCE]: false,
|
||||
[FieldType.BB_REFERENCE_SINGLE]: false,
|
||||
|
|
|
@ -161,6 +161,7 @@ export interface OptionsFieldMetadata extends BaseFieldSchema {
|
|||
constraints: FieldConstraints & {
|
||||
inclusion: string[]
|
||||
}
|
||||
default?: string
|
||||
}
|
||||
|
||||
export interface ArrayFieldMetadata extends BaseFieldSchema {
|
||||
|
@ -169,6 +170,7 @@ export interface ArrayFieldMetadata extends BaseFieldSchema {
|
|||
type: JsonFieldSubType.ARRAY
|
||||
inclusion: string[]
|
||||
}
|
||||
default?: string[]
|
||||
}
|
||||
|
||||
interface BaseFieldSchema extends UIFieldMetadata {
|
||||
|
|
|
@ -71,9 +71,11 @@ export interface ViewV2 {
|
|||
order?: SortOrder
|
||||
type?: SortType
|
||||
}
|
||||
schema?: Record<string, ViewFieldMetadata>
|
||||
schema?: ViewV2Schema
|
||||
}
|
||||
|
||||
export type ViewV2Schema = Record<string, ViewFieldMetadata>
|
||||
|
||||
export type ViewSchema = ViewCountOrSumSchema | ViewStatisticsSchema
|
||||
|
||||
export interface ViewCountOrSumSchema {
|
||||
|
|
Loading…
Reference in New Issue