-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
223 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/command-handlers/src/handlers/create-from-template.command-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { CreateFromTemplateCommand, type ICreateFromTemplateCommandOutput } from "@undb/commands" | ||
import { mustGetCurrentSpaceId } from "@undb/context/server" | ||
import { commandHandler } from "@undb/cqrs" | ||
import { singleton } from "@undb/di" | ||
import { type ICommandHandler } from "@undb/domain" | ||
import { createLogger } from "@undb/logger" | ||
import { injectTemplateService, type ITemplateService, templates } from "@undb/template" | ||
|
||
@commandHandler(CreateFromTemplateCommand) | ||
@singleton() | ||
export class CreateFromTemplateCommandHandler | ||
implements ICommandHandler<CreateFromTemplateCommand, ICreateFromTemplateCommandOutput> | ||
{ | ||
private readonly logger = createLogger(CreateFromTemplateCommandHandler.name) | ||
|
||
constructor( | ||
@injectTemplateService() | ||
private readonly templateService: ITemplateService, | ||
) {} | ||
|
||
async execute(command: CreateFromTemplateCommand): Promise<ICreateFromTemplateCommandOutput> { | ||
this.logger.info(`create from template command received: ${command.templateName}`) | ||
|
||
const template = templates["test"] | ||
|
||
const spaceId = mustGetCurrentSpaceId() | ||
const result = await this.templateService.createBase(template, spaceId) | ||
|
||
return { baseIds: result.map(({ base }) => base.id.value) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { baseIdSchema } from "@undb/base" | ||
import { Command, type CommandProps } from "@undb/domain" | ||
import { z } from "@undb/zod" | ||
|
||
export const createFromTemplateCommand = z.object({ | ||
templateName: z.string(), | ||
}) | ||
|
||
export type ICreateFromTemplateCommand = z.infer<typeof createFromTemplateCommand> | ||
|
||
export const createFromTemplateCommandOutput = z.object({ | ||
baseIds: z.array(baseIdSchema), | ||
}) | ||
|
||
export type ICreateFromTemplateCommandOutput = z.infer<typeof createFromTemplateCommandOutput> | ||
|
||
export class CreateFromTemplateCommand extends Command implements ICreateFromTemplateCommand { | ||
public readonly templateName: string | ||
|
||
constructor(props: CommandProps<ICreateFromTemplateCommand>) { | ||
super(props) | ||
this.templateName = props.templateName | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./dto" | ||
export * from "./schema" | ||
export * from "./service" | ||
export * from "./templates" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./template.service" | ||
export * from "./template.service.provider" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { container, inject } from "@undb/di" | ||
import { TemplateService } from "./template.service" | ||
|
||
export const TEMPLATE_SERVICE = Symbol.for("TemplateService") | ||
export const injectTemplateService = () => inject(TEMPLATE_SERVICE) | ||
container.register(TEMPLATE_SERVICE, { useClass: TemplateService }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,36 @@ | ||
import { Base, injectBaseRepository, WithBaseSpaceId, type IBaseRepository } from "@undb/base" | ||
import { singleton } from "@undb/di" | ||
import { createLogger } from "@undb/logger" | ||
import { injectTableRepository, TableDo, type ITableRepository } from "@undb/table" | ||
import type { IBaseTemplateDTO } from "../dto" | ||
import { TemplateFactory } from "../template.factory" | ||
|
||
export interface ITemplateService { | ||
create(dto: IBaseTemplateDTO): Promise<void> | ||
createBase(dto: IBaseTemplateDTO, spaceId: string): Promise<{ base: Base; tables: TableDo[] }[]> | ||
} | ||
|
||
@singleton() | ||
export class TemplateService implements ITemplateService { | ||
private readonly logger = createLogger(TemplateService.name) | ||
async create(dto: IBaseTemplateDTO): Promise<void> { | ||
|
||
constructor( | ||
@injectBaseRepository() | ||
private readonly baseRepository: IBaseRepository, | ||
@injectTableRepository() | ||
private readonly tableRepository: ITableRepository, | ||
) {} | ||
|
||
async createBase(dto: IBaseTemplateDTO, spaceId: string): Promise<{ base: Base; tables: TableDo[] }[]> { | ||
this.logger.info(dto) | ||
const bases = await this.baseRepository.find(new WithBaseSpaceId(spaceId)) | ||
const baseNames = bases.map((base) => base.name.value) | ||
const result = TemplateFactory.create(dto, baseNames, spaceId) | ||
|
||
for (const { base, tables } of result) { | ||
await this.baseRepository.insert(base) | ||
await this.tableRepository.insertMany(tables) | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Base, BaseFactory } from "@undb/base" | ||
import { type ICreateSchemaDTO, TableCreator, TableDo } from "@undb/table" | ||
import { getNextName } from "@undb/utils" | ||
import { type IBaseTemplateDTO } from "./dto/template.dto" | ||
|
||
export class TemplateFactory { | ||
static create(template: IBaseTemplateDTO, baseNames: string[], spaceId: string): { base: Base; tables: TableDo[] }[] { | ||
const result: { base: Base; tables: TableDo[] }[] = [] | ||
for (const [name, b] of Object.entries(template)) { | ||
const baseName = getNextName(baseNames, name) | ||
const base = BaseFactory.create({ name: baseName, spaceId }) | ||
const baseId = base.id.value | ||
|
||
const tables: TableDo[] = [] | ||
for (const [name, table] of Object.entries(b.tables)) { | ||
const schema = Object.entries(table.schema).map(([name, field]) => ({ ...field, name })) as ICreateSchemaDTO | ||
|
||
const t = new TableCreator().create({ baseId, name, schema, spaceId }) | ||
tables.push(t) | ||
} | ||
|
||
result.push({ base, tables }) | ||
} | ||
|
||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { IBaseTemplateDTO } from "../dto" | ||
import { default as test } from "./test.base.json" | ||
|
||
const templates: Record<string, IBaseTemplateDTO> = { test } as const | ||
|
||
export { templates } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.