Skip to content

Commit

Permalink
feat: allow to create template reference symmetric field
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Sep 27, 2024
1 parent d596808 commit ed946c1
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 28 deletions.
17 changes: 13 additions & 4 deletions apps/backend/src/modules/template/template.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { singleton } from "@undb/di"
import { baseTemplateSchema } from "@undb/template"
import { None } from "@undb/domain"
import { baseTemplateSchema, injectTemplateQueryRepository, type ITemplateQueryRepository } from "@undb/template"
import Elysia from "elysia"

@singleton()
export class TemplateModule {
constructor(
@injectTemplateQueryRepository()
private readonly templateRepo: ITemplateQueryRepository,
) {}
route() {
return new Elysia().get("/api/template/base/schema.json", () => {
return baseTemplateSchema
})
return new Elysia()
.get("/api/template/base/schema.json", () => {
return baseTemplateSchema
})
.get("/api/templates", () => {
return this.templateRepo.find(None)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
export let currentPage: Writable<number | null>
export let isLoading = false
export let total: number
export let hidePagination = false
const t = getTable()
Expand Down Expand Up @@ -350,6 +351,7 @@
</table>
</ScrollArea>

{#if !hidePagination}
<div class="flex items-center justify-center border-t px-4 py-2">
<div class="flex flex-1 flex-row items-center">
<ViewPagination {perPage} bind:currentPage={$currentPage} count={total} />
Expand Down Expand Up @@ -378,6 +380,7 @@
{total} Rows
</span>
</div>
</div>
</div>
</div>
{/if}
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
</div>
<div class="mr-10 space-y-2">
<Button
class="w-44"
disabled={$createFromTemplate.isPending}
on:click={() => $createFromTemplate.mutate({ id: template.id, includeData })}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@
</script>

{#if store}
<GridViewDataTable {viewId} readonly {perPage} {currentPage} isLoading={false} total={records.length} />
{#key $viewId}
<GridViewDataTable
{viewId}
readonly
{perPage}
{currentPage}
isLoading={false}
total={records.length}
hidePagination
/>
{/key}
{/if}
1 change: 1 addition & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const env = createEnv({
clientPrefix: "UNDB_PUBLIC_",
client: {},
server: {
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
UNDB_BASE_URL: z.string().optional().default("http://localhost:3721"),
UNDB_COOKIE_DOMAIN: z.string().optional(),
},
Expand Down
27 changes: 21 additions & 6 deletions packages/persistence/src/template/template-data.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "@undb/env"
import { templates, type IBaseTemplateDTO, type ITemplateDTO } from "@undb/template"

export const templateData: ITemplateDTO[] = [
Expand All @@ -13,6 +14,17 @@ export const templateData: ITemplateDTO[] = [
template: templates.projectManagement as IBaseTemplateDTO,
},
},
{
id: "6ba7b814-9dad-11d1-80b4-00c04fd430c8",
icon: "💼",
name: "CRM",
category: "sales",
description: "A template for managing customer relationships, deals, and activities.",
template: {
type: "base",
template: templates.crm as IBaseTemplateDTO,
},
},
{
id: "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
icon: "🎉",
Expand Down Expand Up @@ -46,15 +58,18 @@ export const templateData: ITemplateDTO[] = [
template: templates.officeInventoryManagement as IBaseTemplateDTO,
},
},
{
]

if (env.NODE_ENV === "development") {
templateData.unshift({
id: "6ba7b814-9dad-11d1-80b4-00c04fd430c8",
icon: "💼",
name: "CRM",
name: "Test",
category: "sales",
description: "A template for managing customer relationships, deals, and activities.",
description: "A template for testing",
template: {
type: "base",
template: templates.crm as IBaseTemplateDTO,
template: templates.test as IBaseTemplateDTO,
},
},
]
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,29 @@ export const createReferenceFieldDTO = createBaseFieldDTO
})
.omit({ display: true })

const createOwnerReferenceFieldOption = z.object({
foreignTable: z.object({
baseName: baseNameSchema.optional(),
tableName: tableName,
}),
})
const createSymmetricReferenceFieldOption = z.object({
symmetricFieldId: fieldId,
})

const createTablesReferenceOption = createReferenceFieldOption
.omit({
foreignTableId: true,
})
.merge(
z.object({
foreignTable: z.object({
baseName: baseNameSchema.optional(),
tableName: tableName,
}),
}),
)
.merge(createOwnerReferenceFieldOption.partial())
.merge(createSymmetricReferenceFieldOption.partial())

export const createTablesReferenceFieldDTO = createReferenceFieldDTO.merge(
z.object({
option: createTablesReferenceOption,
}),
)
export type ICreateTablesReferenceFieldDTO = z.infer<typeof createTablesReferenceFieldDTO>

export type ICreateReferenceFieldDTO = z.infer<typeof createReferenceFieldDTO>
export const updateReferenceFieldDTO = createReferenceFieldDTO
Expand Down Expand Up @@ -121,15 +126,27 @@ export class ReferenceField extends AbstractField<
name: dto.name,
option: { foreignTableId, condition, isOwner: true },
id: FieldIdVo.fromStringOrCreate(dto.id).value,
constraint: dto.constraint,
})
}

static createSymmetricField(foreignTable: TableDo, table: TableDo, field: ReferenceField) {
static createSymmetricField(
foreignTable: TableDo,
table: TableDo,
field: ReferenceField,
dto?: ICreateTablesReferenceFieldDTO,
) {
const symmetricField = new ReferenceField({
type: "reference",
name: table.schema.getNextFieldName(foreignTable.name.value),
option: { isOwner: false, foreignTableId: foreignTable.id.value, symmetricFieldId: field.id.value },
id: FieldIdVo.create().value,
id: dto?.id || FieldIdVo.create().value,
name: table.schema.getNextFieldName(dto?.name || foreignTable.name.value),
option: {
isOwner: false,
foreignTableId: foreignTable.id.value,
symmetricFieldId: field.id.value,
condition: dto?.option.condition,
},
constraint: dto?.constraint,
})

symmetricField.connect(field)
Expand Down
22 changes: 18 additions & 4 deletions packages/table/src/table.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
type ICreateFieldDTO,
type ICreateReferenceFieldDTO,
type ICreateSchemaDTO,
type ICreateTablesReferenceFieldDTO,
} from "./modules"
import { FieldNameShouldBeUnique } from "./modules/schema/rules"
import { TableIdVo } from "./table-id.vo"
Expand Down Expand Up @@ -82,15 +83,25 @@ export class TableFactory {
const baseName = getNextName(baseNames, base.name.value)

const referenceIds = new Set<string>()
const symmetricFields: ICreateTablesReferenceFieldDTO[] = []
const tables = dtos.map((dto) => {
const schema = dto.schema
.filter((f) => f.type !== "rollup")
.map((field) => {
if (field.type === "rollup") {
return null
}
if (field.type === "reference") {
const foreignTableId = ids.mustGet(baseName, field.option.foreignTable.tableName)
if (!field.id) {
field.id = FieldIdVo.create().value
}

// Symmetric field
if (!field.option.foreignTable) {
symmetricFields.push(field)
return null
}

const foreignTableId = ids.mustGet(baseName, field.option.foreignTable.tableName)
if (field.option.createSymmetricField) {
referenceIds.add(field.id!)
}
Expand All @@ -103,7 +114,8 @@ export class TableFactory {
} as ICreateReferenceFieldDTO
}
return field as ICreateFieldDTO
}) as ICreateSchemaDTO
})
.filter((v) => !!v) as ICreateSchemaDTO

const id = ids.mustGet(baseName, dto.name)
const table = this.create({ ...dto, id, schema })
Expand All @@ -118,7 +130,9 @@ export class TableFactory {
if (!foreignTable) {
throw new Error("Foreign table not found")
}
const symmetricField = ReferenceField.createSymmetricField(table, foreignTable, referenceField)
const dto = symmetricFields.find((f) => f.option.symmetricFieldId === referenceField.id.value)
console.log(dto)
const symmetricField = ReferenceField.createSymmetricField(table, foreignTable, referenceField, dto)
foreignTable.$createFieldSpec(symmetricField)
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/template/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { default as crm } from "./crm.base.json"
import { default as eventPlaningList } from "./eventPlaning.base.json"
import { default as officeInventoryManagement } from "./officeInventoryManagement.base.json"
import { default as projectManagement } from "./projectManagement.base.json"
import { default as test } from "./test.base.json"
import { default as todoList } from "./todoList.base.json"

const templates = {
test,
todoList,
projectManagement,
officeInventoryManagement,
Expand Down
77 changes: 77 additions & 0 deletions packages/template/src/templates/test.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"Test": {
"tables": {
"Table1": {
"schema": {
"Title": {
"id": "title",
"type": "string"
},
"Ref2": {
"id": "ref2",
"type": "reference",
"option": {
"createSymmetricField": true,
"foreignTable": {
"tableName": "Table2"
}
}
},
"Roll1": {
"id": "roll1",
"type": "rollup",
"option": {
"referenceFieldId": "ref2",
"rollupFieldId": "title",
"fn": "lookup"
}
}
},
"records": [
{
"Title": "1-1",
"Ref2": ["1", "2"]
},
{
"Title": "1-2",
"Ref2": ["2"]
}
]
},
"Table2": {
"schema": {
"Title": {
"id": "title",
"type": "string"
},
"Ref1": {
"id": "ref1",
"type": "reference",
"option": {
"symmetricFieldId": "ref2"
}
},
"T1": {
"id": "t1",
"type": "rollup",
"option": {
"referenceFieldId": "ref1",
"rollupFieldId": "title",
"fn": "lookup"
}
}
},
"records": [
{
"id": "1",
"Title": "2-1"
},
{
"id": "2",
"Title": "2-2"
}
]
}
}
}
}

0 comments on commit ed946c1

Please sign in to comment.