Skip to content

Commit

Permalink
feat: field id should be unique rule
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Oct 9, 2024
1 parent 3e8af11 commit 147d8a9
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{#each value as item}
{#if !!item}
<span
class="me-2 min-w-8 rounded bg-gray-100 px-2 py-1 text-xs font-medium text-gray-800 dark:bg-gray-700 dark:text-gray-300"
class="me-2 min-w-8 truncate rounded bg-gray-200 px-2 py-0.5 text-xs font-medium text-gray-800 dark:bg-gray-700 dark:text-gray-300"
>
{item}
</span>
Expand Down
3 changes: 2 additions & 1 deletion packages/table/src/methods/create-field.method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { None, Option, Some, andOptions, applyRules } from "@undb/domain"
import { FieldCreatedEvent } from "../events"
import { type Field, type ICreateFieldDTO } from "../modules"
import { FieldFactory } from "../modules/schema/fields/field.factory"
import { FieldIdShouldBeUnique } from "../modules/schema/rules/field-id-should-be-unique.rule"
import { FieldNameShouldBeUnique } from "../modules/schema/rules/field-name-should-be-unique.rule"
import type { TableComositeSpecification } from "../specifications"
import type { TableDo } from "../table.do"
Expand All @@ -18,7 +19,7 @@ export function $createFieldSpec(this: TableDo, field: Field): Option<TableComos
).unwrap()
spec.mutate(this)

applyRules(new FieldNameShouldBeUnique(this.schema))
applyRules(new FieldNameShouldBeUnique(this.schema), new FieldIdShouldBeUnique(this.schema))

const event = new FieldCreatedEvent({
tableId: this.id.value,
Expand Down
3 changes: 2 additions & 1 deletion packages/table/src/methods/update-field.method.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Option, Some, applyRules } from "@undb/domain"
import { FieldUpdatedEvent } from "../events"
import { type IUpdateFieldDTO } from "../modules"
import { FieldIdShouldBeUnique } from "../modules/schema/rules/field-id-should-be-unique.rule"
import { FieldNameShouldBeUnique } from "../modules/schema/rules/field-name-should-be-unique.rule"
import type { TableComositeSpecification } from "../specifications"
import type { TableDo } from "../table.do"
Expand All @@ -10,7 +11,7 @@ export function updateFieldMethod(this: TableDo, dto: IUpdateFieldDTO): Option<T
// TODO: update form
spec.mutate(this)

applyRules(new FieldNameShouldBeUnique(this.schema))
applyRules(new FieldNameShouldBeUnique(this.schema), new FieldIdShouldBeUnique(this.schema))

const event = new FieldUpdatedEvent({
tableId: this.id.value,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DomainRules, ExceptionBase } from "@undb/domain"
import type { Schema } from "../schema.vo"

class FieldIdShouldBeUniqueError extends ExceptionBase {
code = "table:FIELD_ID_SHOULD_BE_UNIQUE"

constructor() {
super("Field id should be unique")
}
}

export class FieldIdShouldBeUnique extends DomainRules<FieldIdShouldBeUniqueError> {
constructor(private readonly schema: Schema) {
super()
}

override err = new FieldIdShouldBeUniqueError()

override isBroken(): boolean {
const ids = this.schema.fields.map((field) => field.id.value)
return new Set(ids).size !== ids.length
}
}
1 change: 1 addition & 0 deletions packages/table/src/modules/schema/rules/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./field-id-should-be-unique.rule"
export * from "./field-name-should-be-unique.rule"
4 changes: 2 additions & 2 deletions packages/table/src/table.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type ICreateSchemaDTO,
type ICreateTablesReferenceFieldDTO,
} from "./modules"
import { FieldNameShouldBeUnique } from "./modules/schema/rules"
import { FieldIdShouldBeUnique, FieldNameShouldBeUnique } from "./modules/schema/rules"
import { TableIdVo } from "./table-id.vo"
import { TableBuilder } from "./table.builder"
import type { TableDo } from "./table.do"
Expand Down Expand Up @@ -65,7 +65,7 @@ export class TableFactory {
.createForms(dto.forms)
.build()

applyRules(new FieldNameShouldBeUnique(table.schema))
applyRules(new FieldNameShouldBeUnique(table.schema), new FieldIdShouldBeUnique(table.schema))

// @ts-ignore - TODO: fix this
table.addDomainEvent(new TableCreatedEvent({ table: table.toJSON() }))
Expand Down
18 changes: 9 additions & 9 deletions packages/template/src/templates/hr.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@
}
}
},
"Department Name": {
"id": "department_name",
"type": "rollup",
"option": {
"referenceFieldId": "department",
"rollupFieldId": "department_name",
"fn": "lookup"
}
},
"Start Date": {
"id": "start_date",
"type": "date",
Expand Down Expand Up @@ -130,15 +139,6 @@
]
}
},
"Department Name": {
"id": "department_name",
"type": "rollup",
"option": {
"referenceFieldId": "department",
"rollupFieldId": "department_name",
"fn": "lookup"
}
},
"Manager Name": {
"id": "manager_name",
"type": "rollup",
Expand Down

0 comments on commit 147d8a9

Please sign in to comment.