Skip to content

Commit

Permalink
feat: support formula field aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Oct 30, 2024
1 parent 6688155 commit b1ceece
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 0 additions & 2 deletions packages/table/src/modules/schema/fields/field.aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { checkboxFieldAggregate } from "./variants/checkbox-field/checkbox-field
import { currencyFieldAggregate } from "./variants/currency-field/currency-field.aggregate"
import { durationFieldAggregate } from "./variants/duration-field/duration-field.aggregate"
import { emailFieldAggregate } from "./variants/email-field/email-field.aggregate"
import { formulaFieldAggregate } from "./variants/formula-field/formula-field.aggregate"
import { jsonFieldAggregate } from "./variants/json-field/json-field.aggregate"
import { longTextFieldAggregate } from "./variants/long-text-field/long-text-field.aggregate"
import { percentageFieldAggregate } from "./variants/percentage-field/percentage-field.aggregate"
Expand All @@ -31,6 +30,5 @@ export const fieldAggregate = stringFieldAggregate
.or(currencyFieldAggregate)
.or(durationFieldAggregate)
.or(percentageFieldAggregate)
.or(formulaFieldAggregate)

export type IFieldAggregate = z.infer<typeof fieldAggregate>
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import type { ReturnType } from "@undb/formula"
import { z } from "@undb/zod"

export const formulaFieldAggregate = z.enum([
//
// "sum",
// "avg",
// "min",
// "max",
"count_empty",
"count_uniq",
"count_not_empty",
"percent_empty",
"percent_not_empty",
"percent_uniq",
])
export const createFormulaFieldAggregate = (returnType: ReturnType) => {
if (returnType === "boolean") {
return z.enum(["count_true", "count_false"])
} else if (returnType === "number") {
return z.enum([
"sum",
"avg",
"min",
"max",
"count_empty",
"count_uniq",
"count_not_empty",
"percent_empty",
"percent_not_empty",
"percent_uniq",
])
}

return z.enum(["count_empty", "count_uniq", "count_not_empty", "percent_empty", "percent_not_empty", "percent_uniq"])
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FormulaFieldValue } from "./formula-field-value.vo"

export class FormulaEqual extends RecordComositeSpecification {
constructor(
readonly value: number | null,
readonly value: number | null | boolean,
readonly fieldId: FieldId,
) {
super(fieldId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { IFieldVisitor } from "../../field.visitor"
import { AbstractField, baseFieldDTO, createBaseFieldDTO } from "../abstract-field.vo"
import { StringEmpty } from "../string-field"
import { FormulaFieldValue } from "./formula-field-value.vo"
import { formulaFieldAggregate } from "./formula-field.aggregate"
import { createFormulaFieldAggregate } from "./formula-field.aggregate"
import {
createFormulaFieldCondition,
type IFormulaFieldCondition,
Expand Down Expand Up @@ -138,6 +138,8 @@ export class FormulaField extends AbstractField<FormulaFieldValue, undefined, IF
.with({ op: "lte" }, ({ value }) => new FormulaLTE(value, this.id))
.with({ op: "is_empty" }, () => new StringEmpty(this.id))
.with({ op: "is_not_empty" }, () => new StringEmpty(this.id).not())
.with({ op: "is_true" }, () => new FormulaEqual(true, this.id))
.with({ op: "is_false" }, () => new FormulaEqual(false, this.id).not())
.exhaustive()

return Option(spec)
Expand All @@ -152,7 +154,7 @@ export class FormulaField extends AbstractField<FormulaFieldValue, undefined, IF
}

override get aggregate() {
return formulaFieldAggregate
return createFormulaFieldAggregate(this.returnType)
}

get fn() {
Expand Down

0 comments on commit b1ceece

Please sign in to comment.