Merge branch 'master' of github.com:budibase/budibase into view-calculation-sql-4
This commit is contained in:
commit
612d0844bf
|
@ -1,5 +1,5 @@
|
||||||
export * as utils from "./utils"
|
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 { default as SqlTable } from "./sqlTable"
|
||||||
export * as designDoc from "./designDoc"
|
export * as designDoc from "./designDoc"
|
||||||
|
|
|
@ -43,6 +43,8 @@ import { cloneDeep } from "lodash"
|
||||||
|
|
||||||
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
|
type QueryFunction = (query: SqlQuery | SqlQuery[], operation: Operation) => any
|
||||||
|
|
||||||
|
export const COUNT_FIELD_NAME = "__bb_total"
|
||||||
|
|
||||||
function getBaseLimit() {
|
function getBaseLimit() {
|
||||||
const envLimit = environment.SQL_MAX_ROWS
|
const envLimit = environment.SQL_MAX_ROWS
|
||||||
? parseInt(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")
|
throw new Error("SQL counting requires primary key to be supplied")
|
||||||
}
|
}
|
||||||
return query.countDistinct(
|
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
|
// Look up the component tree and find something that is provided by an
|
||||||
// ancestor that matches our datasource. This is for backwards compatibility
|
// ancestor that matches our datasource. This is for backwards compatibility
|
||||||
// as previously we could use the "closest" context.
|
// 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
|
// Check for matching view datasource
|
||||||
if (
|
if (
|
||||||
dataSource.type === "viewV2" &&
|
dataSource.type === "viewV2" &&
|
||||||
|
|
|
@ -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("bindings", () => {
|
||||||
describe("string column", () => {
|
describe("string column", () => {
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
|
|
@ -20,7 +20,7 @@ import { Format } from "../../../api/controllers/view/exporters"
|
||||||
import sdk from "../.."
|
import sdk from "../.."
|
||||||
import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils"
|
import { extractViewInfoFromID, isRelationshipColumn } from "../../../db/utils"
|
||||||
import { isSQL } from "../../../integrations/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"
|
import { getTableFromSource } from "../../../api/controllers/row/utils"
|
||||||
|
|
||||||
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
|
const SQL_CLIENT_SOURCE_MAP: Record<SourceName, SqlClient | undefined> = {
|
||||||
|
@ -57,8 +57,12 @@ export function getSQLClient(datasource: Datasource): SqlClient {
|
||||||
export function processRowCountResponse(
|
export function processRowCountResponse(
|
||||||
response: DatasourcePlusQueryResponse
|
response: DatasourcePlusQueryResponse
|
||||||
): number {
|
): number {
|
||||||
if (response && response.length === 1 && "__bb_total" in response[0]) {
|
if (
|
||||||
const total = response[0].__bb_total
|
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)
|
return typeof total === "number" ? total : parseInt(total)
|
||||||
} else {
|
} else {
|
||||||
throw new Error("Unable to count rows in query - no count response")
|
throw new Error("Unable to count rows in query - no count response")
|
||||||
|
|
|
@ -134,8 +134,10 @@ async function processDefaultValues(table: Table, row: Row) {
|
||||||
|
|
||||||
for (const [key, schema] of Object.entries(table.schema)) {
|
for (const [key, schema] of Object.entries(table.schema)) {
|
||||||
if ("default" in schema && schema.default != null && row[key] == null) {
|
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 {
|
try {
|
||||||
row[key] = coerce(processed, schema.type)
|
row[key] = coerce(processed, schema.type)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
|
|
|
@ -53,8 +53,9 @@ const allowDefaultColumnByType: Record<FieldType, boolean> = {
|
||||||
[FieldType.DATETIME]: true,
|
[FieldType.DATETIME]: true,
|
||||||
[FieldType.LONGFORM]: true,
|
[FieldType.LONGFORM]: true,
|
||||||
[FieldType.STRING]: true,
|
[FieldType.STRING]: true,
|
||||||
|
[FieldType.OPTIONS]: true,
|
||||||
|
[FieldType.ARRAY]: true,
|
||||||
|
|
||||||
[FieldType.OPTIONS]: false,
|
|
||||||
[FieldType.AUTO]: false,
|
[FieldType.AUTO]: false,
|
||||||
[FieldType.INTERNAL]: false,
|
[FieldType.INTERNAL]: false,
|
||||||
[FieldType.BARCODEQR]: false,
|
[FieldType.BARCODEQR]: false,
|
||||||
|
@ -64,7 +65,6 @@ const allowDefaultColumnByType: Record<FieldType, boolean> = {
|
||||||
[FieldType.ATTACHMENTS]: false,
|
[FieldType.ATTACHMENTS]: false,
|
||||||
[FieldType.ATTACHMENT_SINGLE]: false,
|
[FieldType.ATTACHMENT_SINGLE]: false,
|
||||||
[FieldType.SIGNATURE_SINGLE]: false,
|
[FieldType.SIGNATURE_SINGLE]: false,
|
||||||
[FieldType.ARRAY]: false,
|
|
||||||
[FieldType.LINK]: false,
|
[FieldType.LINK]: false,
|
||||||
[FieldType.BB_REFERENCE]: false,
|
[FieldType.BB_REFERENCE]: false,
|
||||||
[FieldType.BB_REFERENCE_SINGLE]: false,
|
[FieldType.BB_REFERENCE_SINGLE]: false,
|
||||||
|
|
|
@ -161,6 +161,7 @@ export interface OptionsFieldMetadata extends BaseFieldSchema {
|
||||||
constraints: FieldConstraints & {
|
constraints: FieldConstraints & {
|
||||||
inclusion: string[]
|
inclusion: string[]
|
||||||
}
|
}
|
||||||
|
default?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ArrayFieldMetadata extends BaseFieldSchema {
|
export interface ArrayFieldMetadata extends BaseFieldSchema {
|
||||||
|
@ -169,6 +170,7 @@ export interface ArrayFieldMetadata extends BaseFieldSchema {
|
||||||
type: JsonFieldSubType.ARRAY
|
type: JsonFieldSubType.ARRAY
|
||||||
inclusion: string[]
|
inclusion: string[]
|
||||||
}
|
}
|
||||||
|
default?: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
interface BaseFieldSchema extends UIFieldMetadata {
|
interface BaseFieldSchema extends UIFieldMetadata {
|
||||||
|
|
Loading…
Reference in New Issue